• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1168
  • Last Modified:

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

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.
0
rezasaha
Asked:
rezasaha
1 Solution
 
Jim Dettman (Microsoft MVP/ EE MVE)President / OwnerCommented:
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.

JimD.  

' 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

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

End Function
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now