VBA 访问。插入错误
VBA Access. Insert Into Error
我有一个名为 listComponents1 的列表框,我希望用户在其中选择的项目填充到 table tblMasterData 的字段组件 ID 中。在我的 table 中,字段 Components ID 的类型是数字。我已经编写了代码,但我收到一条错误消息,指出 Insert Into 语句中存在语法错误。有人可以帮助解决这个问题吗?
For Each varItem In Me.listComponents1.ItemsSelected
strSQL = "INSERT INTO tblMasterData" & _
"(Components ID)" & _
"Values(" & Me.listComponents1.Column(0, varItem) & ")"
CurrentDb.Execute strSQL, dbFailOnError
Next
字段 Components ID
有一个 space,所以请附在 [ ] 中。使用 ItemData
从列表项中读取值。
With Me.listComponents1
For Each varItem In .ItemsSelected
strSQL = "INSERT INTO tblMasterData" & _
"([Components ID])" & _
"Values(" & .ItemData(varItem) & ")"
CurrentDb.Execute strSQL, dbFailOnError
Next
End With
建议不要在命名约定中使用 spaces 或 punctuation/special 字符(下划线除外)。
我有一个名为 listComponents1 的列表框,我希望用户在其中选择的项目填充到 table tblMasterData 的字段组件 ID 中。在我的 table 中,字段 Components ID 的类型是数字。我已经编写了代码,但我收到一条错误消息,指出 Insert Into 语句中存在语法错误。有人可以帮助解决这个问题吗?
For Each varItem In Me.listComponents1.ItemsSelected
strSQL = "INSERT INTO tblMasterData" & _
"(Components ID)" & _
"Values(" & Me.listComponents1.Column(0, varItem) & ")"
CurrentDb.Execute strSQL, dbFailOnError
Next
字段 Components ID
有一个 space,所以请附在 [ ] 中。使用 ItemData
从列表项中读取值。
With Me.listComponents1
For Each varItem In .ItemsSelected
strSQL = "INSERT INTO tblMasterData" & _
"([Components ID])" & _
"Values(" & .ItemData(varItem) & ")"
CurrentDb.Execute strSQL, dbFailOnError
Next
End With
建议不要在命名约定中使用 spaces 或 punctuation/special 字符(下划线除外)。