Ian Belcher said:
Hi,
I've got a text field which I have no control over what data is
entered.
I'm also not allowed to change the data (internal auditors!). When I
want to use the data I get problems with the string finishing at "
and not at the end. Is there a way to get around this problem? I
also get errors with |. I assume there are several characters that
will interupt a string. Does anyone have a complete list?
I'm not aware of any problems with the pipe character (|), provided it's
inside quotes. Maybe you could give an example of the sort of problems
you're encountering with that one -- data and code, please. Embedded
quotes do cause problems when you try to embed them into SQL statements
you build at run time. Usually, your best bet is to pick whichever is
the least common of the two accepted quote characters, (') and ("), and
use that as the string delimiter. Then use the Replace function to
double-up any occurrence of that character that may occur inside the
string.
I usually settle on the double-quote character ("), since the
single-quote (') is more common in the data I see, so I've written this
function to do my quoting for me:
'----- start of code -----
Function fncQuoted(StringToQuote As String) As String
fncQuoted = _
Chr(34) & _
Replace(StringToQuote, Chr(34), """""", , , vbBinaryCompare) & _
Chr(34)
End Function
'----- end of code -----
I can call the function when building a SQL string, without worrying
about what a string variable contains. For example,
strSQL = _
"INSERT INTO tblCustomerContacts " & _
"(CustomerID, Employee, ContactDate, ContactDetails) " & _
"VALUES(" & _
Me.CustomerID & ", " & _
fncQuoted(Me.txtEmployee) & ", " & _
Format(Me.txtContactDate, "\#mm/dd/yyyy\#") & ", " & _
fncQuoted(Me.txtContactDetails) & _
")"