Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Additional starting parameters

Posted on 1999-01-11
5
Medium Priority
?
146 Views
Last Modified: 2010-05-03
How do I write code, so that I can identify additional starting parameters?

E.g.
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!
0
Comment
Question by:Olli083097
  • 2
  • 2
5 Comments
 
LVL 3

Accepted Solution

by:
cognition earned 400 total points
ID: 1455448
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
Wend

0
 
LVL 3

Expert Comment

by:cognition
ID: 1455449
' 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)
Else
    nPos = InStr(1, sString, sSeperator)
    If nPos > 0 Then
       
        GetNextWord = Left(sString, nPos - 1)
        sString = Mid(sString, nPos + Len(sSeperator))
    Else
        GetNextWord = sString
        sString = ""
    End If
End If

GetNextWord = Trim(GetNextWord)
   
End Function

0
 
LVL 8

Expert Comment

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

'
'PURPOSE:   Splits a string to tokens
'ARGUMENTS:
'   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
               Else
                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)
      Else
        strtok = Mid(szBuf, iPos2 + 1, iPos - iPos2 - 1)
    End If
End Function

0
 
LVL 8

Expert Comment

by:MikeP090797
ID: 1455451
Hehe, I guess I was too slow :)
0
 

Author Comment

by:Olli083097
ID: 1455452
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
    Next
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
        Else
            '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
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question