VB code

  • Thread starter Thread starter TRob
  • Start date Start date
T

TRob

Access '97
I have a form with 9 fields that needs to be checked for
invalid characters before the record can be saved. I've
been using the "If..Then" to return a message box if one
of the characters is found in any one of the 9 fields. It
is working fine, but I have just been given an additional
19 characters to check for.

Is there an easy way to add the additional 19 characters I
want to check for? Here is a sample of the original code
I have set for each of the 9 fields:
-------------------------------------------
If Me.DeptCode.Value Like "*,*" Then
MsgBox "The Department Code contains a comma." _
& Chr(10) & "Please remove the comma and save the PO
again", vbCritical, "INVALID CHARACTER ALERT"
End
End If
 
Good evening

Here's a function I wrote some time ago that will do the trick.

Hope this helps.



Best Regards

Maurice St-Cyr
Micro Systems Consultants, Inc.


Function StripString(MyStr As Variant) As Variant
' Print StripString("Test s t ring.#,-,&,/,*")
'The invalid characters (".#,-") are successfully stripped from the test
'string.


On Error GoTo StripStringError

Dim strChar As String, strHoldString As String
Dim i As Integer
' Exit if the passed value is null.
If IsNull(MyStr) Then Exit Function
' Exit if the passed value is not a string.
If VarType(MyStr) <> 8 Then Exit Function
' Check each value for invalid characters.
For i = 1 To Len(MyStr)
strChar = Mid$(MyStr, i, 1)
Select Case strChar
Case ".", "!", "%", "#", ",", "-", "&", "/", "*", " ", "@", "$",
"^", "(", ")", ">", "<"
' Do nothing
'strHoldString = strHoldString & Chr(32)
Case Else
strHoldString = strHoldString & strChar
End Select
Next i

' Pass back corrected string.
StripString = strHoldString

StripStringEnd:
Exit Function

StripStringError:
MsgBox Error$
Resume StripStringEnd
End Function
 
Back
Top