Solved

Too Many Quotes

Posted on 1998-06-09
13
137 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
ID: 1462885
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
ID: 1462886
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
ID: 1462887
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
ID: 1462888
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
ID: 1462889
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
ID: 1462890
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 4

Expert Comment

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

Expert Comment

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

Expert Comment

by:clifABB
ID: 1462893
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
ID: 1462894
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
ID: 1462895
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
ID: 1462896
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
ID: 1462897
Thanks for all the help!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…

920 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

11 Experts available now in Live!

Get 1:1 Help Now