?
Solved

Inkey function in Visual Basic 6

Posted on 2006-04-03
11
Medium Priority
?
564 Views
Last Modified: 2013-12-25
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.
0
Comment
Question by:HariniMadugula
  • 6
  • 4
11 Comments
 
LVL 19

Expert Comment

by:dmitryz6
ID: 16365817
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
 
LVL 15

Accepted Solution

by:
JackOfPH earned 1500 total points
ID: 16367497
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
 
LVL 11

Expert Comment

by:leclairm
ID: 16369846
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Expert Comment

by:leclairm
ID: 16369860
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
 
LVL 19

Expert Comment

by:dmitryz6
ID: 16369948
Mike what the different
"better to use a case like JackOfPH did"
0
 
LVL 11

Expert Comment

by:leclairm
ID: 16369962
Much clearer and easier to read /maintain if valid keys are to be added or removed from validity check, that's all.
0
 
LVL 19

Expert Comment

by:dmitryz6
ID: 16369994
Sorry,Not differents.It depend what from personal choice
0
 
LVL 11

Expert Comment

by:leclairm
ID: 16370006
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
 
LVL 19

Expert Comment

by:dmitryz6
ID: 16370131
It just discussion.
Personaly I like If Statement more,but it again depent of personal choice.

Regards
 Dmitry
0
 
LVL 11

Expert Comment

by:leclairm
ID: 16370165
Agreed.  I guess it does boil down to personal choice or style of programming that your used to.
0
 
LVL 11

Expert Comment

by:leclairm
ID: 16371167
The answer you accepted only accepts 0-9.  Is that what you wanted??
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month14 days, 11 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question