Pass arguments to Access 2000 via command line

Is there any way to pass an argument into an Access MDE application via a command line?
We have a .NET application that needs to shell out to Access and open a form in our own Access 2000 application on a given record, so we need to pass the order# into the form while loading the .MDE app.

One solution, which we do not want to use, is to write out the order# to a temp table and then have the Access app read from that table when it opens because of multi-user issues.

Who is Participating?
Jim Dettman (Microsoft MVP/ EE MVE)President / OwnerCommented:
Here's a couple handy functions.  Note that you will need to modify slightly (error handler).


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 entry into system log file.
Function LogSystemEvent(intEventType As Integer, strEventMessage As String) As Integer

    Const RoutineName = "LogSystemEvent"
    Const Version = "2.0"

    Dim db As DAO.Database
    Dim rst As DAO.Recordset

10  On Error GoTo LogSystemEventError

20  LogSystemEvent = True

    ' Add record to event table
30  Set db = CurDb()
40  Set rst = db.OpenRecordset("tblSystemLog", dbOpenDynaset)
50  rst.AddNew
60  rst![Type] = intEventType
70  rst![Message] = strEventMessage
80  rst.Update

90  On Error Resume Next

100 rst.Close
110 Set rst = Nothing

120 Set db = Nothing

130 Exit Function

140 UnexpectedError ModuleName, RoutineName, Version, Err.Number, Err.Description, Err.Source, VBA.Erl
150 LogSystemEvent = False
160 Resume LogSystemEventExit

End Function

Open in new window

Jim Dettman (Microsoft MVP/ EE MVE)President / OwnerCommented:
Use /CMD followed by the arguments.  Best if it is the last switch used.   Within Access, use Command$ to get the arguments passed.

Here is a link to the relevant Help entry -

The article omits a salient point and that is how to retrieve the /cmd value.  Another source (which must remain nameless) suggested using Command() to retrieve the value that follows /cmd.
rfmassaAuthor Commented:
Thank you!
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.

All Courses

From novice to tech pro — start learning today.