cancel all but hex characters

  • Thread starter twas via AccessMonster.com
  • Start date
T

twas via AccessMonster.com

How do I drop all but hex characters (0-9, A-F) from a text box while
allowing use of copy paste, and delete keys? I was thinking that a routine
like
Private Sub txtControl_KeyPress(KeyAscii As Integer)
dim strChar as string
strChar = UCase$(Chr$(KeyAscii))
If (strChar >= "0" And strChar <= "9") _
Or (strChar >= "A" And strChar <= "F") then
' good hex character
Else
KeyAscii = 0 ' cancel keystroke
End If
End Sub

might work, but how do I:
1) convert all the input characters to upper case
2) make sure that the user pasting a string into the control works
3) warn the user if the length of the resultant entry in the text box is
incorrect (a validation rule would force compliance, but that might
compromise interface usability)

thanks
 
T

TonyT

twas via AccessMonster.com said:
How do I drop all but hex characters (0-9, A-F) from a text box while
allowing use of copy paste, and delete keys? I was thinking that a routine
like
Private Sub txtControl_KeyPress(KeyAscii As Integer)
dim strChar as string
strChar = KeyAscii strChar = UCase(strChar)
If (strChar >= "0" And strChar <= "9") _
Or (strChar >= "A" And strChar <= "F") then
' good hex character
Else
KeyAscii = 0 ' cancel keystroke
End If
End Sub

might work, but how do I:
1) convert all the input characters to upper case

see code changed above
2) make sure that the user pasting a string into the control works

either don't let them paste, or, you will need to set up a loop if/then or
probably better select case routine to iterate thru the characters 1 by 1.
AND the keypress events are NOT fired by right mouse click paste options. So
I would move all code to On_Change event, the added bonus is that you also
have access to the oldvalue as well as the newvalue entered in code.
3) warn the user if the length of the resultant entry in the text box is
incorrect (a validation rule would force compliance, but that might
compromise interface usability)

if Len(strChar) > 12 Then
msgbox"Too many characters entered"
End if

TonyT..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top