Solved

Too Many Quotes

Posted on 1998-06-09
13
140 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
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…

690 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