Storing appostrophe's in SQL

  • Thread starter Thread starter Jeff
  • Start date Start date
J

Jeff

I have a text box that allows the user to type in free
form text. I want to save it to a SQLServer database
table. If the user types in <Here's Johnny!>, I can insert
it into SQLServer by inserting the text string <Here''s
Johnny!>

My question: is there a function that does this, or do I
process the text string before I insert it to double all
the appostrophe's?

Thanks!
 
Hi Jeff,
for example:
Function FixApostrophy(ByVal sSQL As String) As String
Dim sPhrase As String, wLength As Integer, m As Integer
wLength = Len(sSQL)
For m = 1 To wLength
If Mid(sSQL, m, 1) = "'" Then
sPhrase = sPhrase & "''"
Else
sPhrase = sPhrase & Mid(sSQL, m, 1)
End If
Next
FixApostrophy = sPhrase
End Function

MP
 
Hi Jeff,

If your code will always be running on Excel 2000 or higher versions of
Excel you can use the VBA Replace function to do this.

Dim szSQLString As String
szSQLString = Replace(txtEntry.Text, "'", "''")

--
Rob Bovey, MCSE, MCSD, Excel MVP
Application Professionals
http://www.appspro.com/

* Please post all replies to this newsgroup *
* I delete all unsolicited e-mail responses *
 
That's a great solution for when we migrate everyone to >=
Excel 2000. But for now I have a lot of users on '97.

Thanks - Jeff
 
Hi Jeff,
you can use Application.Substitute
Dim szSQLString As String
szSQLString = Application.Substitute(txtEntry.Text, "'", "''")

MP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top