-
1
- #1
Here are two functions to help with Single Quotes.
The problems are caused by the character with ascii 39, and there is another character that looks very like it, ascii 96 which causes no problems.
FixApostrophe replaces all the char 39 with char 96.
QorN is for preparing strings to be put in SQL Statements (but not when they feed parameters on stored procs)
Public Function QOrN(xstr As Variant) As String
If xstr = "" Or IsNull(xstr) Then
QOrN = "Null"
Exit Function
Else
xstr = FixApostrophe(xstr)
QOrN = "'" & xstr & "'"
End If
End Function
Public Function FixApostrophe(xstr As Variant) As String
Dim posint As Integer
Dim lenint As Integer
Dim dstr As String
If IsNull(xstr) Then xstr = ""
dstr = xstr
lenint = Len(dstr)
posint = 1
While posint <> 0
posint = InStr(dstr, "'"
If posint <> 0 Then
dstr = Left(dstr, posint - 1) & Chr(96) & Right(dstr, lenint - posint)
End If
Wend
FixApostrophe = dstr
End Function
The problems are caused by the character with ascii 39, and there is another character that looks very like it, ascii 96 which causes no problems.
FixApostrophe replaces all the char 39 with char 96.
QorN is for preparing strings to be put in SQL Statements (but not when they feed parameters on stored procs)
Public Function QOrN(xstr As Variant) As String
If xstr = "" Or IsNull(xstr) Then
QOrN = "Null"
Exit Function
Else
xstr = FixApostrophe(xstr)
QOrN = "'" & xstr & "'"
End If
End Function
Public Function FixApostrophe(xstr As Variant) As String
Dim posint As Integer
Dim lenint As Integer
Dim dstr As String
If IsNull(xstr) Then xstr = ""
dstr = xstr
lenint = Len(dstr)
posint = 1
While posint <> 0
posint = InStr(dstr, "'"
If posint <> 0 Then
dstr = Left(dstr, posint - 1) & Chr(96) & Right(dstr, lenint - posint)
End If
Wend
FixApostrophe = dstr
End Function