473,394 Members | 2,031 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,394 developers and data experts.

Invoking a Database From the Command Line

NeoPa
32,556 Expert Mod 16PB
Introduction:

Sometimes, it may be necessary to open a database from the command line, or even from a shortcut. Often, in such cases, a specific routine is required to be run in this mode, yet when opened normally, the database should not execute this same code. We will cover both of these scenarios in this short tutorial.

When we've covered that, we will look at scheduling a specific database to open, execute some code, and close, at a predetermined time. The scheduler, by default, has no user credentials associated with it (It runs by default as the SYSTEM account), and this tutorial will not cover changing that, as it is a deeper issue with associated ramifications. What this means, in effect, is that networked resources are generally unavailable to normal scheduled processes.


Command Line & CMD File:

A command line can be set up that invokes Access in conjunction with an individual database, as well as a parameter to be passed to it if required. We'll get onto that later. Such a command (single) can be associated with a shortcut, or even a scheduled task, all by itself. More complex sets of instructions however, need either to be included as code invoked by the database itself (a possible yet rather clumsy solution), or grouped together into a CMD file which itself need only be invoked in a variety of standard ways (Command Line; Start / Run; Shortcut; Scheduler; From within some VBA code; etc).

As a simple example of the sorts of things that can be done in a CMD file I include one I use. I've doctored real-life sensitive data but the format's the same.
NB. As the network address used here (Line #5) is actually the local machine, even the SYSTEM account can run this successfully.
Expand|Select|Wrap|Line Numbers
  1. REM V: Drive should be available.
  2. REM Make sure it is added if not.
  3. REN V:\Vecta\Imports\Done Done
  4. IF NOT ERRORLEVEL 1 GOTO Cont1
  5. NET USE V: \\STP01NTS003\Vecta
  6.  
  7. :Cont1
  8. CD /D V:\Vecta
  9. IF NOT ERRORLEVEL 1 GOTO Cont2
  10. ECHO Unable to map correct drive.
  11. GOTO EndCmd
  12.  
  13. :Cont2
  14. REM Start Access stuff here
  15. REM The AccessPath Environment Variable must be set for this to work.
  16. "%AccessPath%\MSAccess.Exe" V:\Vecta\Vecta.Mdb ;Auto
  17.  
  18. CD Imports
  19. REN Vecta*.Csv *.*
  20. IF NOT ERRORLEVEL 1 GOTO Cont3
  21. ECHO No files to process.
  22. GOTO EndCmd
  23.  
  24. :Cont3
  25. REM WinZip CMDLine must be installed for this to work.
  26. "C:\Program Files\WinZip\WZZip.Exe" -a+ -ex -ybc StatPlusVecta.Zip *.Csv
  27. DEL *.Csv
  28. START "Upload to {remote server}" /DV:\Vecta\Imports /LOW FTP.Exe -n -s:V:\Vecta\VectaWebDaily.Ftp -w:16384 {DummyFTPAddress}
  29.  
  30. :EndCmd
  31. REM PAUSE
  32. ECHO ON
I don't plan to go through this in detail as it is only to give a flavour of what's possible, however this, in it's undoctored state, works perfectly as a scheduled task. Line #16 is an example of invoking Access with an associated database and also passes a parameter to that database.


Starting Access into a Database:

To handle calling Access and passing both the database AND the parameter to the database, you need a command with essentially 4 elements in it :
  1. The address of the Access executable itself. If it is in the Path Environment Variable then this is not necessary, but it's best not to assume that. It generally isn't in my experience. This value may be got by enquiring of the registry, but I find that setting an Environment Variable for each PC where this is to be used works fine. Somehow or another, you need to handle this requirement whatever.
  2. The name of the executable file itself. Generally and simply "MSAccess.Exe".
  3. The name of the database to be passed.
  4. The parameter you want passed to the database. This should follow a ";" character (This is a shortcut for /cmd). In the CMD example (Line #16) above the parameter is "Auto".
If the fourth element (the parameter) isn't passed the database should open normally for full interaction with the user.
A useful reference for invoking Access can be found at How to use command-line switches in Microsoft Access.


Designing the Database to Handle the Parameter:

There are two ways for a database to run code automatically upon opening :
  1. An AutoExec macro. I would never personally, recommend using macros in grown-up software, whatever they try to foist upon you in Access 2007.
  2. In the Tools / Startup form there is the option of selecting a form to run automatically open opening of the database. The chosen form should contain an OnOpen event procedure. This runs automatically when the form is opened, hence also when the database is opened.

In my example code which follows, I check the Command() function to see the passed parameter, and execute specific code if it turns out to be the predefined value "Auto".
On Line #4 I invoke a procedure which is also used when the database is opened normally, hence the parameter (varOpenArgs) which is passed to tell the procedure thet we are running in Auto mode. Lines #5 & #6 ensure that the normal processing of the form (showing on the screen and awaiting operator input) are to be ignored, and that the database should close down upon completion of the process. This particular database is used with the scheduler and anything along the lines of operator intervention would fail as there will be none available to intervene.
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Open(Cancel As Integer)
  2.     'Some standard stuff
  3.     If Command = "Auto" Then
  4.         Call clsTo.ShowForm(strTo:="frmUpdate", varOpenArgs:="Auto")
  5.         Cancel = True
  6.         Call Application.Quit(Option:=acQuitSaveNone)
  7.     End If
  8. End Sub

Scheduling:

Although there are other methods available, I will stick here with the basic NT Scheduler which has been around for many versions of Windows NT (Inc 3.0; 3.5; 4.0; 2000; XP; Vista; etc). A useful reference for the Command Line Scheduler (AT) can be found at How To Use the AT Command to Schedule Tasks.

My command line follows. I will explain the elements used, below that :
Expand|Select|Wrap|Line Numbers
  1. AT 2:30 /INTERACTIVE /EVERY:M,T,W,Th,F,S "D:\Vecta\VectaWebDaily.Cmd"
  1. AT.EXE is the command scheduler itself.
  2. 2:30 specifies the task should run at that time in the morning on those days it runs.
  3. /INTERACTIVE allows any (presumably insomniac) operator on the console to see and interact with the task. Relying on interaction is highly dubious, though for testing purposes (and possibly debugging if it errors) it is a handy option.
  4. /EVERY:M,T,W,Th,F,S means each specified day of the week. The reference above lists the code for each day.
  5. The executed task should always be in quotes. The commands available are not restricted to CMDs. Pretty well any executable command can be used here.


Further Comment:

In the typical setup where a CMD file is used to invoke the Database process, it can also be used to do further processing on any files output or updated by the Database process.

To accompany this tutorial I knocked up a very small example (attached).

Although it's small it does include all the elements mentioned.

OpenAuto.Cmd :
Expand|Select|Wrap|Line Numbers
  1. @ECHO OFF
  2. REM The database is opened from this CMD file.
  3. REM Before running, the Environment Variable AccessPath should be set.
  4. REM Without this the process will abort.
  5. REM EG SET AccessPath=C:\Program Files\Microsoft Office\OFFICE11
  6. REM The access database (OpenAuto.Mdb) should be in the default db folder.
  7. REM To use a different folder than Access's default, specify it explicitly.
  8.  
  9. IF "%AccessPath%" == "" GOTO ERR_NAP
  10. "%AccessPath%\MSAccess.Exe" OpenAuto.Mdb ;Auto
  11. ECHO This should have run and terminated automatically.
  12. GOTO END
  13.  
  14. :ERR_NAP
  15. ECHO AccessPath not set!
  16.  
  17. :END
  18. ECHO Hit any key to continue.
  19. PAUSE
  20. ECHO ON
Form_Open() :
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Private Sub Form_Open(Cancel As Integer)
  5.     If Command() = "Auto" Then
  6.         Call MsgBox("The database is opened automatically, " & _
  7.                     "and will close as soon as you click on OK", _
  8.                     vbOKOnly, _
  9.                     "AutoMessage")
  10.         Cancel = True
  11.         Call Application.Quit(acQuitSaveNone)
  12.     End If
  13. End Sub
Attached Files
File Type: zip OpenAuto.Zip (10.0 KB, 427 views)
Sep 2 '09 #1
0 17480

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Tian | last post by:
In Windows, I have been simply using os.system() to run command line program in python. but there will be a black console window. How can I run the program without invoking that window? i guess...
5
by: Maurice LING | last post by:
Hi, I've been using FB1.5 and access the database using Kinterbasdb + Python. My connection is established using kinterbasdb.connect() method and the parameters host, dns, database, user,...
5
by: Simon Harvey | last post by:
Hi, I have a simple .exe application that I need to invoke from my C# application. I need to send it two strings and then get the result back. Can anyone advise me on how this is done? ...
19
by: Bush is a Fascist | last post by:
Hi all, Nobody in the php forum seems to know how to invoke php properly from C, so I must ask here. I am doing is the following, which seems to be by the book, but then again I am finding...
1
by: Wade | last post by:
Hey all, I have a setup / deployment project that is used to create builds for my web project. I can very easily invoke this manually, in the IDE, to get the packaged MSI file. However, I...
14
by: Gio Galma | last post by:
how I can invoke the db2diag utility in a Windows environment? in my D:\Program Files\IBM\SQLLIB\BIN directory I can see only db2diag.dll, and it seems there isn't the command line version; which...
0
by: CAG | last post by:
Hi, I'm trying to call the c# dll from command line & also want to invoke the methods of dll from command line directly. I'm working on c# .net project. Can anyone help me. ...
1
by: Luc The Perverse | last post by:
Hello! You may remember me as the guy who's trying to learn C# and using that old version 2003 of visual studio. (Although free version of command line .NET compiler is available, so if some new...
12
by: grace | last post by:
i am wondering why my database retrieval becomes too slow...we set up a new server (ubuntu, breezy badger) machine where we transferred all our files from the old server.. Our new server uses Asus...
6
by: John O'Flaherty | last post by:
Hello. I have an ms acess database that I use to track my mp3 files. I have included code that lets me select and play files by invoking an mp3 player (Winamp). This is done by using visual basic...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.