Solved

Additional starting parameters

Posted on 1999-01-11
5
138 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 100 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now