[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3680
  • Last Modified:

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.

2 Solutions
Brad GrouxCommented:
Create a macro in to execute the query.
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: http://support.microsoft.com/kb/209207

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)PresidentCommented:
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.

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now