tags:

views:

32

answers:

4

I am unable to use a variable as the vale for a table, though direct values are stored. I get the error as "Syntax error in INSERT INTO Statement.How do I overcome this ?

sDBPAth = App.Path & "\SETMDBPATH.mdb"
sConStr = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                  "Data Source=" & sDBPAth & ";" & _
                 "Jet OLEDB:Engine Type=4;"
                 ' Type=4 to create an Access97 mdb, If omitted Access2000 mdb

' ------------------------
' Create New ADOX Object
' ------------------------
 Set oDB = New ADOX.Catalog
 oDB.Create sConStr

 Set oCn = New ADODB.Connection
 oCn.ConnectionString = sConStr
 oCn.Open

 Set oCM = New ADODB.Command
 oCM.ActiveConnection = oCn
 oCM.CommandText = "CREATE TABLE MDBPATH(" & _
        "MDBPATH TEXT(40)      NOT NULL," & _
        "pass  TEXT(10))"
 oCM.Execute

' Populate the table.
 oCn.Execute "INSERT INTO MDBPATH VALUES (sDBPAth, 1,)" 'If 'sDBPath' is used the word sDBPath is stored not the variable value
'
' ------------------------
' Release / Destroy Objects
' ------------------------
 If Not oCM Is Nothing Then Set oCM = Nothing
 If Not oCn Is Nothing Then Set oCn = Nothing
 If Not oDB Is Nothing Then Set oDB = Nothing

 End Sub
+2  A: 

Try

' Populate the table.
 oCn.Execute "INSERT INTO MDBPATH VALUES ('" & sDBPAth & "', 1)"

You had an extra comma at the end, and you need to pass the variable in outside of the string.

You're relatively save in this case since you're not pass in a user input, but if you continue to build INSERT statements like above, you'll be vulnerable to SQL Injection attacks.

LittleBobbyTables
Ha, ha, that's hilarious (the fact that Little Bobby Tables is the one warning us about SQL injection attacks). Good one! +1 for that alone.
paxdiablo
+1  A: 

You need to call the variable outside of the string.

oCn.Execute "INSERT INTO MDBPATH VALUES ('" & sDBPAth & "', 1)"
Laplace
You still have the spare comma inside the parenthesis
LittleBobbyTables
Woops:) Didn't catch that one
Laplace
A: 

If sDBPAth is an actual VB variable, you need to use something like:

oCn.Execute "INSERT INTO MDBPATH VALUES ('" & sDBPAth & "', '1')"

so that the insert statement is constructed from the value of the variable rather than a fixed string.

In addition, you appear to have a superfluous comma in your statement and you may need quotes around both values since they're both text type.

paxdiablo
+4  A: 

You need to amend your SQL statement to allow the application to substitute the sDBPath value

' Populate the table. 
 oCn.Execute "INSERT INTO MDBPATH VALUES ('" & sDBPAth & "', 1)"
Ardman
Thanks for your answer Ardman.It worked perfectly
Dario Dias
A small help, as it can be seen from the script it has a table named "MDBPATH" with two fields "MDBPATH" and "pass".What modification should be done only to write value only in the first fields value not in pass,not write blank also.
Dario Dias
Ardman
Thanks very much.Helped me a lot.
Dario Dias