Passing Variable Into VB from Access '97

I'm passing variables into my VB app.  I've developed a way to parse the command$, but it's not working.  When I hard code the command value and test it works fine.

Now, I want to deug the command$ value, as it calls to the *.exe with variables.
(ie test.exe /cmd test1 test2 test3)

I guess my question is, is there away to pass the command variables to the vbp and run in debug?

Thanks in advance for your assistance.
Who is Participating?
mcriderConnect With a Mentor Commented:
An easy way to see what arguments are being passed to your program during runtime is to make this line the 1st executable line in your program (either your FORM_LOAD event or MAIN subroutine), compile it, and the run your program...

   MsgBox "Arguments passed: [" + Command + "]"

When the messagebox pops up, everything between the braces were passed to the program on the command line.

You can remove the line when you're satisfied that the program calling your program is passing arguments correctly.

Also, here are two parsing routines that work really well for parsing the command line... GetArgCount and GetArgs.

The GetArgCount function will return the number of arguments (like argc) or -1 if there are no arguments.

The GetArgs function will get the actual arguments (like argv) either as an array or a string by using the argument number.

To use GetArgs as an array, you can do this:

    Dim Element As Variant
    If Not GetArgCount() = -1 Then
        For Each Element In GetArgs()
            Debug.Print Element
    End If

To get the first argument string like the "C" call argv[1] you can do this:

    Dim MyString As String
    MyString = GetArgs(1)

To get the second argument string do this:

    MyString = GetArgs(2)

The GetArgs function also parses quoted argument as a single argument.  For example, let's say that argument 2 is supposed to be a filename, and you are passing long filenames on the command like this:

    MyProg.exe /b "C:\Program Files\My Dir\My Long File.txt"

and in your program you do:

    Dim MyString1 As String
    Dim MyString2 As String

Then MyString1 will contain: /b
and MyString2 will contain: C:\Program Files\My Dir\My Long File.txt

The quotes will also be stripped in MyString2.

Hope this helps!



    Function GetArgCount() As Long
        On Error Resume Next
        GetArgCount = -1
        GetArgCount = UBound(GetArgs()) + 1
    End Function
    Function GetArgs(Optional ArgNumber As Variant) As Variant
        Dim lArgs() As String
        Dim iVal As Long
        Dim jVal As Long
        Dim kVal As Long
        Dim lBuf As String
        GetArgs = "" 
        lBuf = Trim$(Command)
        iVal = 1
            iVal = InStr(iVal, lBuf, Chr$(34))
            If iVal = 0 Then Exit Do
            jVal = InStr(iVal + 1, lBuf, Chr$(34))
            If jVal = 0 Then Exit Do
            For kVal = iVal To jVal
                If Mid$(lBuf, kVal, 1) = " " Then Mid$(lBuf, kVal, 1) = Chr$(1)
            Next kVal
            iVal = jVal + 1
        iVal = 0
            If lBuf = "" Then Exit Do
            jVal = InStr(1, lBuf, " ")
            ReDim Preserve lArgs(iVal) As String
            If jVal = 0 Then
                lArgs(iVal) = lBuf
                lBuf = "" 
                lArgs(iVal) = Left$(lBuf, jVal - 1)
                lBuf = Trim$(Mid$(lBuf, jVal + 1))
            End If
            jVal = 1
                jVal = InStr(jVal, lArgs(iVal), Chr$(1))
                If jVal = 0 Then Exit Do
                Mid$(lArgs(iVal), jVal, 1) = " "
                jVal = jVal + 1
            If Left$(lArgs(iVal), 1) = Chr$(34) Then lArgs(iVal) = Trim$(Mid$(lArgs(iVal), 2))
            If Right$(lArgs(iVal), 1) = Chr$(34) Then lArgs(iVal) = Trim$(Left$(lArgs(iVal), Len(lArgs(iVal)) - 1))
            iVal = iVal + 1
        On Error Resume Next
        Err = 0
        If IsMissing(ArgNumber) = True Then
            GetArgs = lArgs
            If UBound(lArgs) >= ArgNumber Then GetArgs = lArgs(ArgNumber - 1)
        End If
    End Function

Go into project properties (in vb5 the Make tab) and put the value you want to test in the command line arguments entry, just debug as normal.
flinxAuthor Commented:
This is a partial answer.  It allows me to test the command$ parsing.  But, I also want to verify that my pass through command is working.
Never miss a deadline with

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

I don't understand what the problem is.. dhodge told you how to pass command line options to your application while in the VB IDE.  If you are having problems in being able to step through the code then you can put a Debug.Assert = True in the sub main() or in the Form_Load() (depending on how your application starts)
Are you saying you want to debug test.exe as well as the calling app.  If so you can run test.exe in the VB IDE then start another instance of VB to run the calling app.
flinxAuthor Commented:
Thank you, that is exactly what I was looking for.  Have a great day! :-)
Glad I could help! Thanks for the points!

All Courses

From novice to tech pro — start learning today.