问题
I have this function:
Dim db As DAO.Database
Dim strInsert As String
Set db = CurrentDb
strInsert = "INSERT INTO items_affected_Table(CR_Id, VerOld, Equipment_Serial, Document_No, Description, Version) SELECT Items_affected_Table.CR_Id, Items_affected_Table.Version, Items_affected_Table.Equipment_Serial, Items_affected_Table.Document_No, Items_affected_Table.Description, Items_affected_Table.VerNext FROM Items_affected_Table WHERE (((Items_affected_Table.CR_Id)=[Forms]![CR form-unapproved]![CR_ID]) AND ((Items_affected_Table.Version)=[Forms]![CR form-unapproved]![Version]))"
db.Execute strInsert, dbFailOnError
It gives the error "too few parameters. Expected 2"
If I change the form references to actual numbers the append query works:
strInsert = "INSERT INTO items_affected_Table(CR_Id, VerOld, Equipment_Serial, Document_No, Description, Version) SELECT Items_affected_Table.CR_Id, Items_affected_Table.Version, Items_affected_Table.Equipment_Serial, Items_affected_Table.Document_No, Items_affected_Table.Description, Items_affected_Table.VerNext FROM Items_affected_Table WHERE (((Items_affected_Table.CR_Id)=8) AND ((Items_affected_Table.Version)=0))"
What am I missing?
Thanks
回答1:
Concatenate variable references:
strInsert = "INSERT INTO items_affected_Table(CR_Id, VerOld, Equipment_Serial, Document_No, Description, Version) " & _
"SELECT CR_Id, Version, Equipment_Serial, Document_No, Description, VerNext " & _
"FROM Items_affected_Table " & _
"WHERE CR_Id=" & [Forms]![CR form-unapproved]![CR_ID] & " AND Version=" & [Forms]![CR form-unapproved]![Version]
来源:https://stackoverflow.com/questions/60749653/append-query-in-vba-for-microsoft-access-not-taking-the-where-parameters