I would like to be able to insert a statement into my table which contains a number of single quotes. For ex.,
'John's friend'.
What do I surround this statement with in my SQL to avoid errors?
Thank you,
Olga
Printable View
I would like to be able to insert a statement into my table which contains a number of single quotes. For ex.,
'John's friend'.
What do I surround this statement with in my SQL to avoid errors?
Thank you,
Olga
Double quotes - "
JHausMann, I just want to clarify...
Do you mean the string should be "John's Friend" ?
Thanks in advance for your reply...
yup.
"John's Friend"
I am not sure what version of VB you are using so here is a solution that will work for sure:
Then use it in this fashion:Code:Public Function FixQuotes(A As String)
Dim i As Integer
i = InStr(A, "'")
Do While i > 0
A = Left(A, i) & Mid(A, i)
i = InStr(i + 2, A, "'")
Loop
FixQuotes = A
End Function
Also you could use this function to fix all text boxes on a form:Code:YourString = "John's Friend"
YourString = FixQuotes(YourString) ' String now is John''s Friend
If you are using VB6 you could also use the replace function, just replace single quotes with two single quotes.Code:Public Sub FixFormQuotes(glCallfrm As Form)
Dim mycontrol As Control
For Each mycontrol In glCallfrm.Controls
If TypeOf mycontrol Is TextBox Then
mycontrol.Text = FixQuotes(mycontrol.Text)
End If
Next
End Sub
Hope this helps,
It's kind of hard (not impossible, but hard) to wrap a SQL command in single quotes in VB, simply because the compiler will treat it as a comment....
There may be an even easier way to doing this. If you are using a sybase database try making a stored proceedure in the database and pass your string to it as an argument, it may even speed things up.