Additional starting parameters

How do I write code, so that I can identify additional starting parameters?

If I, in Run, write: MyApp.EXE a_string -a -b -f:1000
Then when the app starts, I want to be able to write code that responds to the parameters
E.g. A messagebox that displays the "a_string" string, and so on...

Get the hint?

BTW: An example would be nice!
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

cognitionConnect With a Mentor Commented:
Something like the following.

Dim sString as String
Dim nPos as integer
Dim Param()
Dim i as integer
Dim j as start
sString = Command
j = 1
While Instr(j, sString, "-") > 0 then
    i = Ubound(Param) + 1
    Redim Preserve Param(i)
    Param(i) = Mid(j, sString, nPos - j)
    j = i + 1

' Takes a string and and gets the next word, and removes the word from the string
' If word to get then return the word after the comma - wordtoget comma
Public Function GetNextWord(sString As String, Optional sSeperator As String = ",", Optional WordToGet As Integer = 1) As String
Dim nPos As Long
If IsMissing(sSeperator) Then
    sSeperator = ","
End If
If Left(sString, 1) = sSeperator Then
    GetNextWord = ""
    sString = Mid(sString, 2)
    nPos = InStr(1, sString, sSeperator)
    If nPos > 0 Then
        GetNextWord = Left(sString, nPos - 1)
        sString = Mid(sString, nPos + Len(sSeperator))
        GetNextWord = sString
        sString = ""
    End If
End If

GetNextWord = Trim(GetNextWord)
End Function

Use the Command$ internal variable to get the command line. For parsing, you can use this code:

'PURPOSE:   Splits a string to tokens
'   IN szString:        the string to tokenise. Should be the string itself on the first call,
'                       and "" on the following calls.
'   IN szSeparators:    The characters that act as separators between the tokens in the
'                       string, can be more then 1 character
'RETURNS:   The next token in the string, or "" if nothing is left
'NOTES:     This uses static variable, hence only 1 string can be tokenised at one time.
'           Passing a string as the first parameter overrides previos calls, and the string
'           processing starts over. For more info, see help on 'strtok' in the C library reference
Public Function strtok(szString As String, szSeparators As String) As String
    Static szBuf As String  'Static buffer to store the string
    Dim I As Integer
    Dim iPos2 As Integer
    Static iPos As Integer
    Dim iTmp As Integer
    Dim iPos1 As Integer
    Dim bSet As Boolean
    If szString <> "" Then szBuf = szString     'Update to a new string
    If iPos = 0 Then iPos = 1
    iPos2 = iPos
    iTmp = Len(szSeparators)
    'Iterate through all the separators, and find the position of the closes next occurence
    'of any of them
    For I = 1 To iTmp
        iPos1 = InStr(iPos2 + 1, szBuf, Mid(szSeparators, I, 1))
        If iPos1 > 0 Then
            If Not bSet Then
                iPos = iPos1
                If iPos1 < iPos Then iPos = iPos1
            End If
            bSet = True
        End If
    Next I
    'If no more separators, means the string has ended, return all of it
    If Not bSet Then
        strtok = Right$(szBuf, Len(szBuf) - iPos)
        iPos = Len(szBuf)
        Exit Function
    End If
    If iPos2 = 1 Then
        strtok = Mid(szBuf, iPos2, iPos - iPos2)
        strtok = Mid(szBuf, iPos2 + 1, iPos - iPos2 - 1)
    End If
End Function

Hehe, I guess I was too slow :)
Olli083097Author Commented:
cognition: Thanx!

MikeP: hehe, jepp, too slow :-)

BTW: Here is what I used, it's from the VB help file:

Option Explicit

Private Sub Form_Load()
    Dim execCommand
    For Each execCommand In GetCommandLine
        If execCommand <> "" Then MsgBox execCommand
End Sub

Function GetCommandLine(Optional MaxArgs)
    'Declare variables.
    Dim C, CmdLine, CmdLnLen, InArg, i, NumArgs
    'See if MaxArgs was provided.
    If IsMissing(MaxArgs) Then MaxArgs = 10
    'Make array of the correct size.
    ReDim ArgArray(MaxArgs)
    NumArgs = 0: InArg = False
    'Get command line arguments.
    CmdLine = Command()
    CmdLnLen = Len(CmdLine)
    'Go thru command line one character
    'at a time.
    For i = 1 To CmdLnLen
        C = Mid(CmdLine, i, 1)
        'Test for space or tab.
        If (C <> " " And C <> vbTab) Then
            'Neither space nor tab.

            'Test if already in argument.
            If Not InArg Then
            'New argument begins.
            'Test for too many arguments.
                If NumArgs = MaxArgs Then Exit For
                NumArgs = NumArgs + 1
                InArg = True
            End If
            'Concatenate character to current argument.
            ArgArray(NumArgs) = ArgArray(NumArgs) & C
            'Found a space or tab.

            'Set InArg flag to False.
            InArg = False
        End If
    Next i
    'Resize array just enough to hold arguments.
    ReDim Preserve ArgArray(NumArgs)
    'Return Array in Function name.
    GetCommandLine = ArgArray()
End Function
All Courses

From novice to tech pro — start learning today.