Link to home
Start Free TrialLog in
Avatar of groone
groone

asked on

VB6 SQL usage myDB.execute help

In an application I have a sql call similar to the one below:
mydb.Execute ("DELETE FROM myTable WHERE myField = '" & Combo1.Text & "'")

Now the problem exist that the myField can contain an apostrophe like in - Gun's - but when this happens the SQL statement bombs.  How can I use this statement when such an occurence exists?

Avatar of hes
hes
Flag of United States of America image

Try using this

mydb.Execute ("DELETE FROM myTable WHERE myField = '" & Replace(Combo1.Text,"'","''") & "'")
 Because it is hard to read it is replace one ' with two ''
300 point? :)

mydb.Execute ("DELETE FROM myTable WHERE myField = """ & Combo1.Text & """")

hes i am not agree with you about using another function instead using two " in a text.

regards
suat
ASKER CERTIFIED SOLUTION
Avatar of srauda
srauda

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of blueforce
blueforce

Dim sText as String

sText = Replace(Combo1.Text, "'", "''")

mydb.Execute("DELETE FROM myTable WHERE myField = '" & sText & "''"


The object is to replace any apostrophes within the text with double apostrophes.  This will keep SQL from bombing as well as retain the apostrophes within the original text in the DB.

Thank u