Use a batch file to open Access 2010, run a query, and close Access?

Hello Experts.

I need to open Access 2010, run a query, and close Access.  I anticipate using Scheduler to run the batch file once a day.  If there is an easier, or better way to do this please feel free to impart you wisdom.

The macro is:
Private Sub autoMasterQry_Click()
DoCmd.TransferText acExportDelim, "", "masterQry", "F:\Folder\Master" & Format(Date, "yymmdd") & ".csv", True, ""
End Sub

Open in new window

The batch file is:
"F:\Folder\dbFolder\myDb.accdb" /X autoMasterQry

Open in new window

The query works as intended.  But when I attempt to use the batch file to execute the query I get the following error message after Access opens:

MyDatabase cannot find the object 'autoMasterQry.'
If 'autoMasterQry' is a new macro or macro group, make sure you have saved it and that you have typed its name correctly.

Any help would be greatly appreciated.

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.

Brad GrouxSenior Manager (Wintel Engineering)Commented:
Create a macro in to execute the query.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
That's not a macro, that's VBA code.

IMO the best way to do this is to launch your database directly from a scheduled task, and then fire off the code you need. You could create a macro to do this, as Brad Groux suggests, or you could set a Startup Form in Access, and have this form run that code. The downside of the startup form is that the code would ALWAYS run when you start the database, so that may not be the best way to do it.

You could also move that code to a Standard Module (it's obviously in a Form module now), and then create a macro that would call the new Function or Sub. For example, if you create a new standard module named basImport, and include a function like this:

Public Sub ImportMasterQry()
  DoCmd.TransferText acExportDelim, "", "masterQry", "F:\Folder\Master" & Format(Date, "yymmdd") & ".csv", True, ""
End Sub

Open in new window

You could then create a macro that calls that Sub - let's say you name the macro "mcrMasterQuery". You could then use Scheduler to run that query:

"full path to msaccess.exe" "Full path to your database" /X mcrMasterQuery

This is known as a "command line switch". See this article for more information on that:

One thing to remember - even if you're specifying a Macro to run via the command line, any code you have in a startup form, or in an AutoExec macro in your database, will run when you fire this off from a scheduled task.
Jim Dettman (Microsoft MVP/ EE MVE)President / OwnerCommented:
Check out the following Micro Tutorial:

MS Access – Different Ways to Start Up a Database

everything there has already been mentioned, but it might help if you see it in a different way.

ferguson_jeraldAuthor Commented:
Thanks for the assistance.  I created a macro to launch the code, and it worked as intended.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.