INSERT SQL works only with hard coded values

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


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?





You are trying to insert the text 'id' into an integer column.
– pritaeas
18 mins ago





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.

Popular posts from this blog

Visual Studio Code: How to configure includePath for better IntelliSense results

Spring cloud config client Could not locate PropertySource

Makefile test if variable is not empty