0

I have a listbox named listComponents1 and I want the items selected in this by the user to be populated in field Components ID of the table tblMasterData. In my table the field Components ID is of type number. I have written the code but I get an error saying syntax error in Insert Into statement. Can someone help with this issue?

For Each varItem In Me.listComponents1.ItemsSelected
    strSQL = "INSERT INTO tblMasterData" & _
                "(Components ID)" & _
                 "Values(" & Me.listComponents1.Column(0, varItem) & ")"
    CurrentDb.Execute strSQL, dbFailOnError
Next

1 Answer 1

1

Field Components ID has a space so enclose in [ ]. Use ItemData to read value from list item.

With Me.listComponents1
    For Each varItem In .ItemsSelected
        strSQL = "INSERT INTO tblMasterData" & _
                    "([Components ID])" & _
                     "Values(" & .ItemData(varItem) & ")"
        CurrentDb.Execute strSQL, dbFailOnError
    Next
End With

Advise not to uses spaces nor punctuation/special characters (underscore only exception) in naming convention.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.