Use a variable to enter the values ​​of the table

advertisements

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


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)"