Inkey function in Visual Basic 6

I need to check a value to make sure that there are no spaces or special characters in it. Alpha numeric characters are valid. But not any other characters like space, *, %, etc.
HariniMadugulaAsked:
Who is Participating?
 
JackOfPHCommented:
Private Sub text1_KeyPress(KeyAscii As Integer)
Select case Keyascii
case 48 to 57
msgbox ("text is valid")
case else
msgbox("text not valid")
end select
end sub
0
 
dmitryz6Commented:
Better to put function to check character on Key Press Event

Private Sub text1_KeyPress(KeyAscii As Integer)
    Dim iNumbers As Integer
    Dim sMsg As String
    iNumbers = KeyAscii


    If ((iNumbers < 48 Or iNumbers > 57) And (iNumbers < 65 Or iNumbers > 90) and (iNumbers < 97 Or iNumbers > 122)) Then
        sMsg = MsgBox("Could Not")
        KeyAscii = 0
    End If
End Sub
0
 
leclairmCommented:
Don't forget about backspace:

    If ((iNumbers < 48 Or iNumbers > 57) And (iNumbers < 65 Or iNumbers > 90) And (iNumbers < 97 Or iNumbers > 122) And iNumbers <> 8 ) Then

......
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
leclairmCommented:
Actually, better to use a case like JackOfPH did:

Select Case KeyAscii
    Case 8, 48 To 57, 65 To 90, 97 To 122
        'valid
    Case Else
        MsgBox ("Invalid")
End Select
0
 
dmitryz6Commented:
Mike what the different
"better to use a case like JackOfPH did"
0
 
leclairmCommented:
Much clearer and easier to read /maintain if valid keys are to be added or removed from validity check, that's all.
0
 
dmitryz6Commented:
Sorry,Not differents.It depend what from personal choice
0
 
leclairmCommented:
Didn't say it was different, just clearer.  Both will give the same result, but compare your if to the case I put and tell me which one is easier to read.
0
 
dmitryz6Commented:
It just discussion.
Personaly I like If Statement more,but it again depent of personal choice.

Regards
 Dmitry
0
 
leclairmCommented:
Agreed.  I guess it does boil down to personal choice or style of programming that your used to.
0
 
leclairmCommented:
The answer you accepted only accepts 0-9.  Is that what you wanted??
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.