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!

KeyAscii

Status
Not open for further replies.

Goodloe

Programmer
Sep 4, 2001
25
US
I'm working on a project that has a form with several text boxes. These text boxes include "text and Numeric Data".
Using KeyAscii, I would like for the data to be restricted to the proper text boxes. In other words; if a text box is setup to accept text only, and if a numeric value is entered in that text box, not only do I want the text box not to accept the numeric data, I would also like a "msgbox indicating an error message as soon as the wrong data had been entered.

The following code does work in terms of not allowing the wrong data to be entered. However, as I just mentioned, I also need a "msgbox" to appear as soon as the wrong data is entered. So, can you provide some sample code that would include a message box?

Private Sub txtForeName_KeyPress(KeyAscii As Integer)
If KeyAscii >= Asc(&quot;0&quot;) And KeyAscii <= Asc(&quot;9&quot;) Then KeyAscii = 0
End Sub
 
You are close


Private Sub txtForeName_KeyPress(KeyAscii As Integer)
If KeyAscii >= Asc(&quot;0&quot;) And KeyAscii <= Asc(&quot;9&quot;) Then
KeyAscii = 0
MsgBox &quot;Letters Only&quot;
End If
End Sub

Hope this helps
If you choose to battle wits with the witless be prepared to lose.
[machinegun][ducky]
 
Function OnlyNumerics(KeyAscii As Integer) As Integer

Select Case KeyAscii
Case 8, 48 To 57
Case Else
KeyAscii = 0
MsgBox (&quot;You have entered an invalid key!&quot;), vbInformation, &quot;Invalid Key&quot;
End Select
OnlyNumerics = KeyAscii

End Function

Private Sub Text1_KeyPress(KeyAscii As Integer)
KeyAscii = OnlyNumerics(KeyAscii)
End Sub Swi
 
If KeyAscii < 48 Or KeyAscii > 57 Then
If KeyAscii = 8 Or KeyAscii = 13 Then
'allows enter and backspace keys to function
Else
MsgBox &quot;Numeric Data Only&quot;, vbExclamation, &quot;Invalid Data&quot;
KeyAscii = 0
End If
End If
 
Thank you very much, I knew it was something very simple, I just couldn't put my finger on it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top