PDA

Click to See Complete Forum and Search --> : Quotes problem


Stevie
Aug 16th, 2000, 05:45 AM
When searching for a particular Employee I would use a SQL string as such .....


strSQL = "SELECT * FROM Employees " & _
"WHERE Surname = '" & strSurname & "'"


What do I do if the strSurname has a single quote in it?

eg....


strSQL = "SELECT * FROM Employees " & _
"WHERE Surname = 'O'Sullivan'"


Any help would be appreciated. :)

davidrobin
Aug 16th, 2000, 07:08 AM
One way to get round the problem is to either place a extra ' character in the surname if you are using a literal string like so

Where surname= 'O''Sullivan'

or you may prefer to create a function which is passed a surname and checks each character inserting the relevant character where needed like so:

Function SQLStr(ByVal str As String)
Dim intPos As Integer

intPos = InStr(str, "'")
Do While intPos > 0
str = Left(str, intPos - 1) & "'" & Mid(str, intPos)
intPos = InStr(intPos + 2, str, "'")
Loop
SQLStr = str
End Function

so the WHERE clause will look like so


strSQL = "SELECT * FROM Employees " & _
"WHERE Surname = '" & SQLstr(strSurname) & "'"

Stevie
Aug 16th, 2000, 07:21 AM
Cheers, I thought that would be the way to do it.

Thanks for your help. :)