INSERT SQL works only with hard coded values

Multi tool use


INSERT SQL works only with hard coded values
Cannot insert a variable using INSERT in MS ACCESS.
the insert works great when I insert a hard-coded value such as
dbs.Execute "INSERT INTO Flights " _
& "(FlightID) VALUES " _
& "('2');"
But fails with a variable
Private Sub MainSaveBtn_Click()
Dim dbs As Database
Set dbs = CurrentDb
Dim id As Integer
id = 20
Debug.Print id
dbs.Execute "INSERT INTO Flights " _
& "(FlightID) VALUES " _
& "('id');"
dbs.Close
End Sub
*Flights table has only one Integer colum named FlightID
What am i missing here?
Are
Flight
and ID
fields in a table or variables in your code?– ashleedawg
18 mins ago
Flight
ID
id is an integer variable im trying to insert its value in the table
– VLados
8 mins ago
2 Answers
2
It look like you are trying to insert "id" in the database. Try to concat your id in the String with the "+" symbol. Or just use a prepared statement to inject your variables in
The 'id' should be a integer variable and not the string 'id'. The correct SQL statement is listed below.
dbs.Execute "INSERT INTO Flights " _
& "(FlightID) VALUES " _
& "(" & CStr(id) & ");"
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
You are trying to insert the text 'id' into an integer column.
– pritaeas
18 mins ago