Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

INSERT INTO Crashes with Apostrophe 5

Status
Not open for further replies.

ClulessChris

IS-IT--Management
Jan 27, 2003
890
0
0
GB
I use the following statement it insert into an Access database:
Code:
g_sSQL = "INSERT INTO tblRequests (sSurname, sInitials,sNino,bFromCCC, sRequested" & _
                ",sAddress, sDetails, sFrom, dSent,dImported)" & _
                "VALUES('" & Field_Arr(0) & "','" & Field_Arr(1) & "','" & Field_Arr(2) & "'" & _
                ",'" & Field_Arr(3) & "','" & Field_Arr(4) & "','" & Field_Arr(5) & "','" & Field_Arr(6) & "'" & _
                ",'" & Field_Arr(7) & "','" & Field_Arr(8) & "','" & Now & "')"
                
        dbFormRequests.Execute g_sSQL

However should one of the fields I try to insert contain an apostrophe (e.g. O'Neil) the statement crashes. Please help.

Everybody body is somebodys Nutter.
 
You need to replace ' with ''

g_sSQL = "INSERT INTO tblRequests (sSurname, sInitials,sNino,bFromCCC, sRequested" & _
",sAddress, sDetails, sFrom, dSent,dImported)" & _
"VALUES('" & Field_Arr(0) & "','" & Field_Arr(1) & "','" & Field_Arr(2) & "'" & _
",'" & Field_Arr(3) & "','" & Field_Arr(4) & "','" & Field_Arr(5) & "','" & Field_Arr(6) & "'" & _
",'" & Field_Arr(7) & "','" & Field_Arr(8) & "','" & Now & "')"

g_sSQL = replace(g_sSQL, "'", "''")

dbFormRequests.Execute g_sSQL

Transcend
[gorgeous]
 
Transcend
Thanks for your input however I still get the same error.

Everybody body is somebodys Nutter.
 
Does this work without apostrophes??

I just realised that you have quotes in your sql so you might be better off doing

g_sSQL = "INSERT INTO tblRequests (sSurname, sInitials,sNino,bFromCCC, sRequested" & _
",sAddress, sDetails, sFrom, dSent,dImported)" & _
"VALUES('" & replace(Field_Arr(0), "'", "''")
& "','" & Field_Arr(1) & "','" & Field_Arr(2) & "'" & _
",'" & Field_Arr(3) & "','" & Field_Arr(4) & "','" & Field_Arr(5) & "','" & Field_Arr(6) & "'" & _
",'" & Field_Arr(7) & "','" & Field_Arr(8) & "','" & Now & "')"


dbFormRequests.Execute g_sSQL

This only replaces quotes in the surname, you should add the replace function to any other variables which may contain quotes.

Transcend
[gorgeous]
 
Yes, Yes, Yes, That worked just GREAT. Many thanks.

Everybody body is somebodys Nutter.
 
A cleaner, safer option - faq709-1526.

zemp
 
Zemp

"A cleaner, safer option - faq709-1526"

I Don't see much difference here apart from the connection, ADO rarther than DAO, is this what you're driving at or have i missed something?

Everybody body is somebodys Nutter.
 
SQL Injection attacks. With a regular insert statement they are possible and can drop your entire database.

Sorry I assumed that you were using ADO and was just offerring an alternative solution.

zemp
 
Zemp was talking about the use of the command object and parameters in which case you will avoid the downfall of having to account for characters such as the apostrophe and others as well as SQL Injection attacks.
 
bjd4jc
Many thanks for the clarifacation.

Everybody body is somebodys Nutter.
 
Here is a VB Function which you can parse all your SQL Text through:

Public Function SQLEncode(sqlValue As String) As String
On Error Resume Next ' NOT really needed
SQLEncode = CStr(Replace(sqlValue, "'", "''"))
End Function


Brian Gillham
FailSafe Systems
 
Failsafe -
Your code (using the replace function to double-up the single quotes) leaves the SQL statement open to attack via SQL Injection. Please read the FAQ that zemp included in his post and you'll see why doing this is such a Bad Thing to do.

Second-worst case scenario for a SQL Injection attack is your drive gets formatted. The worst case is the attacker puts a backdoor on your system that you never detect.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top