473,612 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Invoking a Database From the Command Line

NeoPa
32,566 Recognized Expert Moderator MVP
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.E xe".
  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, 428 views)
Sep 2 '09 #1
0 17534

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

Similar topics

4
10313
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 there are some function with which I can redirect the output?
5
4588
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, password are all defaulted to 'None'. On my own machine running Mac OSX 10.3, I can connect using the following: host = 'localhost' database = '<my path to FB database>'
5
2597
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? Also, I'm currently using a windows forms application but I would like to use an ASP.net appliocation to invoke the .exe file in the future. Is this possible and will it cause any complications?
19
2020
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 very few explanations on the subject. setenv ("REQUEST_METHOD", "GET", !0);
1
1432
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 would like to be able to invoke the packaging of this MSI file from the command line, so that I can embed it in a script. Anyone know what this command line statement would be?
14
3523
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 is the DB2 tool that uses this dll, in order to analyze the db2diag.log? thank you in advance for help (and please, excuse my bad english) Jo --
0
1310
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. Thanks in advance.
1
1814
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 feature would greatly simplify my life, then I am not opposed) I currently have an application that is single threaded that does batch work (you pass it a file in the command line, it runs for 2-45 minutes and spits out another file). I do not...
12
3928
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 p5pe-vm motherboard and an Intel Pentium D 3.0Ghz processor, compared to the old one where we uses asrock motherboard and AMD Duron. Both has the same version of mysql installed... To summarized, both machine has the same configuration except...
6
1875
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 command "shell c:\winamp\winamp.exe blah-blah.mp3". It works nicely. I can use this database from a laptop, either by copying the .mdb file to the laptop or by clicking a shortcut that points to the .mdb file on the main machine. The file play...
0
8105
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8605
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8565
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8246
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8415
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7039
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6076
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5532
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4109
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.