can't see backspace as chr string ?

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

trevorC via AccessMonster.com

Can you help ?

I'm trying to limit the user input to HEX only values, this works ok but they
can't use backspace to undo an incorrect entry if they type in the wrong
value.

How can i resolve this issue and keep the BACKSPACE to undo as required.

Dim strChar As String
strChar = UCase$(Chr$(KeyAscii))
If (strChar >= "0" And strChar <= "9") Or (strChar >= "A" And_
strChar <= "F") Or (strChar = "n") Or (strChar = "\") Or_
(strChar = "/") Or (strChar = "{BS}") Then
'do nothing
Else
' don't accept key input
KeyAscii = 0 ' cancel keystroke
End If
 
L

Linq Adams via AccessMonster.com

Actually, for this kind of thing, you have to include a number of non-
character keys, if you want your users to be able to navigate within the
textbox to correct mistakes and to navigate out of the textbox without using
the mouse. Besides the Backspace key, you also need to include the Delete,
Left Arrow, Right Arrow, Tab and Enter keys. You can do this by including
their KeyCodes in the "allowed" keys in the If...part of your code with code
like this:

Or (Keycode = 8) Or (Keycode = 46)...etc.

Keycodes:

Backspace 8
Delete 46
Left Arrow 37
Right Arrow 39
Tab 9
Enter 13
 
P

Peter Hibbs

Trevor,

I think you are making this too complicated. Try this :-


Private Sub TextBox_KeyPress(KeyAscii As Integer)

KeyAscii = Asc(UCase(Chr(KeyAscii)))
If Chr(KeyAscii) Like "[!0-9A-F]" And KeyAscii <> vbKeyBack Then
KeyAscii = 0
End If

End Sub

where TextBox is the name of your Text box field.

HTH

Peter Hibbs.
 

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