[AccessD] Retrieve command line arguments

Jim Dettman jimdettman at verizon.net
Fri Jan 25 12:29:55 CST 2008


Rocky,

 Below is code to grab the value you pass in with /cmd on the command line.

 Call GetCommandLine() in your startup form.  You can optionally pass an
argument on the number of parameters you expect to see.  If you don't, it
assumes up to 10.  The procedure assumes a space or tab delimits an argument
unless it's within quotes.

 Once you've done that, then anytime in the app call GetCommandLineArg()
with the argument number to get it's value.

 Here's an example of that:

          ' Main processing loop for app
          ' Check command line passed:
          ' P1 - Job/Customer:
          '      'ALL' - Process all defined jobs (Customers).
          '      '<job number>' as per table tbl_Init
          '
          ' P2 - Document Type:
          '      'ALL' - Process all document types.
          '      '<EDI Doc Number>' - Process only for this doc type
          Const RoutineName = "AppMain"
          Const Version = "1.0"

          Dim varJob As Variant
          Dim varDocType As Variant
          Dim intJobFound As Integer
          Dim rsJobs As ADODB.Recordset
          Dim intRet As Integer

10        On Error GoTo Error_AppMain

20        Call GetCommandLine(2)

30        varJob = GetCommandLineArg(0)
40        varDocType = GetCommandLineArg(1)
50        intJobFound = False
60        OpenADORecordset rsJobs, "Select * from tbl_Init order by ODBC"



 Note that I have not posted the UnexpectedError procedure, so you need to
change the error handling a bit.

HTH,
Jim.



' Used within this module
Private gvarCmdArgArray() As Variant

Public Sub GetCommandLine(Optional intMaxArgs As Integer)

        Const RoutineName = "GetCommandLine"
        Const Version = "3.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
        Dim intInQuotes 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
50      intInQuotes = False

        'Get command line arguments.
60      strCmdLine = Command()
70      strCmdLnLen = Len(strCmdLine)

        'Go thru command line one character at a time.
80      For intI = 1 To strCmdLnLen
90        strChr = Mid(strCmdLine, intI, 1)

          'Test for space or tab.
100       If (intInQuotes = False And strChr <> " " And strChr <> vbTab And
strChr <> Chr$(34)) Or (intInQuotes = True And strChr <> Chr$(34)) Then
            'Neither space, tab, or quote.
            'Test if already in argument.
110         If Not intInArg Then
              'New argument begins.
              'Test for too many arguments.
120           If intNumArgs = intMaxArgs Then Exit For
130           intNumArgs = intNumArgs + 1
140           intInArg = True
150         End If
            'Add character to current argument.
160         gvarCmdArgArray(intNumArgs - 1) = gvarCmdArgArray(intNumArgs -
1) + strChr
170       Else
            'Found a space, tab or quote.
            'Set intInArg flag to False.
180         intInArg = False
190         If strChr = Chr$(34) Then
200           intInQuotes = True
210         Else
220           intInQuotes = False
230         End If
240       End If
250     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)

260     For intI = 0 To intNumArgs - 1
270       gvarCmdArgArray(intI) = Trim(gvarCmdArgArray(intI))
280     Next intI

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 

-----Original Message-----
From: accessd-bounces at databaseadvisors.com
[mailto:accessd-bounces at databaseadvisors.com] On Behalf Of Rocky Smolin at
Beach Access Software
Sent: Friday, January 25, 2008 9:53 AM
To: 'Access Developers discussion and problem solving'
Subject: [AccessD] Retrieve command line arguments

 
Dear List:
 
I want to run a function in an application (emails out reports)
automatically.  I see that one way to do this is to use the /x switch on
startup to run a macro which will run a module which could have
Application.Quit at the end of it.  But I can't seem to get this to work.
And besides, I'm under the impression that macros are not PC anymore.
 
Anyway, if I can 'see' the startup switches on the command line that calls
access and starts the app,  I suppose I could intercept al the startup
housekeeping, run the reports, send the emails and exit.
 
Is there a way to see what the command was that started an app , IOW
retrieve the command line arguments ?  Or is there a better way to do this?

 
The automated email sender has to be part of the larger app - originally it
was split off for development purposes, but now, it's part of the design
that it should be integrated into the application.

MTIA
 
Rocky
 

 	
	
-- 
AccessD mailing list
AccessD at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/accessd
Website: http://www.databaseadvisors.com




More information about the AccessD mailing list