Append query in VBA for microsoft access not taking the WHERE parameters

自古美人都是妖i 提交于 2021-01-28 17:46:21

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!