to remove all except hex characters and then validate

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

twas via AccessMonster.com

This seems like it should be easy, but...

I need to remove all but valid hexidecimal characters (0-9.A-F) from a text
field and then validate that after all extranous characters were removed,
there were exactly 12 characters left that were stored in the field. So if
someone put in 01.23.45.67.89.AB or 01-23-45-67-89-AB the result would be
0123456789AB and valid, but if someone entered 01-23-45-67- the result would
be invalid, not the right number of characters (even though they put in 12
characters initially)

thanks
Twas
 
D

Douglas J. Steele

Untested aircode:

Function StripCharacters(InputString As String) As String
Dim intLoop As Integer
Dim strCurrChar As String
Dim strOutput As String

strOutput = vbNullString
For intLoop = 1 to Len(InputString)
strCurrChar = UCase$(Mid$(InputString, intLoop, 1))
If (strCurrChar >= "0" And strCurrChar <= "9") _
Or (strCurrChar >= "A" And strCurrChar <= "F") Then
strOutput = strOutput & strCurrChar
End If
Next intLoop

StripCharacters = strOutput

End

If your intent is to chnage what they've entered in a text box, put code in
the text box's BeforeUpdate event:

Private Sub MyTextBox_BeforeUpdate(Cancel As Integer)
Dim strCorrected As String

strCorrected = StripCharacters(Me.MyTextBox)
If Len(strCorrected) = 12 Then
Me.MyTextBox = strCorrected
Else
MsgBox Me.MyTextBox & " is invalid."
Cancel = True
End If

End Sub
 
T

twas via AccessMonster.com

I am clearly doing something wrong. I tried

Public Function OnlyHexCharacters(InputString As String) As String
Dim intLoop As Integer
Dim strCurrChar As String
Dim strOutput As String
strOutput = vbNullString
For intLoop = 1 To Len(InputString)
strCurrChar = UCase$(Mid$(InputString, intLoop, 1))
If (strCurrChar >= "0" And strCurrChar <= "9") _
Or (strCurrChar >= "A" And strCurrChar <= "F") Then
strOutput = strOutput & strCurrChar
End If
Next intLoop
OnlyHexCharacters = strOutput
End Function

Private Sub atMACa_BeforeUpdate(Cancel As Integer)
Dim strCorrected As String
Dim strInput As String
Dim iAns As Integer
Const cSize = 12 ' how many characters in a MAC address
strInput = atMACa.Value
strCorrected = OnlyHexCharacters(strInput)
If Len(strCorrected) = cSize Then
atMACa.Value = strCorrected
ElseIf Len(strCorrected) < cSize Then
iAns = MsgBox(atMACa.Value & _
" is too short; MAC values should have " & cSize & " hex digits.", _
vbOKCancel + vbCritical)
If iAns = vbCancel Then
Cancel = True
End If 'iAns = vbCancel
Else ' length over limit
iAns = MsgBox(atMACa.Value & _
" is too long; MAC values should have only " & cSize & " hex digits.",
_
vbOKCancel)
atMACa.Value = Left$(atMACa.Value, cSize)

End If ' length
Cancel = False

End Sub

-- with atMACa a text box bound to a variable of length 12. However, when I
try to use it, first it doesn't let me type more than 12 characters into the
text box (I was expecting to type something like 12.34.56.78.9a.bc and get
something like 123456789ABC in the field) but:
Access won't let me type over 12 characters in the text field
I get an error 2115 indicating that my code is preventing Access from
saving the record.
Help !
thanks
Twas
Untested aircode:

Function StripCharacters(InputString As String) As String
Dim intLoop As Integer
Dim strCurrChar As String
Dim strOutput As String

strOutput = vbNullString
For intLoop = 1 to Len(InputString)
strCurrChar = UCase$(Mid$(InputString, intLoop, 1))
If (strCurrChar >= "0" And strCurrChar <= "9") _
Or (strCurrChar >= "A" And strCurrChar <= "F") Then
strOutput = strOutput & strCurrChar
End If
Next intLoop

StripCharacters = strOutput

End

If your intent is to chnage what they've entered in a text box, put code in
the text box's BeforeUpdate event:

Private Sub MyTextBox_BeforeUpdate(Cancel As Integer)
Dim strCorrected As String

strCorrected = StripCharacters(Me.MyTextBox)
If Len(strCorrected) = 12 Then
Me.MyTextBox = strCorrected
Else
MsgBox Me.MyTextBox & " is invalid."
Cancel = True
End If

End Sub
This seems like it should be easy, but...
[quoted text clipped - 10 lines]
thanks
Twas
 
T

TonyT

You are being way too complicated for what you are trying to acheive -
Douglas' function allowed unlimited characters that would then strip out all
unwanted characters (btw you would need another or line in his function to
accept letters "a" to "f" in lowercase) leaving you with a *stripped* out
string value.

