Solved

Too Many Quotes

Posted on 1998-06-09
13
136 Views
Last Modified: 2010-05-03
I"m using SQL to insert a line into a database.  It also uses a variable, so it looks like this
SQL = "INSERT INTO DATABASE (name) VALUES (' " & MyRs("name") & " ')"
MyDb.Execute SQL

The SQL statement then equals:  INSERT INTO DATABASE (name) VALUES ('John Doe')

This works fine, but then I found a case where it crashes.   If the name has an apostrophee.  
ie.   INSERT INTO DATABASE (name) VALUES ('John O'Conner')

There are too many quotes, and the program crashes.  
How can I solve this?
0
Comment
Question by:ssteeves
  • 3
  • 3
  • 2
  • +3
13 Comments
 
LVL 1

Expert Comment

by:Sekans
Comment Utility
Replace the apostrophee with a double quote.
Ex.  ...VALUES('John O''Conner').  This should solve your problem.

Regards,
Sekans

0
 
LVL 9

Expert Comment

by:Dalin
Comment Utility
ssteeves,
Check out:
HOWTO: Query for Literal Special Characters in a
      Where Clause
http://support.microsoft.com/support/kb/articles/q147/6/87.asp
Regards
Dalin
0
 
LVL 1

Author Comment

by:ssteeves
Comment Utility
That's all fine and good, but it doesn't solve my problem.  If I knew before hand I was inserting the name "O'Conner" I'd be fine.  But, it is a variable.   Depending on the what options the user clicks, different names will be inserted into the database.  O'Conner may never be used.
The variable is from a record set:  MyRs("Name"), and the name O'Conner is stored in another database.  Is there any other way I can phase my

SQL = "INSERT INTO DATABASE (name) VALUES (' " & MyRs("name") & " ')"

line so IF MyRs("name") ever equals "O'Conner", my program won't crash?
0
 
LVL 9

Accepted Solution

by:
Dalin earned 50 total points
Comment Utility
ssteeves,
In that case, you need to make a routine to check the name variable to see if it contains the special characters, use the instr function.
Then use a "if" or "case" statment to built up your SQL based on what you have found.
Regards
Dalin

0
 
LVL 1

Expert Comment

by:Sekans
Comment Utility
ssteves,
Try this:
If InStr(strName, "'") > 0 Then
    strName = Left(strName, InStr(strName, "'")) & Right(strName, (Len(strName) - InStr(strName, "'")) + 1)
End If

Sekans
0
 
LVL 4

Expert Comment

by:yowkee
Comment Utility
ssteeves,

  Use a function to process the string you want to insert into database:

---
Public Function funcProcessStr(sText As String) As String

    Dim lPos As Long
    Dim lLen As Long
   
    lPos = InStr(1, sText, "'")
    lLen = Len(sText)
    If lPos = 0 Then
        funcProcessStr = sText
    Else
        If lPos = lLen Then
            funcProcessStr = Mid$(sText, 1, lPos - 1) & "''"
        ElseIf lPos < lLen Then
            If lPos = 1 Then
                funcProcessStr = "''" & _
                    funcProcessStr(Mid$(sText, 2, lLen - 1))
            Else
                funcProcessStr = Mid$(sText, 1, lPos - 1) & _
                  "''" & _                                                         funcProcessStr(Right$(sText, lLen - lPos))
            End If
        End If
    End If
       
End Function
-----
Then,

SQL = "INSERT INTO DATABASE (name) VALUES (' " & funcProcessStr(MyRs("name")) & " ')"


0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 4

Expert Comment

by:yowkee
Comment Utility
Oops..it's too late :)
Good luck, ssteeves..
0
 
LVL 6

Expert Comment

by:clifABB
Comment Utility
What could be simpler than:
SQL = "INSERT INTO DATABASE (name) VALUES (" & Chr$(38) & MyRs("name") & Chr$(38) & ")"
?
0
 
LVL 6

Expert Comment

by:clifABB
Comment Utility
What could be simpler?  How about using the correct CHR$?

That should be Chr$(34) not Chr$(38)!
0
 
LVL 9

Expert Comment

by:Dalin
Comment Utility
clif,
Have you tried your code? The last time I tested, the jet treat chr$(34) no differently than "'", so the problem still exist, but I don't remember if it is in VB4 or VB5.
Dalin
0
 
LVL 12

Expert Comment

by:Trygve
Comment Utility
Some time ago I made this little function. I know that the question has already been answered, but I think this function is a bit easier than the one proposed.

Function ReplaceSubString(OrigStr, SubStr, ReplStr)
On Error Resume Next
' Programmed By:    THa
' Intention:        Replace parts of string.
'                   Exa. ReplaceSubString("Don't say that","'","''") = "Don''t say that"
' Parameters:       OrigStr - Original string to be searched
'                   SubStr - String to be replaced
'                   ReplStr - String to be filled in
' Return:           Updated String

    Dim i As Integer
   
    i = InStr(1, OrigStr, SubStr)
    Do Until i = 0
        OrigStr = Left(OrigStr, i - 1) & ReplStr & Mid(OrigStr, i + Len(SubStr))
        i = InStr(i + Len(ReplStr), OrigStr, SubStr)
    Loop
    ReplaceSubString = OrigStr

End Function

0
 
LVL 6

Expert Comment

by:clifABB
Comment Utility
Dalin:
In answer to your question, I just tried it.
Yes, it works.  (Seems to me it worked on VB4 as well)
0
 
LVL 1

Author Comment

by:ssteeves
Comment Utility
Thanks for all the help!
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now