# Run code at 12 AM everyday in MS Access

Set timer to run at 12 AM everyday in access how to do that. You can see the code and details in the below thread.
i want to run the macro everyday 12AM. I search for it and could not find how to run on specific time.

https://www.experts-exchange.com/questions/29115749/Loop-through-query-and-send-email-everyday-at-12AM.html
LVL 33
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Senior DeveloperCommented:
Built a separate front-end, which sends the mails started by using the AutoExec macro. Create a scheduled task, which starts that front-end at 12am.
Commented:
You can write a short VBScript which invokes the Access VBA code. You can then use the Windows Task Scheduler to schedule the VBScript to run everyday at 12.
'VB Script
Dim appAccess, blComplete

Set appAccess = CreateObject("Access.Application")
appAccess.OpenCurrentDatabase "\\path\to\the\ms\access\database.mdb"
appAccess.Visible = True

blComplete = appAccess.Run("NameOfVBAFunctionHere", "Pameter1IfAny")

Set appAccess =Nothing


Remember, your VBScript file is just a plain text file with extension *.vbs
EE Solution Guide - Technical Dept HeadAuthor Commented:
This a code in Form Timer
Senior DeveloperCommented:
You missed Mlanda's and my point:

Get rid of the timer. The scheduling is done by invoking the send method via the scheduled task and the AutoExec macro or the VBScript.
Chief Technology OfficerCommented:
i.e:
The Access db is closed.
The Windows Task Scheduler runs the VBScript .vbs file.
The Access db opens...
The autoexec runs....
The VBA process runs....
The VBA code can close the Access file, or do whatever you want it to when it's finished.
Wait for the scheduler to run again at 12:00 AM

Simple....
EE Solution Guide - Technical Dept HeadAuthor Commented:
I created a script and I get this error. It is not opened.

Senior DeveloperCommented:
When using the AutoExec approch, we don't need the VBScript.
Chief Developer/ Microsoft AccessCommented:
I prefer using the timer approach, sometimes the Task Scheduler doesn't work. I have a client that is a summer camp and I set up the timer to play the bugle (Taps) every night at 9PM. The output jack on the computer hooked into the PA system.
Commented:
The Timer sitting on a form will ONLY fire when the Access Database and that Form are open. The Task Scheduler approach will work so long as the Windows machine is powered up and running and will give you a history of previous invocations and whether they succeeded or failed - which makes for a more robust solution in my view.

Maybe use a hybdrid approach:
- You VBScript can check if the Access lock file exists - if it exists - the DB is open and it must not execute.
- The (Access form based) Timer will then fire instead.

Both approaches would work and combine to give a good solution.
EE Solution Guide - Technical Dept HeadAuthor Commented:
I ended up with this batch file and scheduled this batch filie to run everyday 12AM.
START "" /MAX "C:\Program Files (x86)\Microsoft Office\Office14\MSACCESS.EXE" "D:\HRM\HRMS.accdb" /x passportnotify30days

NerdCommented:
For a database dedicated to a scheduled task like this, you should add the following line to the code posted in your earlier question, to close out the database after running the query/emails:

DoCmd.Quit


Experts Exchange Solution brought to you by