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

Posted on 2007-08-08
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 56

    Accepted Solution

    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
    Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
    Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now