[RESOLVED] Limiting textbox characters
Hi I have a program where i want the texbox only to input numbers i have the ascii coding but the problem is that it is a private sub so even when i dont have to put in data into the textbox it still does the ascii check. here is my coding
Private Sub txtfirstname_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 33 To 64, 91 To 96, 123 To 126
MsgBox ("Only letters supported for this field! Please input a name.")
KeyAscii = 0
Exit Sub
End Select
End Sub
Re: Limiting textbox characters
Re: Limiting textbox characters
Quote:
Originally Posted by
TechKid
Hi I have a program where i want the texbox only to input numbers i have the ascii coding but the problem is that it is a private sub so even when i dont have to put in data into the textbox it still does the ascii check. here is my coding
I don't understand that part of the post. the KeyPress event is only fired when you press a key. So if you have no data to enter you don't press a key. Why would the event trigger ? As a side note, Tabbing through the control will not trigger the event as the TAB key is "special" that way.
Can you clarify what you mean by "when I dont have to put in data into the textbox it still does the ascii check" ?
Re: Limiting textbox characters
Quote:
Originally Posted by
koolsid
I already have the coding and it works. My problem is i only want it to be enabled when I click add and the texbox is able to accept input. At the begining of my program the textbox is not allowed to acced input until i click add. When i start my program even before i click add the texbox is already formatted and that is my problem
Re: Limiting textbox characters
Quote:
Originally Posted by
Optional
I don't understand that part of the post. the KeyPress event is only fired when you press a key. So if you have no data to enter you don't press a key. Why would the event trigger ? As a side note, Tabbing through the control will not trigger the event as the TAB key is "special" that way.
Can you clarify what you mean by "when I dont have to put in data into the textbox it still does the ascii check" ?
Ok The boxes are used for displaying and inputting information. When it is only diplaying it still checks if the textbox if it has only letters and if i type a number it gives an error. I want it to only check for letters when i click add. I think the problem is because I have it under the private sub. I need to get it under the Add procedure so that it can only check when i click add
Re: Limiting textbox characters
I'm assuming when you only display data your textbox.enabled property is set to false ?
Would a validation of the enabled property help ?
VB Code:
Private Sub txtfirstname_KeyPress(KeyAscii As Integer)
If (Not (txtfirstname.Enabled)) Then Exit Sub
Select Case KeyAscii
Case 33 To 64, 91 To 96, 123 To 126
MsgBox ("Only letters supported for this field! Please input a name.")
KeyAscii = 0
Exit Sub
End Select
End Sub
Re: Limiting textbox characters
Ok... Remove that code from Keypress and validate the contents of the textbox in the Add event...
Something like
Code:
Private Sub CmdAdd_Click()
For i = 1 To Len(Text1.Text)
If Asc(Mid(Text1.Text, i, 1)) > 47 And _
Asc(Mid(Text1.Text, i, 1)) < 58 Then
MsgBox "Textbox cannot have numbers"
Exit For
End If
Next i
End Sub
Re: Limiting textbox characters
Thanks guys It worked. thank you so much
Re: Limiting textbox characters
Edit: - deleted post - Didn't see koolsid already posted the solution.
Re: [RESOLVED] Limiting textbox characters
Im going to use both of them