B
bjh29
Thank you in advance for any help.
I am trying to insert data from an unbound form into a table. I keep
getting the run-time error 3134. syntax error in INSERT INTO statement.
I have tried single quotes, quotes as you see below. I have tried
db.Execcute and the DoCmd you see below. I inherited this unfinished project
and trying to work out the bugs.
The table I am inserting into is 'CO-OPStudentReg' with fields: LMHSCID,
sessionID, studentsID, ClassChoice. I don't have a primary key set or an
auto number field. The fields in the table look up values from four other
tables.
My form has LMHSCID (giving me info from a user initiated parameter query),
sessionID (again, info from parameter qry), studentsID (parameter qry), and
ClassChoice10 (selects info from my Classes table and sorts by the class
time), ClassChoice11 (info from same class table, just limiting to 11:00
time), ClassChoice1, ClassChoice2.
Essentially, I would like to enter up to four records into my table
depending on how many classes the student has chosen.
If I don't select any classes and click my button, the form clears as it is
supposed to, but no data is entered into table. As soon as I choose a class,
I get the error.
Thanks,
bjh29
Here is my code:
Private Sub btnADD_Click()
'**** add a new record ****
Dim db As Database
Dim rs As Recordset
Set db = CurrentDb
If Not IsNull(ClassChoice10) Then
DoCmd.RunSQL "INSERT INTO CO-OPStudentReg
([LMHSCID],[sessionID],[studentsID],[ClassChoice]) VALUES (""" &
LMHSCID.Value & """,""" & sessionID.Value & """,""" & studentsID.Value &
""",""" & ClassChoice10.Value & """)"
End If
If Not IsNull(ClassChoice11) Then
DoCmd.RunSQL "INSERT INTO CO-OPStudentReg
([LMHSCID],[sessionID],[studentsID],[ClassChoice]) VALUES (""" &
LMHSCID.Value & "," & sessionID.Value & "," & studentsID.Value & "," &
ClassChoice11.Value & """)"
End If
If Not IsNull(CLassChoice1) Then
DoCmd.RunSQL "INSERT INTO CO-OPStudentReg
([LMHSCID],[sessionID],[studentsID],[ClassChoice]) VALUES (""" &
LMHSCID.Value & "," & sessionID.Value & "," & studentsID.Value & "," &
CLassChoice1.Value & """)"
End If
If Not IsNull(ClassChoice2) Then
DoCmd.RunSQL "INSERT INTO CO-OPStudentReg
([LMHSCID[,[sessionID],[studentsID],[ClassChoice]) VALUES (""" &
LMHSCID.Value & "," & sessionID.Value & "," & studentsID.Value & "," &
ClassChoice2.Value & """)"
MsgBox (Me!studentsID) & " has been added to the CO-OP
registration table."
End If
studentsID.Value = Null
ClassChoice10.Value = Null
ClassChoice11.Value = Null
CLassChoice1.Value = Null
ClassChoice2.Value = Null
End Sub
I am trying to insert data from an unbound form into a table. I keep
getting the run-time error 3134. syntax error in INSERT INTO statement.
I have tried single quotes, quotes as you see below. I have tried
db.Execcute and the DoCmd you see below. I inherited this unfinished project
and trying to work out the bugs.
The table I am inserting into is 'CO-OPStudentReg' with fields: LMHSCID,
sessionID, studentsID, ClassChoice. I don't have a primary key set or an
auto number field. The fields in the table look up values from four other
tables.
My form has LMHSCID (giving me info from a user initiated parameter query),
sessionID (again, info from parameter qry), studentsID (parameter qry), and
ClassChoice10 (selects info from my Classes table and sorts by the class
time), ClassChoice11 (info from same class table, just limiting to 11:00
time), ClassChoice1, ClassChoice2.
Essentially, I would like to enter up to four records into my table
depending on how many classes the student has chosen.
If I don't select any classes and click my button, the form clears as it is
supposed to, but no data is entered into table. As soon as I choose a class,
I get the error.
Thanks,
bjh29
Here is my code:
Private Sub btnADD_Click()
'**** add a new record ****
Dim db As Database
Dim rs As Recordset
Set db = CurrentDb
If Not IsNull(ClassChoice10) Then
DoCmd.RunSQL "INSERT INTO CO-OPStudentReg
([LMHSCID],[sessionID],[studentsID],[ClassChoice]) VALUES (""" &
LMHSCID.Value & """,""" & sessionID.Value & """,""" & studentsID.Value &
""",""" & ClassChoice10.Value & """)"
End If
If Not IsNull(ClassChoice11) Then
DoCmd.RunSQL "INSERT INTO CO-OPStudentReg
([LMHSCID],[sessionID],[studentsID],[ClassChoice]) VALUES (""" &
LMHSCID.Value & "," & sessionID.Value & "," & studentsID.Value & "," &
ClassChoice11.Value & """)"
End If
If Not IsNull(CLassChoice1) Then
DoCmd.RunSQL "INSERT INTO CO-OPStudentReg
([LMHSCID],[sessionID],[studentsID],[ClassChoice]) VALUES (""" &
LMHSCID.Value & "," & sessionID.Value & "," & studentsID.Value & "," &
CLassChoice1.Value & """)"
End If
If Not IsNull(ClassChoice2) Then
DoCmd.RunSQL "INSERT INTO CO-OPStudentReg
([LMHSCID[,[sessionID],[studentsID],[ClassChoice]) VALUES (""" &
LMHSCID.Value & "," & sessionID.Value & "," & studentsID.Value & "," &
ClassChoice2.Value & """)"
MsgBox (Me!studentsID) & " has been added to the CO-OP
registration table."
End If
studentsID.Value = Null
ClassChoice10.Value = Null
ClassChoice11.Value = Null
CLassChoice1.Value = Null
ClassChoice2.Value = Null
End Sub