Stripping Non Ascii (1 Viewer)

alexfwalker81

Member
Local time
Yesterday, 22:27
Joined
Feb 26, 2016
Messages
93
I'm using the code below to strip certain non ascii characters in a query. It works nicely, apart from where it comes across a single quote. I understand why it's breaking, but I don't understand how I'd alter the function below to cope with single quotes in the fields that it's looking at;

Code:
Public Function fReplaceNonAscii(ByVal strInput As Variant) As Variant
Dim strSQL As String
strInput = strInput & vbNullString
If Len(strInput) > 0 Then
    strSQL = "SELECT T.* FROM tbl_ascii AS T " & _
            "WHERE INSTR(1,'" & strInput & "', T.[NonAscii]) > 0"
    With CurrentDb.OpenRecordset(strSQL, _
            dbOpenSnapshot, dbReadOnly)
        If Not (.BOF And .EOF) Then
            .MoveFirst
        End If
        Do Until .EOF
            strInput = Replace(strInput, !NonAscii, !Ascii)
            .MoveNext
        Loop
    End With
    
End If
fReplaceNonAscii = strInput
End Function
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 01:27
Joined
May 21, 2018
Messages
8,525
I am assuming it breaks in the where. Is that the problem. I would first replace any ' with double singles.
strInput = strInput & vbNullString
strInput = replace(strInput,"'","''")
 

alexfwalker81

Member
Local time
Yesterday, 22:27
Joined
Feb 26, 2016
Messages
93
I am assuming it breaks in the where. Is that the problem. I would first replace any ' with double singles.
strInput = strInput & vbNullString
strInput = replace(strInput,"'","''")
Works like a charm - thank you!
 

Users who are viewing this thread

Top Bottom