How can i detect if the user has an upper-case letter in the textbox when they hit ok?
Printable View
How can i detect if the user has an upper-case letter in the textbox when they hit ok?
Text1 cant have numbers or it will say there is UpperCase even if it isnt.Code:Private Sub Command1_Click()
Dim IsIt As Boolean
For i = 1 To Len(Text1)
If Asc(Mid(Text1, i, 1)) = Asc(UCase(Mid(Text1, i, 1))) Then
IsIt = True
Else
IsIt = False
End If
Next
If IsIt = True Then MsgBox "uppercase"
End Sub
Use keypress event of the textbox and get the ASCII value of the letter currently entered.
I think he meant this:
Replace bla with the value of the Ascii and blabla with the command you want to give him.Code:If KeyAscii = bla then blabla
To view the Ascii Value list go to the Help Topics on VB and see under Character Sets.
Then go to See Also and go to the other Character Sets to see part 2 of the list.
I hope I helped.
I hope this helps.Code:Private Sub Text1_KeyPress(KeyAscii As Integer)
If Chr(KeyAscii) = UCase(Chr(KeyAscii)) Then MsgBox "Uppercase detected!"
End Sub
You could also use the Like Statement with the "*[A-Z]*" Pattern, i.e.Code:Private Sub Command1_Click()
Caption = IIf(Text1 Like "*[A-Z]*", "Contains Uppercase Chars", "No Uppercase Chars.")
End Sub
I would rather use a Select Case or If...Then, than a IIf statement, it is less code, but I heard IIf is a lot slower than a normal If...Then.
Aaron's example should be fast enough but this could be even faster:
Code:If text1<>lcase(text1) then msgbox "Contains upper case"