Search code examples
vbams-access

VBA Does Not Return Correct System Date


I have a procedure that will send a report out if the report has not been sent out on that date. When it sends the report it will run a query that inserts the date and report name into a table.

The problem is the date that it is returning is February 19th, 2019. I would understand if it was a formatting issue but this date is completely off.

Dim strFile As String
Dim strTo As String
Dim strSubject As String
Dim strBody As String
Dim InReport As String
Dim InDate As Date

strFile = Forms!frmMain!strFile
strTo = "email"
strSubject = "subject"
strBody = "body"
InReport = "ReportName"
InDate = Date

    If DCount("*", "tblEmailTracking", "[SentDate]=#" & InDate & "#") > 0 Then

    Exit Sub
        'do nothing
Else
        'send email
        
    DoCmd.OpenReport "ReportName", acViewPreview, , , acHidden
       Reports("ReportName").Caption = strFile
       
    DoCmd.SendObject acSendReport, "ReportName", acFormatPDF, strTo, strCC, , 
    strSubject, strBody, False
    DoCmd.RunSQL "INSERT INTO tblEmailTracking(SentDate, SentReport) VALUES('" & InDate & "', '" & InReport & "');"
    
    DoCmd.Close acReport, "ReportName"

End If

Solution

  • Leave the work to SQL:

    DoCmd.RunSQL "INSERT INTO tblEmailTracking(SentDate, SentReport) VALUES( Date(), '" & InReport & "');"