Link to home
Start Free TrialLog in
Avatar of sjmmajor
sjmmajor

asked on

Run a query by using the command prompt.

Is it possible to run a query from the command prompt as you can a macro?  If so, what would be the code?

Thanks in advance.

Avatar of Jim_Bob_Joe
Jim_Bob_Joe

I dont know of any ways of doing this on an Access database but with MSDE (MicroSoft Desktop Edition is a compact version of SQL Server and without the documentation) you can use the OSQL commander to run a query which can be returned to a file or the screen.

You could maybe think of changing your data to MSDE.

Jim
What are you trying to do?

We have a lot of databases that open via command prompt (or batch file), do something, then shut down.  
Batch processing, data refreshing, log file analysis....

Is that what you're trying to do?
Avatar of sjmmajor

ASKER

I want to be able to append data to a backup database and then delete that same data in the current database automatically.  I was thinking of using Microsoft Scheduler or another one to schedule this process every 2 days.  
You can't run queries from the command prompt, like startup arguments etc. But like you said, you can run macros from the command prompt. So why don't you just place all your queries that update the db into a macro and run it from batch/cmd prompt?

Another option would be to run a VBScript that opens the db, then runs your various tasks.

Do you need help with either of these options?

Cheers,
Mike
One more option would be:

- Open your db using the /cmd argument. Anything after /cmd (which must be the last arg) will be remembered by access while your db is open.

ex. "C:\Microsoft Office\Office\MSACCESS.EXE" C:\YourDB.mdb /cmd EE Rules

- Now you will need to setup an autoexec, or specify a macro to run when the db opens, that will look at the value of Command() to find the /cmd argument. Pass the value to a DoCmd like so...

1. Open db "C:\Microsoft Office\Office\MSACCESS.EXE" C:\YourDB.mdb /x QryMacro /cmd QryToRun
2. The macro QryMacro runs this function:

Function Submarine()
dim strQry$
strQry = Command()
DoCmd.Openquery strQry <--- This is where QryToRun is actually ran.
end function

So when you run the function "Submarine" from your macro, it will run the query that you specify in /cmd "YourQuery". Make sense?

Let me know...

Mike
Personally, I prefer setting a form to open on Startup.
The form has the OnTimer() event set to run the code that does what I want it to do, with a delay.
That way, I can open the database and close the form before it kicks off the task.
If you want an example database, I'll send one and you can modify it to your heart's content.

The /cmd thing sounds cool.  I didn't know about that.
You could use a switch statement to pick the action you wanted to do based on the command field.

If you need more details, let us know.
Just now got the chance to get back to this.  When I run the macro from the command prompt it just opens the database to that macro.  It doesn't run it.  Am I missing something?
Your syntax is probably incorrect, post you command line.

Mike
ASKER CERTIFIED SOLUTION
Avatar of GringoMike
GringoMike
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks GringoMike that worked.  I didn't have the quotes around the macro and I now put the whole path of access in there.