VBA/SQL : INSERT INTO query how to deal with optional value ?

Nyanko

Registered User.
Local time
Today, 16:34
Joined
Apr 21, 2005
Messages
57
Hi,

I am using the below adapted code to insert multiple invoices into a database.
http://stackoverflow.com/questions/...statement-to-loop-through-fields-in-a-subform

I have included the below function to help with the US/UK/SQL dates issue that I always run into
http://allenbrowne.com/ser-36.html

I am getting an error when a date field is empty (it is an optional field) however I think because the SQL statement is looking for a value it is failing. I have tested and with the date entered it works, when left blank it fails.

My question is : On the below code how can I have this statement allow an empty/null value ?
Code:
" & SQLDate(rst![RPIDate]) & "

Successful debug result with date :
INSERT INTO tbl_MainData (RecordID, ScheduledDate, RPIDate, InvoiceID, ProformaID, TransactionType, InputBy, InputDate, PONumber, ProjectCode, ServiceCode, InvoiceValue, Commissionable, Terms, DueDate, ExpectedDate, Status, InvFreq) VALUES (10082, #06/07/2016#, #04/01/2016#,'111', '222', 'Invoice', 'John Smith', #07/06/2016#, '', 'ENE01198', 'CAS00000', 123, 123, '30 Days',#07/07/2016#,#08/06/2016#,'Proforma Scheduled to Raise', 'TBC')

Unsuccessful debug with no date :
INSERT INTO tbl_MainData (RecordID, ScheduledDate, RPIDate, InvoiceID, ProformaID, TransactionType, InputBy, InputDate, PONumber, ProjectCode, ServiceCode, InvoiceValue, Commissionable, Terms, DueDate, ExpectedDate, Status, InvFreq) VALUES (10081, #06/07/2016#, ,'111', '222', 'Invoice', 'John Smoth', #07/06/2016#, '', 'ENE01198', 'CAS00000', 123, 123, '30 Days',#07/07/2016#,#08/06/2016#,'Proforma Scheduled to Raise', 'TBC')

I get an error 3134 : Syntax error in INSERT INTO statement

Full Code :
Code:
Private Sub btn_CreateInvoices_Click()
'------------------------------------------------------------
' Procedure : Create the repeat invoices
' Author    : HansV
' Website   : http://windowssecrets.com/forums/showthread.php/37448-looping-through-fields-(Access-2000-SR-1)
' Amendment : http://stackoverflow.com/questions/36061026/need-help-to-amend-access-sql-statement-to-loop-through-fields-in-a-subform
' Purpose   : Loop through the fields in a temp table then update the main table
'------------------------------------------------------------
Dim StrSQL As String, MyStatus As String
Dim RecordIDValue As Long, RowIDValue As Long, UpdateCount As Long

Dim dbs As DAO.Database
Dim tdf As DAO.TableDef
Dim rst As DAO.Recordset
Dim fld As DAO.Field

Set dbs = CurrentDb
Set tdf = dbs.TableDefs("tbl_RepeatTemp")
Set rst = tdf.OpenRecordset

'Find the next RecordID to use
    RecordIDValue = DMax("[RecordID]", "[tbl_MainData]") + 1
    RowIDValue = DMax("[RowID]", "[tbl_MainData]")
    UpdateCount = 0
    If Me.FormProformaStatus = "" Then MyStatus = "" Else MyStatus = "Proforma Scheduled to Raise"

' Loop through the scheduled dates entered
    Do Until rst.EOF
        'Insert into tbl_MainData
        StrSQL = ""
        StrSQL = StrSQL & " INSERT INTO tbl_MainData"
        StrSQL = StrSQL & " (RecordID, ScheduledDate, RPIDate, InvoiceID, ProformaID, TransactionType, InputBy, InputDate, PONumber, ProjectCode, ServiceCode, InvoiceValue, Commissionable, Terms, DueDate, ExpectedDate, Status, InvFreq)"
        StrSQL = StrSQL & " VALUES"
        StrSQL = StrSQL & " (" & RecordIDValue & ", " & SQLDate(rst![ScheduleDate]) & ", " & SQLDate(rst![RPIDate]) & ",'" & Me.FormInvoiceID & "', '" & Me.FormProformaID & "',  '" & rst![TransactionType] & "', '" & Me.FormInputBy & "', #" & Format(Me.InputDate, "dd/mm/yyyy") & "#, '" & Me.FormPONumber & "', '" & Me.FormProjectCode & "', '" & Me.FormServiceCode & "', " & Me.FormInvoiceValue & ", " & Abs(Me.FormInvoiceValue) & ", '" & Me.FormTerms & "'," & SQLDate(rst![ScheduleDate] + Left(Me.FormTerms, 2)) & "," & SQLDate(rst![ScheduleDate] + Left(Me.FormTerms, 2) + 30) & ",'" & MyStatus & "', '" & Me.FormInvFreq & "')"
        Debug.Print StrSQL
        CurrentDb.Execute StrSQL
    
        'Insert into tbl_DebtTracker
        RowIDValue = RowIDValue + 1
        StrSQL = ""
        StrSQL = StrSQL & " INSERT INTO tbl_DebtTracker"
        StrSQL = StrSQL & " (RowID, RecordID, ProjectCode)"
        StrSQL = StrSQL & " VALUES"
        StrSQL = StrSQL & " (" & RowIDValue & ", " & RecordIDValue & ", '" & Me.FormProjectCode & "');"
        Debug.Print StrSQL
        CurrentDb.Execute StrSQL
        UpdateCount = UpdateCount + 1
    rst.MoveNext
    Loop
    rst.Close
    
'Notify
DoCmd.Close acForm, "frm_MainInputRepeat"
MsgBox UpdateCount & " Records successfully created.", , "Success !"
    
    'Clear values
    Set rst = Nothing
    Set fld = Nothing
    Set tdf = Nothing
    Set dbs = Nothing
End Sub

Advice and suggestions gratefully received !
 
Hi Nyanko,

Firstly thank you for such a concise explanation of the issue.

My approach here would be to check the value of rst![RPIDate] before building strSQL. You will need to build strSQL including RPIDate if RPIDate is a date and build strSQL excluding RPIDate if RPIDate is null.

Code:
If Nz(rst!RPIDate, "") = "" Then
    strSQL = strSQL & " INSERT INTO tbl_MainData"
    strSQL = strSQL & " (RecordID, ScheduledDate, InvoiceID, ProformaID, TransactionType, InputBy, InputDate, PONumber, ProjectCode, ServiceCode, InvoiceValue, Commissionable, Terms, DueDate, ExpectedDate, Status, InvFreq)"
    strSQL = strSQL & " VALUES"
    strSQL = strSQL & " (" & RecordIDValue & ", " & SQLDate(rst![ScheduleDate]) & ",'" & Me.FormInvoiceID & "', '" & Me.FormProformaID & "',  '" & rst![TransactionType] & "', '" & Me.FormInputBy & "', #" & Format(Me.InputDate, "dd/mm/yyyy") & "#, '" & Me.FormPONumber & "', '" & Me.FormProjectCode & "', '" & Me.FormServiceCode & "', " & Me.FormInvoiceValue & ", " & Abs(Me.FormInvoiceValue) & ", '" & Me.FormTerms & "'," & SQLDate(rst![ScheduleDate] + Left(Me.FormTerms, 2)) & "," & SQLDate(rst![ScheduleDate] + Left(Me.FormTerms, 2) + 30) & ",'" & MyStatus & "', '" & Me.FormInvFreq & "')"
Else
    strSQL = strSQL & " INSERT INTO tbl_MainData"
    strSQL = strSQL & " (RecordID, ScheduledDate, RPIDate, InvoiceID, ProformaID, TransactionType, InputBy, InputDate, PONumber, ProjectCode, ServiceCode, InvoiceValue, Commissionable, Terms, DueDate, ExpectedDate, Status, InvFreq)"
    strSQL = strSQL & " VALUES"
    strSQL = strSQL & " (" & RecordIDValue & ", " & SQLDate(rst![ScheduleDate]) & ", " & SQLDate(rst![RPIDate]) & ",'" & Me.FormInvoiceID & "', '" & Me.FormProformaID & "',  '" & rst![TransactionType] & "', '" & Me.FormInputBy & "', #" & Format(Me.InputDate, "dd/mm/yyyy") & "#, '" & Me.FormPONumber & "', '" & Me.FormProjectCode & "', '" & Me.FormServiceCode & "', " & Me.FormInvoiceValue & ", " & Abs(Me.FormInvoiceValue) & ", '" & Me.FormTerms & "'," & SQLDate(rst![ScheduleDate] + Left(Me.FormTerms, 2)) & "," & SQLDate(rst![ScheduleDate] + Left(Me.FormTerms, 2) + 30) & ",'" & MyStatus & "', '" & Me.FormInvFreq & "')"
End If
 
Thank you for your fast reply.

I'm still getting stuck when there is no date added to the RPIDate field and it's still returning error 3134 : Syntax error in INSERT INTO statement.

I think it still wants something entered into the VALUES section...
I tried forcing a 0 in there somehow

Code:
        If Nz(rst!RPIDate, "") = "" Then
            StrSQL = StrSQL & " INSERT INTO tbl_MainData"
            StrSQL = StrSQL & " (RecordID, ScheduledDate, InvoiceID, ProformaID, TransactionType, InputBy, InputDate, PONumber, ProjectCode, ServiceCode, InvoiceValue, Commissionable, Terms, DueDate, ExpectedDate, Status, InvFreq)"
            StrSQL = StrSQL & " VALUES"
            StrSQL = StrSQL & " (" & RecordIDValue & ", " & SQLDate(rst![ScheduleDate]) & ",[COLOR="Red"]#" & Format(0, "dd/mm/yyyy") & "#[/COLOR],'" & Me.FormInvoiceID & "', '" & Me.FormProformaID & "',  '" & rst![TransactionType] & "', '" & Me.FormInputBy & "', #" & Format(Me.InputDate, "dd/mm/yyyy") & "#, '" & Me.FormPONumber & "', '" & Me.FormProjectCode & "', '" & Me.FormServiceCode & "', " & Me.FormInvoiceValue & ", " & Abs(Me.FormInvoiceValue) & ", '" & Me.FormTerms & "'," & SQLDate(rst![ScheduleDate] + Left(Me.FormTerms, 2)) & "," & SQLDate(rst![ScheduleDate] + Left(Me.FormTerms, 2) + 30) & ",'" & MyStatus & "', '" & Me.FormInvFreq & "')"

I sense there's an answer here, but everything I've tried doesn't work
http://www.tek-tips.com/viewthread.cfm?qid=300424
 
The piece of code you just posted has 17 field names but 18 values so Access does not know where the extra piece of data should go.

When there is no date (rst![RPIDate] is null) you do not want to include the field name [RPIDate] in list of fields to assign values to AND you do not want to include a value in the VALUES list.
 
Wow. I don't know how I missed that.

This works brilliantly now. Thank you for your help and advice !
 

Users who are viewing this thread

Back
Top Bottom