I'm using Access VBA to insert values into a table based on information from a form. The SQL statement is throwing a syntax error, however, if I pull the SQL into the query builder it will run so I'm not sure where the syntax error is.
INSERT INTO [TBLactionstaken] (RecordNumber, ActionID, ReasonID, ActionBy, LetterTo, LetterAddress, LetterType, MANHType, MANReqAmt, NRReason1, NRReason2, NRReason3, NRReason4, NRReason5, RFActionID, ActionDate, ActionStatus)
Values (" & Forms![frmActions]![txtrecordNumber2] & ", " & Forms![frmActions]![lstActions] & ", " & Forms![frmActions]![lstReasons] & ", '" & _
Forms![frmActions]![txtActionBy] & "', '" & apostrophe(Forms![frmActions]![cboLetterTo]) & "', '" & Forms![frmActions]![cboLetterAddress] & "', '" & _
Forms![frmActions]![cboLetterType] & "', '" & IIf(IsNull(Forms![frmActions]![cboMANHType]), "", Forms![frmActions]![cboMANHType]) & "', " & _
Forms![frmActions]![cboReqAmt] & ", " & IIf(IsNull(Forms![frmActions]![cboNRReason1]), "", "'" & Forms![frmActions]![cboNRReason1] & "'") & ", " & _
IIf(IsNull(Forms![frmActions]![cboNRReason2]), "", "'" & Forms![frmActions]![cboNRReason2] & "'") & ", " & _
IIf(IsNull(Forms![frmActions]![cboNRReason3]), "", "'" & Forms![frmActions]![cboNRReason3] & "'") & ", " & _
IIf(IsNull(Forms![frmActions]![cboNRReason4]), "", "'" & Forms![frmActions]![cboNRReason4] & "'") & ", " & _
IIf(IsNull(Forms![frmActions]![cboNRReason5]), "", "'" & Forms![frmActions]![cboNRReason5] & "'") & ", " & _
IIf(IsNull(Forms![frmActions]![cboActID]), "", "'" & Forms![frmActions]![cboActID] & "'") & ", #" & Forms![frmActions]![txtDatereceived] & "#, 'Pending')"
This type of code is simply not maintainable. As you have (probably painfully) seen when trying to debug it.
I would replace it with
RecordSet.AddNew, where you assign form values to recordset values line by line. This is readable and maintainable.See comments.