Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Can we pass a parameter to MS-Access through a command line

Posted on 2007-08-08
Medium Priority
Last Modified: 2013-12-20
Is there an easy way to pass a data parameter to a MS-Access file through command line:

For example: I want to type the command line:

C:\program files\Ms-office\MSACCESS.EXE  c:\My document\MyDatabase.MDB  PARAM=12345

Then when the command line is called, my Macro or Query will grab the PARAM and run my report for the data which the field=12345.
Question by:rezasaha
1 Comment
LVL 59

Accepted Solution

Jim Dettman (Microsoft MVP/ EE MVE) earned 2000 total points
ID: 19657827
Yes you can.  You need to use the /CMD switch and it needs to be the last switch used followed by the arguments you want to pass.  You then use the Command() function to retrieve the passed arguments as a string within Access.

   In addition, you might write something like the two procedures below to take care of handling multiple arguments within the app.  GetCommandLine is called at app startup and optionally checks the number of arguments.  GetCommandLineArg is called through out my apps to get a specific argument.


' Used within this module
Private gvarCmdArgArray() As Variant

Public Sub GetCommandLine(Optional intMaxArgs As Integer)
        Const RoutineName = "GetCommandLine"
        Const Version = "2.0"
        'Declare variables.
        Dim strChr As String
        Dim strCmdLine As String
        Dim strCmdLnLen As Integer
        Dim intInArg As Integer
        Dim intI As Integer
        Dim intNumArgs As Integer
        'See if intMaxArgs was provided.
10      If IsMissing(intMaxArgs) Then intMaxArgs = 10
        'Make array of the correct size.
20      ReDim gvarCmdArgArray(intMaxArgs - 1)
30      intNumArgs = 0
40      intInArg = False
        'Get command line arguments.
50      strCmdLine = Command()
60      strCmdLnLen = Len(strCmdLine)
        'Go thru command line one character at a time.
70      For intI = 1 To strCmdLnLen
80        strChr = Mid(strCmdLine, intI, 1)

          'Test for space or tab.
90        If (strChr <> " " And strChr <> vbTab) Then
            'Neither space nor tab.
            'Test if already in argument.
100         If Not intInArg Then
            'New argument begins.
            'Test for too many arguments.
110           If intNumArgs = intMaxArgs Then Exit For
120             intNumArgs = intNumArgs + 1
130             intInArg = True
140           End If
            'Add character to current argument.
150         gvarCmdArgArray(intNumArgs - 1) = gvarCmdArgArray(intNumArgs - 1) + strChr
160       Else
            'Found a space or tab.
            'Set intInArg flag to False.
170         intInArg = False
180       End If
190     Next intI
        ' Don't want to do this as some arguments may be optional.
        'Resize array just enough to hold arguments.
        'ReDim Preserve gvarCmdArgArray(intNumArgs - 1)

End Sub

Public Function GetCommandLineArg(intArgNumber) As Variant
        ' Returns an argument from the command line
        ' Null is returned on Error or non-existant argument
        Const RoutineName = "GetCommandLineArg"
        Const Version = "2.0"
10      On Error GoTo GetCommandLineArgError
20      If intArgNumber > UBound(gvarCmdArgArray()) Then
30        GetCommandLineArg = Null
40      Else
50        GetCommandLineArg = gvarCmdArgArray(intArgNumber)
60      End If

70      On Error Resume Next
80      Exit Function
90      UnexpectedError ModuleName, RoutineName, Version, Err.Number, Err.Description, Err.Source, VBA.Erl
100     GetCommandLineArg = Null
110     Resume GetCommandLineArgExit

End Function

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
In a use case, a user needs to close an opened report by simply pressing the Escape (Esc) key. This can be done by adding macro code in Report_KeyPress or Report_KeyDown event.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question