stephenlecompte 07-20-2003, 08:20 AM I have a text boxes in an array (1) that I would like to make sure that the user only types in letters and a text box in an array(2) that only types in numbers. The text boxes for Access were so helpful in that regard! What command do I need to place in the following:
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case Index
Case 1
'
'
' place code here?
End Select
Case 2
'
'
'
'place doe here?
End Select
Even if someone knows the command for a regular text box is appreciated. Thanks,
Stephen
Blackfire 07-20-2003, 08:30 AM The IsNumeric() function will tell you if whatever you pass to it is a number. There are similar functions for dates and booleans etc. but i don't think there is one for strings, unfortunately.
PWNettle 07-20-2003, 11:19 AM This topic has been covered several times, so you might want to search the forum for really good examples - or check the Code Library and Tutor's Corner forums here.
Here's some simple examples:
For a numeric only textbox, you might do something like this:
Private Sub Text1_KeyPress(KeyAscii As Integer)
' 48 is ASCII for zero.
' 57 is ASCII for nine.
If KeyAscii < 48 Or KeyAscii > 57 Then
KeyAscii = 0
End If
End Sub
The simple version above only permits integers - it does't check for the decimal point. The one below allows decimals to be entered:
Private Sub Text1_KeyPress(KeyAscii As Integer)
' 48 is ASCII for zero.
' 57 is ASCII for nine.
' 46 is ASCII for the period character.
If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 46 Then
KeyAscii = 0
End If
End Sub
Checking for character input only is done similarly, with the wrinkle that you most likely want to allow upper and lower cased letters - so you have a slightly more complex conditional:
' 65 is ASCII for 'A'
' 90 is ASCII for 'Z'
' 97 is ASCII for 'a'
' 122 is ASCII for 'z'
If (KeyAscii < 65 Or KeyAscii > 90) And (KeyAscii < 97 Or KeyAscii > 122) Then
KeyAscii = 0
End If
If you need to support both types of validations in a control array then incorporate the Index from the control array's KeyPress event into a conditional to seperate and use both validations in the same routine (just like you have in your original post).
For developing these kinds of routines you can either find an ASCII value chart (in MSDN) or, if you're lazy like me, you can just bring up the immediate window (press ctrl+g in VB IDE) and type stuff like:
?asc("a"), asc("z"), asc("A"), asc"Z")
ASC() is a function that returns the ASCII value for a character. (conversely you can use Chr$() to get the character for an ASCII number). The immediate window will can evaluate simple expressions or some functions. '?' is short for print. So if you type this:
? "Hello Stephen" (and press enter key)
...in the immediate window it'll just echo back 'Hello Stephen' at you. And if you type:
?12345 / 15 (and press enter)
...in the immediate window it'll evaluate the expression and display '823'.
The immediate window is good for a variety of things - including finding ASCII values for characters and a cheap calculator. ;)
Incidentally, if you break into your program while it's running you can also display the values for variables that are in scope, call subroutines from your project, and other nifty things in the immediate window - handy for debugging.
But enough of my rambling!
Cheers,
Paul
stephenlecompte 08-09-2003, 06:16 PM When I used to program on my Tandy ,Color Computer 3 - someone wrote an interesting code based on ASCII values that figured out the large print of a small alphabetic character and vice versa.
If you look at the ASCII value list - the same amount of characters between A and a is the same amount of characters between B and b, C and c, etc. The only thing is that I know that ASC returns the value of a letter - right? What command would do the contrast of ASC - return the value of ASCII into a character? If I knew the command - I would think that the above algorithim would be perfect for finding capital letters for smaller characters and vice versa.
By the way - my brain is fried. If anybody would like to post the code to figure out the ASCII values - feel free.
Thanks,
Stephen
Press F2, type KeyCodes. List of all of the vbKeyCodes, click on each one to find its Ascii value.
blindwig 08-09-2003, 06:34 PM When I used to program on my Tandy ,Color Computer 3 - someone wrote an interesting code based on ASCII values that figured out the large print of a small alphabetic character and vice versa.
If you look at the ASCII value list - the same amount of characters between A and a is the same amount of characters between B and b, C and c, etc. The only thing is that I know that ASC returns the value of a letter - right? What command would do the contrast of ASC - return the value of ASCII into a character? If I knew the command - I would think that the above algorithim would be perfect for finding capital letters for smaller characters and vice versa.
By the way - my brain is fried. If anybody would like to post the code to figure out the ASCII values - feel free.
Thanks,
Stephen
Look at the ASC() and CHR$() functions in your MSDN - That should help you figure it out.
BinaryMayhem 08-09-2003, 06:40 PM ' letter!
Private Sub Text1_KeyPress(Index As Integer, KeyAscii As Integer)
If Not (UCase(Chr$(KeyAscii))) >= "A" And (UCase(Chr$(KeyAscii))) <= "Z" Then
KeyAscii = 0
End If
End Sub
'number!
Private Sub Text1_KeyPress(Index As Integer, KeyAscii As Integer)
If Not (Chr$(KeyAscii) >= "0" And Chr$(KeyAscii) <= "9") Then
KeyAscii = 0
End If
End Sub
For the number you can use:
Public Declare Function SetWindowLong Lib "user32.dll" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Declare Function GetWindowLong Lib "user32.dll" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Public Const GWL_STYLE As Long = (-16)
Public Const ES_NUMBER As Long = &H2000&
SetWindowLong .hwnd, GWL_STYLE, GetWindowLong(.hwnd, GWL_STYLE) Or ES_NUMBER
But for text you'll have to use BinaryMayhem's code
stephenlecompte 08-15-2003, 04:01 PM The following code works for me. USER would be the variable that I would contain any small letters .
Private Sub small_letters()
For I = 1 To Len(USER): SEC = Asc(Mid$(USER, I, 1))
If SEC < 65 Or SEC > 90 Then Mid$(USER, I, 1) = Chr$(SEC - 32)
Next I
End Sub
You can just use the LCase command
Thinker 08-15-2003, 04:22 PM Private Sub small_letters()
For I = 1 To Len(USER): SEC = Asc(Mid$(USER, I, 1))
If SEC < 65 Or SEC > 90 Then Mid$(USER, I, 1) = Chr$(SEC - 32)
Next I
End Sub
This is really, really bad code. All this does is check if a character is
uppercase, and if not, it changes it into a character that is 32
below it in ASCII value. For instance, that means that the number 0
becomes a Ctrl P (Chr$(16)). Sure, this will turn lowercase letters into
uppercase, but the UCase() function does it much, much easier and
better.
stephenlecompte 08-16-2003, 04:26 PM Private Sub small_letters()
For I = 1 To Len(USER): SEC = Mid$(USER, I, 1)
If (SEC >= "a" And SEC <= "z") Then Mid$(USER, I, 1) = Chr$(Asc(SEC) - 32)
Next I
End Sub
I still think this is better than UCASE
MikeJ 08-16-2003, 04:33 PM It really isn't. Microsoft provided UCase$ for this purpose, so it's a waste of time and effort to do it this way.
:-\
~MikeJ
Thinker 08-16-2003, 04:56 PM I don't expect you to believe it, but I have to point it out for others who
might see this thread later, it absolutely is not better. You are adding in
completely needless looping and you are making no allowances for any
characters other than English lower case (ASCII codes above 127).
stephenlecompte 08-17-2003, 05:56 PM I have code in my program to make sure every text box only displays capital letters! But where I run into a problem is with the inputbox. How can I ensure the inputbox only displays capital letters as well?
You would probably have to subclass the InputBox, and it would probably be easier for you to create your own InputBox form than to deal with that.
If you still want to go forward with the subclassing part: http://www.elitevb.com/content/01,0062,01/
Csharp 08-17-2003, 06:21 PM i guess you could always do this, to only allow upper case in an inputbox ...
Private Sub Command1_Click()
Dim strString As String
strString = InputBox("do stuff!")
If Not strString = LCase(strString) Then
MsgBox "it's caps"
'/// do stuff
Else
MsgBox "it's lower case"
Exit Sub
End If
End Sub
|