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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validation

Status
Not open for further replies.

technohead

Technical User
Jan 12, 2002
64
0
0
US
Hi,
i am using the code below to validate a textbox, however if i hit the return key it brings up the msgbox " invalid entry".
i was wondering is there a way of allowing to press the return key,

thanks



If KeyAscii < 8 Or KeyAscii > 8 Then
If KeyAscii < Asc(&quot;'&quot;) Or KeyAscii > Asc(&quot;'&quot;) Then
If KeyAscii < Asc(&quot;-&quot;) Or KeyAscii > Asc(&quot;-&quot;) Then
If KeyAscii < Asc(&quot;A&quot;) Or KeyAscii > Asc(&quot;Z&quot;) Then
If KeyAscii < Asc(&quot;a&quot;) Or KeyAscii > Asc(&quot;z&quot;) Then
If KeyAscii < Asc(&quot; &quot;) Or KeyAscii > Asc(&quot; &quot;) Then
KeyAscii = 0 ' Cancel the character.
Beep ' Sound error signal.

With txtfirstname
.SelStart = 0 'Select the current entry
.SelLength = Len(.Text)
End With
End If
If KeyAscii = 0 Then
answer = MsgBox(&quot;Invalid entry&quot;)
End If
End If
End If
End If
End If
End If
 
Here is a start:

Public Function CustomKeys(KeyAscii As Integer) As Integer
Select Case KeyAscii
Case 8, 13, 95, 65 To 90, 97 To 122, 48 To 57
Case Else
KeyAscii = 0
End Select
CustomKeys = KeyAscii
End Function

Then call it in the text keypress event:

KeyAscii = CustomKeys(KeyAscii)
 
is this just for the return problem or for everything above, it still does'nt work if i put this in.
 
Public Function CustomKeys(KeyAscii As Integer) As Integer
Select Case KeyAscii
Case 8, 32, 39, 45, 65 To 90, 97 To 122,
Case Else
KeyAscii = 0
End Select
CustomKeys = KeyAscii
End Function

Then call it in the text keypress event:

KeyAscii = CustomKeys(KeyAscii)

This should take care of all of the cases stated above.
 
still the same, it will tab fine but nothing when i hit the return key
 
Add the number 13 to what I gave you.
 
The decimal representation for a carriage return is 13 and that is what is created when you press Enter (Return on the keyboard). I created a text box on a form and set it's multiline property to true for testing purposes and it allow me to hit Enter. Your code should look like this.

Public Function CustomKeys(KeyAscii As Integer) As Integer
Select Case KeyAscii
Case 8, 13, 32, 39, 45, 65 To 90, 97 To 122,
Case Else
KeyAscii = 0
End Select
CustomKeys = KeyAscii
End Function

Then call it in the text keypress event:

Private Sub Text1_KeyPress(KeyAscii As Integer)
KeyAscii = CustomKeys(KeyAscii)
End Sub
 
yep thats exactly what i have and it works for the carriage return but i want to move onto the next text box. is that possible
 
Usually you press tab or click the mouse to move to the next textbox. That is why VB allows you to set a tab index in the properties window if you don't have a mouse.
 
Yeh i thought that, but was just wondering if you could do that as well.

no prob, thanks a million for your help
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top