MS-Access query to only run once a day when first user opens.
I have an MS- Access database that needs to run a query whenever the user opens it for the first time in the day and only once a day. Is there code that I can run that will do this?
Thanks.
Microsoft AccessSQL
Last Comment
donnie91910
8/22/2022 - Mon
Rey Obrero (Capricorn1)
one way to do this, which i have done in one of my application is to create a table with a Date/time field.
when the db is open, check if there is value in the Date field equal to the current date,
if no date found, run the query, then insert or update the date field with the current date.
got the idea?
donnie91910
ASKER
I created a table called DateCheck with a field called ForDateCheck as a DateTime datatype.
So here is the code that I have written. Please feel free to make any changes that you feel are syntacticly incorrect. Thanks.
Public Sub Check_Date()
If [DateCheck]![ForDateCheck] <> Now() Then
DoCmd.RunQuery "Qry_RunTestData"
Else
DoCmd.RunQuery "Qry_UpdateCheck_DateWithTodaysDate"
End If
End Sub
* you can call the sub in the open event of the form
* you can place the codes in the open event of the form
* you can change the Sub to a Function and run the Function from a macro
take your pick
donnie91910
ASKER
I changed the code to a Function. When I run the function from a Macro I get the following Error:
RunTime error '2001':
You Cancelled the Previous Operation
End Debug
I choose Debug
and the first line of code is highlighted in yellow:
If Nz(DLookup("ForDateCheck", "DateCheck")) <> Date Then
when the db is open, check if there is value in the Date field equal to the current date,
if no date found, run the query, then insert or update the date field with the current date.
got the idea?