Oyster
03-01-2005, 07:57 AM
I'm new to VB so any help will be appreciated:
I'm getting an Expected: list separator or ) error message when I try to run this code. The problem appears to be my RunSQL statement. Thanks in advance for the help!!!
Private Sub CmdSaveSampleArrival_BatchSampleInfo_Click()
On Error GoTo Err_CmdSaveSampleArrival_BatchSampleInfo_Click
Dim i As Long
For i = 1 To Me.[#Replicate]
DoCmd.RunSQL("Insert Into IndividualSampleInformation (Replicate#,LabBatchCode,LabIndSampleCode) " & _
"Values('" & Cstr(i),Me.LabBatchCode, Me.LabBatchCode & CStr(i) & "');
Next i
Exit_CmdSaveSampleArrival_BatchSampleInf:
Exit Sub
Err_CmdSaveSampleArrival_BatchSampleInfo_Click:
MsgBox Err.Description
Resume Exit_CmdSaveSampleArrival_BatchSampleInf
End Sub
modest
03-01-2005, 08:26 AM
DoCmd.RunSQL("Insert Into IndividualSampleInformation (Replicate#,LabBatchCode,LabIndSampleCode) " & _
"Values('" & Cstr(i),Me.LabBatchCode, Me.LabBatchCode & CStr(i) & "');"
Just looking at a glance.. you need to close your quotes... there might also be referencing problems here.. but the quotes would be the biggest problem.
The second biggest problem is that you don't use DoCmd.RunSQL with (). Normally you only use "()" when you're returning a value of a function.
For instance, variable = functionName ("param1",param2)
if function name is just by itself then do:
functionName "param1", param2
3rd) for cleaner programming, put your SQL in a variable
So finished code:
Dim strSQL As String
Dim i As Long
For i = 1 To Me.[#Replicate]
strSQL = "Insert Into IndividualSampleInformation ( Replicate#, LabBatchCode, LabIndSampleCode) " & _
"Values('" & Cstr(i), Me.LabBatchCode, Me.LabBatchCode & CStr(i) & "');"
DoCmd.RunSQL strSQL
Next i
Oyster
03-01-2005, 08:36 AM
Thanks, but I'm afraid its still not working. I plugged in your code and I'm getting an error at "Values('" & Cstr(i),. The comma after (i) is highlighted and the error says, "Expected : end of statement"
Private Sub CmdSaveSampleArrival_BatchSampleInfo_Click()
On Error GoTo Err_CmdSaveSampleArrival_BatchSampleInfo_Click
Dim i As Long
Dim strSQL As String
For i = 1 To Me.[#Replicate]
strSQL = "Insert Into IndividualSampleInformation ( Replicate#, LabBatchCode, LabIndSampleCode) " & _
"Values('" & Cstr(i),Me.LabBatchCode, Me.LabBatchCode & CStr(i) & "');"
DoCmd.RunSQL strSQL
Exit_CmdSaveSampleArrival_BatchSampleInf:
Exit Sub
Err_CmdSaveSampleArrival_BatchSampleInfo_Click:
MsgBox Err.Description
Resume Exit_CmdSaveSampleArrival_BatchSampleInf
End Sub
Oyster
03-01-2005, 09:47 AM
It's working now! Only had a slight error:
Private Sub CmdSaveSampleArrival_BatchSampleInfo_Click()
On Error GoTo Err_CmdSaveSampleArrival_BatchSampleInfo_Click
Dim i As Long
Dim strSQL As String
For i = 1 To Me![#Replicates]
strSQL = "Insert Into IndividualSampleInformation ( [Replicate#], BatchSampleID, LabBatchCode, LabIndSampleCode) " & _
"Values('" & CStr(i) & "','" & Me!BatchSampleID & "', '" & Me!Text61 & "', '" & Me!Text61 & CStr(i) & " ');"
DoCmd.RunSQL strSQL
Next i
Exit_CmdSaveSampleArrival_BatchSampleInf:
Exit Sub
Err_CmdSaveSampleArrival_BatchSampleInfo_Click:
MsgBox Err.Description
Resume Exit_CmdSaveSampleArrival_BatchSampleInf
End Sub
modest
03-01-2005, 10:14 AM
yeah i didn't look at your query, i just copied what you put...and showed you noticeable errors.
glad you got it working.