Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Ensure Numeric Data is not Entered into a Text Field 1

Status
Not open for further replies.

Terpsfan

Programmer
Dec 8, 2000
954
US
I have found this short code snippit to be useful to prevent the user from entering numeric data in a text field, on purpose or by accident. When the user attempts to enter a numeric key a message is delivered to the user and the entry is nullified. The cursor is returned to the exact place before the user entered the numeric key. I'm not sure if this is too elementary for you computer experts, but I think someone might find it handy.

Private Sub txtLastName_KeyPress(Key Ascii As Integer)
If IsNumeric(Chr(KeyAscii)) Then
MsgBox "Numeric data is not allowed in this field"
KeyAscii = 0
End If
End Sub
 
Hi,

copy and paste the following code in a module

Public Function validate(KeyAscii As Integer) As Integer
If KeyAscii = 8 Or KeyAscii = 46 Then
validate = KeyAscii
Exit Function
End If
If KeyAscii < 48 Or KeyAscii > 57 Then
MsgBox &quot;Enter Only Numbers&quot;, vbCritical, &quot;Entry Error&quot;
validate = 0
Else
validate = KeyAscii
Exit Function
End If
End Function

and in the form text_keypress call

KeyAscii = validate(KeyAscii)


bye

 
Why &quot;bother&quot; the user with a message box?
Allow backspace and one decimal.
Code:
Private Sub txtKeyPressDecimal(ctl as control,KeyAscii As Integer)
    Do
        if KeyAscii = 8 then exit do ' BACKSPACE
        Select Case Chr(KeyAscii)
        Case &quot;0&quot; to &quot;9&quot;
        Case &quot;.&quot;
            if Instr(ctl.Text) > 0 then
                KeyAscii = 0
            End if
        Case Else
            Keyascii = 0
        End select
    Exit Loop: Loop
    #if blnBugTheUser then
    if KeyAscii = 0 then
         ctl.Backcolor = Rgb(255,0,0)
         ctl.Forecolor = rgb(255,255,255)
    Else
         ctl.BackColor = rgb(255,255,255)
         ctl.forecolor = rgb(0,0,0)
    End if
    #End if
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top