the second piece of his code does *alot* more simply what you are
redundantly trying to acheive with your before update code, you just have to
change the Else part to ;
ElseIf Len(strCorrected) > 12 then
msgbox "too many characters" '<<enter your choice of message
ElseIf Len(strCorrected) < 12 then
msgbox "too few characters"
End If
to separate too many or too few characters, rather than his version, which
said 'wrong length'. Apart from that I don't see your code trying to add any
other functionality to his reply.

Hope this helps clarify it a bit more.

TonyT..

twas via AccessMonster.com said:
I am clearly doing something wrong. I tried

Public Function OnlyHexCharacters(InputString As String) As String
Dim intLoop As Integer
Dim strCurrChar As String
Dim strOutput As String
strOutput = vbNullString
For intLoop = 1 To Len(InputString)
strCurrChar = UCase$(Mid$(InputString, intLoop, 1))
If (strCurrChar >= "0" And strCurrChar <= "9") _
Or (strCurrChar >= "A" And strCurrChar <= "F") Then
strOutput = strOutput & strCurrChar
End If
Next intLoop
OnlyHexCharacters = strOutput
End Function

Private Sub atMACa_BeforeUpdate(Cancel As Integer)
Dim strCorrected As String
Dim strInput As String
Dim iAns As Integer
Const cSize = 12 ' how many characters in a MAC address
strInput = atMACa.Value
strCorrected = OnlyHexCharacters(strInput)
If Len(strCorrected) = cSize Then
atMACa.Value = strCorrected
ElseIf Len(strCorrected) < cSize Then
iAns = MsgBox(atMACa.Value & _
" is too short; MAC values should have " & cSize & " hex digits.", _
vbOKCancel + vbCritical)
If iAns = vbCancel Then
Cancel = True
End If 'iAns = vbCancel
Else ' length over limit
iAns = MsgBox(atMACa.Value & _
" is too long; MAC values should have only " & cSize & " hex digits.",
_
vbOKCancel)
atMACa.Value = Left$(atMACa.Value, cSize)

End If ' length
Cancel = False

End Sub

-- with atMACa a text box bound to a variable of length 12. However, when I
try to use it, first it doesn't let me type more than 12 characters into the
text box (I was expecting to type something like 12.34.56.78.9a.bc and get
something like 123456789ABC in the field) but:
Access won't let me type over 12 characters in the text field
I get an error 2115 indicating that my code is preventing Access from
saving the record.
Help !
thanks
Twas
Untested aircode:

Function StripCharacters(InputString As String) As String
Dim intLoop As Integer
Dim strCurrChar As String
Dim strOutput As String

strOutput = vbNullString
For intLoop = 1 to Len(InputString)
strCurrChar = UCase$(Mid$(InputString, intLoop, 1))
If (strCurrChar >= "0" And strCurrChar <= "9") _
Or (strCurrChar >= "A" And strCurrChar <= "F") Then
strOutput = strOutput & strCurrChar
End If
Next intLoop

StripCharacters = strOutput

End

If your intent is to chnage what they've entered in a text box, put code in
the text box's BeforeUpdate event:

Private Sub MyTextBox_BeforeUpdate(Cancel As Integer)
Dim strCorrected As String

strCorrected = StripCharacters(Me.MyTextBox)
If Len(strCorrected) = 12 Then
Me.MyTextBox = strCorrected
Else
MsgBox Me.MyTextBox & " is invalid."
Cancel = True
End If

End Sub
This seems like it should be easy, but...
[quoted text clipped - 10 lines]
thanks
Twas
 
D

Douglas J. Steele

TonyT said:
(btw you would need another or line in his function to
accept letters "a" to "f" in lowercase)

No, you don't, since I'm converting to upper case:

strCurrChar = UCase$(Mid$(InputString, intLoop, 1))
 
T

twas via AccessMonster.com

But the real problem is that the approach doesn't seem to work - I get error
2115 instead of updated records, and the input limits at 12 characters typed,
so if the user types periods or dashes every two characters, their input gets
truncated early, and the procedure fails.

I've been thinking that instead of filtering out the characters after entry,
I should filter them as they are typed (maybe in the KeyUp event?). I could
then check in the BeforeUpdate event to see if there were exactly 12
characters and give the user a message if the count is wrong. I'm not sure
that this approach would work if the user pasted in the characters rather
than typing them.; haven't found any references to what happens in such a
case.

The only option would seem to be having the user type into an unbound text
box, running the routine, and then storing the result back to the right.
Seems messy, but might be best.


thanks
Twas
 
T

twas via AccessMonster.com

Both, I think; other than getting overly complicated about what to do if the
length of the responses were incorrect, I was essentiallly trying to
implement your suggestion. Your routine to filter out the non-hex characters
worked fine, but I couldn't make the overall solution work in context. Most
of the differences were introduced only because the original question and
answer were general, and my solution was specific to my particular problem.

thanks
Twas
WHICH approach doesn't work: yours, or the one I proposed?
But the real problem is that the approach doesn't seem to work - I get
error
[quoted text clipped - 28 lines]
 

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