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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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

......
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook 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
dmitryz6Commented:
Mike what the different
"better to use a case like JackOfPH did"
leclairmCommented:
Much clearer and easier to read /maintain if valid keys are to be added or removed from validity check, that's all.
dmitryz6Commented:
Sorry,Not differents.It depend what from personal choice
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.
dmitryz6Commented:
It just discussion.
Personaly I like If Statement more,but it again depent of personal choice.

Regards
 Dmitry
leclairmCommented:
Agreed.  I guess it does boil down to personal choice or style of programming that your used to.
leclairmCommented:
The answer you accepted only accepts 0-9.  Is that what you wanted??
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.