Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Too Many Quotes

Posted on 1998-06-09
13
Medium Priority
?
142 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 9

Accepted Solution

by:
Dalin earned 200 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
Suggested Courses

604 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