I am trying to create an automated "End of Month" report which prints when an end of day report is printed, but it's also the last day of the month.
It follows some simple conditions:
1) Find the last day of the month and stamp it in a table
2) If the last day is a Sunday, then stamp the Saturday instead
3) If the stamp date is in the past, the end of month report wasn't processed (i.e. for what ever reason the store wasn't open on the last day of the month - for example Christmas) - then produce report.
At the moment, I have a stamp in my main table "tblBranch": "30/09/2014"
It follows some simple conditions:
1) Find the last day of the month and stamp it in a table
2) If the last day is a Sunday, then stamp the Saturday instead
3) If the stamp date is in the past, the end of month report wasn't processed (i.e. for what ever reason the store wasn't open on the last day of the month - for example Christmas) - then produce report.
At the moment, I have a stamp in my main table "tblBranch": "30/09/2014"
Code:
Dim EndDay As Date
Dim EndMonth As Date
Dim dbs As Database
Dim rs As Recordset
Dim EndOfMonth As String
Dim NextMonth As Date
Set dbs = CurrentDb
bi = "5" 'retreived from a function to find Branch ID
EndOfMonth = "Select * FROM tblBranch Where ID = " & bi
Set rs = dbs.OpenRecordset(EndOfMonth)
EndDay = rs.Fields(22) ' field 22 = date '<============= This isn't retrieving the date properly!!!
Debug.Print EndDay ' prints 00:00:00 instead of 30/09/2014
NextMonth = DateSerial(Year(EndDay), Month(EndDay) + 1, 0) finds the last day of the next month
If Format(NextMonth, "ddd") = "Sun" Then 'if its a Sunday, use Saturday as the last day
NextMonth = DateSerial(Year(NextMonth), Month(NextMonth), Day(NextMonth) - 1)
End If
If EndDay <= Date Then
dbs.Execute "UPDATE tblBranch SET [End of Month] = " & NextMonth & " WHERE ID = " & bi 'update the next end of month date
MsgBox "You are " & Date - EndDay & "over!!"
Else
MsgBox "not yet!"
End If