Question

I am trying to validate entry of a form field using the following VBA. This works very well. My problem is this Access app creates a variety of XML data files, and I don't want certain characters within that xml....namely soft-returns (Shift+Enter). I believe that the Chr for this is Chr(11), but I don't think I can just add , Chr(11)) to the end of the array below and this will work...how can I use Chr(#) in link manner?

 Dim i, j As Integer
 dim myField as variant

 varNo = Array("\", "/", ":", "*", "?", """", "<", ">", "|")

 If IsNull(Me.FieldValue) = False Then
    myField = Me.FieldValue 
    For i = 0 To UBound(varNo)
    j = InStr(1, myField , varNo(i))
    If j > 0 Then
        MsgBox "Cannot use character:" & Chr(13) & Chr(10) & Chr(13)
        & Chr(10) & varNo(i), vbCritical, " Illegal Character"

        Exit Sub
        Exit For
    End If
    Next
 End If

again the above works great for those things in the array, but I would like to include Chr() as well.

Was it helpful?

Solution

You can build your array with an empty string as the new last element, then change the value of the last element to Chr(11).

varNo = Array("\", "/", ":", "*", "?", """", "<", ">", "|", "")
varNo(UBound(varNo)) = Chr(11)

Actually, I'm unsure why that should be necessary. This works for me ...

varNo = Array("\", "/", ":", "*", "?", """", "<", ">", "|", Chr(11))

Based on the comments, I think it will be useful to confirm the text you're evaluating actually contains the characters you expect. Feed the text to this procedure and examine its output in the Immediate window.

Public Sub AsciiValues(ByVal pInput As String)
    Dim i As Long
    Dim lngSize As Long
    lngSize = Len(pInput)
    For i = 1 To lngSize
        Debug.Print i, Mid(pInput, i, 1), Asc(Mid(pInput, i, 1))
    Next
End Sub

OTHER TIPS

Here are a few notes on another approach:

'' Microsoft VBScript Regular Expressions library
'' or CreateObject ("VBScript.RegExp")

Dim rex As New RegExp

With rex
   .MultiLine = False
   .Global = True
   .IgnoreCase = False
End With

'A line for testing
sLine = "abc" & Chr(11)

'Anything that is NOT a-zA-Z0-9 will be matched
rex.Pattern = "[^a-zA-Z0-9]"
If rex.Test(sLine) Then
    Debug.Print "Problem: include only alpha numeric"
End If
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top