I am designing a form for a data entry function where two fields that are entered are hex (8characters). I want to edit this field for valid hex numbers. How do I do this?
Editing an 8 Character Hexadecimal Field in a Form
Collapse
X
-
Hi. There are no built-in facilities for entering and validating hexadecimal values into textboxes. You will need to validate what the user entered yourself using the BeforeUpdate events of the controls concerned to apply validation.
If you are entering each value as a four-character hex value you will need to:
1. ensure that four characters have been entered
2. ensure that the characters are either digits 0..9 or letters A..F
Here's an example of validating a four-character Hex string to get you going, using the BeforeUpdate event of a textbox called Text0 in this case:
Code:Private Sub Text0_BeforeUpdate(Cancel As Integer) If fInvalidHex(Me.Text0) Then Cancel = True MsgBox "Please re-enter as four-character hex address" End If End Sub Public Function fInvalidHex(ByRef ControlValue As Variant) As Boolean Dim blResult As Boolean Dim lngChrCount As Long Dim lngCounter As Long Dim strCh As String blResult = False If IsNull(ControlValue) Then fInvalidHex = True ElseIf Len(ControlValue) <> 4 Then fInvalidHex = True Else For lngCounter = 1 To 4 strCh = UCase(Mid(ControlValue, lngCounter, 1)) Select Case strCh Case "0" To "9", "A" To "F" 'do nothing Case Else blResult = True End Select Next fInvalidHex = blResult End If End Function
-Stewart -
I took a totally different approach on this Thread. Since you indicated that this Text Box will be used for Data Entry, then each and every Character entered into the Text Box should be Validated to ensure that it is in the Range (0 to 9, A - F). If the Character is outside this Range, it is then simply not allowed to be entered, and is technically stripped while leaving the remainder of the Valid String in tact. Fortunately, Access has an Event which can be used specifically for this type of functionality, the Change() Event. The following Code will allow only the Characters 0,1,2,3,4,5,6,7 ,8,9,A,B,C,D,E, F to be entered into the Text Box, while stripping invalid ones. The Arrow Keys, and Backspace will not effect the outcome, but you may wish to check other Keystrokes on your own.
Code:Private Sub txtHex_Change() Dim tHex As TextBox Dim intCtr As Integer Set tHex = Me![txtHex] If Len(tHex.Text) = 0 Then Exit Sub Else For intCtr = 1 To Len(tHex.Text) Select Case Asc(Mid$(tHex.Text, intCtr, 1)) Case 48 To 57 'Valid, 0 to 9 Case 65 To 70 'Valid, A to F Case Else 'Invalid, strip Character then move 'Cursor to end of string tHex.Text = Left$(tHex.Text, Len(tHex.Text) - 1) tHex.SelStart = Len(tHex.Text) End Select Next End If End Sub
Code:Private Sub txtHex_KeyPress(KeyAscii As Integer) If KeyAscii = 32 Then KeyAscii = 0 End Sub
Comment
Comment