Trying to create a record in a table (tblToday) for any dates in the past five days which do not already exist. This needs to include the previous 5 days for other forms/reports to function correctly.
The code below is the simplest way I could work out how to insert multiple rows (on a row by row basis) however I am having trouble with strNowDateThree...
I found out quickly that it wanted to post dates in MM/DD/YYYY format (not the UK DD/MM/YYYY format) so amended this for all Dates as you can see below.
However strNowDateThree posts in the format DD/MM/YYYY still (hence the msgbox text halfway through so I could work out what it is doing), have I missed something glaringly obvious or is there a better way to handle this?
So running this today gives me the following:
StrNowDateFive = 01/30/2015 (30th jan 15)
StrNowDateFour = 01/31/2015 (31st jan 15)
StrNowDateThree = 01/02/2015 (2nd jan 15) - so doesnt post as is before strLastDate
StrNowDateTwo = 02/02/2015 (2nd feb 15)
StrNowDateOne = 02/03/2015 (3rd feb 15)
StrNowDate = 02/04/2015 (4th feb 15)
I have changed table field to long date so I can see exactly what is posting which is how I first found the error.
The code below is the simplest way I could work out how to insert multiple rows (on a row by row basis) however I am having trouble with strNowDateThree...
I found out quickly that it wanted to post dates in MM/DD/YYYY format (not the UK DD/MM/YYYY format) so amended this for all Dates as you can see below.
However strNowDateThree posts in the format DD/MM/YYYY still (hence the msgbox text halfway through so I could work out what it is doing), have I missed something glaringly obvious or is there a better way to handle this?
Code:
Private Sub cmdLogin_Click()
Dim strNowDate As Date
strNowDate = Format(Date, "MM/DD/YYYY")
Dim strNowDateOne As Date
strNowDateOne = Format(Date - 1, "MM/DD/YYYY")
Dim strNowDateTwo As Date
strNowDateTwo = Format(Date - 2, "MM/DD/YYYY")
Dim strNowDateThree As Date
strNowDateThree = Format(Date - 3, "MM/DD/YYYY")
Dim strNowDateFour As Date
strNowDateFour = Format(Date - 4, "MM/DD/YYYY")
Dim strNowDateFive As Date
strNowDateFive = Format(Date - 5, "MM/DD/YYYY")
Dim strLastDate As Date
strLastDate = Nz(DMax("t_date", "tblToday"))
MsgBox "strNowDateFive = " & strNowDateFive
MsgBox "strLastDate = " & strLastDate
MsgBox "strNowDateThree = " & strNowDateThree
If strNowDateFive > strLastDate Then
DoCmd.RunSQL "INSERT INTO tblToday ([t_date], [t_comments])" & " VALUES " & _
"(" & "#" & strNowDateFive & "#, " & _
"'" & "No notes available" & "')"
Else
MsgBox "strNowDateFive is LESS"
End If
If strNowDateFour > strLastDate Then
DoCmd.RunSQL "INSERT INTO tblToday ([t_date], [t_comments])" & " VALUES " & _
"(" & "#" & strNowDateFour & "#, " & _
"'" & "No notes available" & "')"
Else
MsgBox "strNowDateFour is LESS"
End If
If strNowDateThree > strLastDate Then
DoCmd.RunSQL "INSERT INTO tblToday ([t_date], [t_comments])" & " VALUES " & _
"(" & "#" & strNowDateThree & "#, " & _
"'" & "No notes available" & "')"
Else
MsgBox "strNowDateThree is LESS"
End If
If strNowDateTwo > strLastDate Then
DoCmd.RunSQL "INSERT INTO tblToday ([t_date], [t_comments])" & " VALUES " & _
"(" & "#" & strNowDateTwo & "#, " & _
"'" & "No notes available" & "')"
Else
MsgBox "strNowDateTwo is LESS"
End If
If strNowDateOne > strLastDate Then
DoCmd.RunSQL "INSERT INTO tblToday ([t_date], [t_comments])" & " VALUES " & _
"(" & "#" & strNowDateOne & "#, " & _
"'" & "No notes available" & "')"
Else
MsgBox "strNowDateOne is LESS"
End If
If strNowDate > strLastDate Then
DoCmd.RunSQL "INSERT INTO tblToday ([t_date], [t_comments])" & " VALUES " & _
"(" & "#" & strNowDate & "#, " & _
"'" & "No notes available" & "')"
Else
MsgBox "strNowDate is LESS"
End If
DoCmd.OpenForm "frmMaster"
DoCmd.Close acForm, "frmWelcome", acSaveNo
End Sub
So running this today gives me the following:
StrNowDateFive = 01/30/2015 (30th jan 15)
StrNowDateFour = 01/31/2015 (31st jan 15)
StrNowDateThree = 01/02/2015 (2nd jan 15) - so doesnt post as is before strLastDate
StrNowDateTwo = 02/02/2015 (2nd feb 15)
StrNowDateOne = 02/03/2015 (3rd feb 15)
StrNowDate = 02/04/2015 (4th feb 15)
I have changed table field to long date so I can see exactly what is posting which is how I first found the error.
Last edited: