This example shows how to use a command line switch and a scheduled task to start a Microsoft Access application unattended and run some logic automatically.
Perhaps you need a switchboard form to appear for users when they open a database during the day, but you want the database to open automatically at night, skip the switchboard, and run some logic. One option is to create separate front-end databases. Another option is to give the database an autopilot that runs at night. Here’s how.
(1) Add this code to your database:
Public gblnAuto As Boolean
Public Function IsAuto() As Boolean
' This function returns a True if database was started with the
' Auto command.
' IsAuto() Version 1.0.0
' Copyright © 2009 Extra Mile Data, www.extramiledata.com.
' For questions or issues, please contact support@extramiledata.com.
' Use (at your own risk) and modify freely as long as proper credit is given.
On Error GoTo Err_IsAuto
' Check the command option to see if the database was started
' with the Auto command. Set the global variable and pass back
' a True or False.
If Command = "Auto" Then
gblnAuto = True
IsAuto = True
Else
gblnAuto = False
IsAuto = False
End If
Exit_IsAuto:
On Error Resume Next
Exit Function
Err_IsAuto:
MsgBox Err.Number & " " & Err.Description, vbCritical, "IsAuto"
IsAuto = False
Resume Exit_IsAuto
End Function
Though not necessary, you can use the global variable gblnAuto to control how the logic, the forms, or the reports are run.
(2) Create a scheduled task that uses something like this for the Run value:
"C:\Program Files\Microsoft Office\Office12\MSACCESS.EXE" "C:\TestCode.accdb" /cmd Auto
The value behind the /cmd command line switch will be returned when the VBA function Command() is called.
(3) In the database, create a macro named AutoExec.
The AutoExec macro will run when the database is opened. Make the actions it runs conditional on the results of IsAuto(). For instance, if IsAuto() = False, the user opened the database, so open the switchboard form. If IsAuto() = True, then the database was opened by the scheduled task. In that case, run some logic and then close the database.

Download Code: basAuto.zip