Solved

Too Many Quotes

Posted on 1998-06-09
13
138 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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
 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

772 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