Results 1 to 2 of 2
  1. #1
    Join Date
    Aug 2003
    Location
    Saudi Arabia
    Posts
    9

    Red face Unanswered: How can I pass a paramater to MS Access query through the command line

    How can I pass a paramater to MS Access query through the command line " C:\....\MSAccess.exe C:\....\MyAccess_database.mdb "

  2. #2
    Join Date
    Dec 2001
    Posts
    79
    Below is some information about passing parameters to Access via the command line from one of our recent newsletters. Once your parameter for the query is passed to the Access program, you can save it in a global variable, then write a simple function to return the value of that global variable. Include the simple function in your query to retrieve the value for use in your query.

    --------------------------------------------------------
    Access Tips - Making Use of Command-Line Parameters
    --------------------------------------------------------
    Have you ever wanted to have a quick and easy way to bypass the startup form and get directly to the Access object you want to open or edit?

    How about scheduling a set of reports to print at a certain time?

    Or, have you ever wanted to have an easy way to open just the administration form that no other user has access to on their switchboard menu?

    With a command-line processor you can do this and more to greatly increase the utility and functionality of your database.

    The "Command" function, a batch file or shortcut, and a little code is all you need to get going. In the example database available here:

    http://www.peterssoftware.com/cl97.zip

    ... you will see how we added the following code to the startup form OnOpen event procedure to see if a command line parm has been passed to our database:

    If Command = "" Then
    '* No command line parameters passed.
    Else
    '* There were parameters passed, so let's process them
    ng_ProcessCommandLine
    Exit Sub
    End If

    Then, the ng_ProcessCommandLine function parses all the command-line text after the "/cmd" command-line switch, looking for strings delimited by semi-colons. By passing a delimited string, you can provide any number of parameters to the command line processing function. The parsing works by calling the ever-useful xg_GetSubString function:

    '* Get individual parms from the command-line string
    strCLParm1 = xg_GetSubString(Command, 1, ";")
    strCLParm2 = xg_GetSubString(Command, 2, ";")
    strCLParm3 = xg_GetSubString(Command, 3, ";")
    strCLParm4 = xg_GetSubString(Command, 4, ";")
    strCLParm5 = xg_GetSubString(Command, 5, ";")

    Now, if we assume that the first parm, strCLParm1, contains a command that determines how the rest of the parameters will be processed, then we have a lot of flexibility. Ex.:

    Select Case strCLParm1
    Case "OpenReport"
    '* Open a report here
    Case "OpenForm"
    '* Open a form here
    Case "DoSomethingElse"
    '* Do something else here
    Case Else
    End Select

    You could put code here to open a set of weekly reports, to open a particular procedure in a module in design mode, or open a special administrator form.

    Then, to get this code to run, just specify the command-line in a shortcut or batch file that will cause the code to execute. Ex.:

    "C:\Program Files\Microsoft Office\Office\MSACCESS.EXE" "d:\My Documents\cl97.mdb" /cmd "OpenModule;MyModuleName;MyProcedureName"

    Or,

    "C:\Program Files\Microsoft Office\Office\MSACCESS.EXE" "d:\My Documents\cl97.mdb" /cmd "OpenReport;rptCustomers;Preview"

    Once you have the shortcut text, you can use Windows Task Scheduler to execute Access with the command line at a specific time of day, week, or month.

    See the example database here: http://www.peterssoftware.com/cl97.zip for more information, and for how to incorporate a command-line processor into your application.
    Peter De Baets
    Peter's Software - Microsoft Access Tools for Developers
    http://www.peterssoftware.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •