Hi,
I am trying to use selected values in a list box on a form to create records in a table using a runSQL method. The list box is not bound to a field since i want to make multiple records from the selection. However, for some reason the VBA code below won't return the selected record. Only blank values which appear in the MsgBox. The function results in the invalid SQL syntax error "Syntax error in INSERT INTO statement". The statement works when I substitute text for the variables. So I am fairly confident the SQL statement is correct. Can someone look this over to see if I am missing something? Thanks.
I am trying to use selected values in a list box on a form to create records in a table using a runSQL method. The list box is not bound to a field since i want to make multiple records from the selection. However, for some reason the VBA code below won't return the selected record. Only blank values which appear in the MsgBox. The function results in the invalid SQL syntax error "Syntax error in INSERT INTO statement". The statement works when I substitute text for the variables. So I am fairly confident the SQL statement is correct. Can someone look this over to see if I am missing something? Thanks.
Code:
Private Sub btnAddSample_Click()
On Error GoTo Err_btnAddSample_Click
DoCmd.GoToRecord , , acNewRec
Dim varItem As Variant
Dim sampleName, analysisName, SQLstr As String
Dim analyList As Control
Set analyList = Forms![Enter Sample Information]![Analyses]
sampleName = Me.Sample_Name.Value
For Each varItem In analyList.ItemsSelected
analyisName = analyList.ItemData(varItem)
MsgBox (analysisName)
SQLstr = "INSERT INTO tblSampleAnalyses (Sample_Name,Analysis_Name) VALUES (" & sampleName & "," & analysisName & ");"
DoCmd.RunSQL SQLstr
Next varItem
Exit_btnAddSample_Click:
Exit Sub
Err_btnAddSample_Click:
MsgBox Err.Description
Resume Exit_btnAddSample_Click
End Sub
Comment