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]