Parse open args into three strings

foxtrot123

Registered User.
Local time
Today, 11:38
Joined
Feb 18, 2010
Messages
57
I have an open args statement with three values, separated by pipes ("|").

In the on load event of a form, I need to parse out the three values as strings. The following works if there are just two arg values. How can I change it so it works with three?

Code:
   Dim intPos As Integer
    Dim strFirstArg As String
    Dim strSecondArg As String

If Len(Me.OpenArgs) > 0 Then
        ' Position of the pipe
        intPos = InStr(Me.OpenArgs, "|")
            If intPos > 0 Then
                ' Retrieve value from the first part of the string
                strFirstArg = Left$(Me.OpenArgs, intPos - 1)
                ' Retrieve value from the middle of the string
                strSecondArg = Mid$(Me.OpenArgs, intPos + 1)
                ' Retrieve value from the end of the string
                [COLOR=Red]* strThirdArg = ???? [/COLOR]

                ' Assign the values to the control                               
                Me.FirstArg = strFirstArg
                Me.SecondArg = strSecondArg
                * Me.ThirdArg = strThirdArg
            End If
    End If
 
Last edited:
Take a look at the Split function which will parse your string into an array for a given delimiter.
 
alternatively, instead of passing openargs, just store the relevant data in a public structure
 
Try;
Code:
   Dim intPos As Integer
    Dim strFirstArg As String
    Dim strSecondArg As String

If Len(OpenArgs) > 0 Then
        ' Position of the first pipe
        intPos = InStr(OpenArgs, "|")
            If intPos > 0 Then
                ' Retrieve value from the first part of the string
                strFirstArg = Left(OpenArgs, intPos - 1)
                 'Reset OpenArgs by removing first portion of string
                 OpenArgs = Right(Openargs, Len(openargs) - intPos )
                 'Reset intPos
                 intPos = InStr(OpenArgs, "|")
                ' Retrieve value from the end of the string
                strSecondArg = Left(OpenArgs, intPos - 1)
                 strThirdArg = Right(Openargs, Len(OpenArgs) - intPos )

                ' Assign the values to the control                               
                Me.FirstArg = strFirstArg
                Me.SecondArg = strSecondArg
                 Me.ThirdArg = strThirdArg
            End If
    End If
 
Got it! Used a slightly different approach:

Dim x As Variant
Dim strFirstArg As String
Dim strSecondArg As String
Dim strThirdArg As String

If Len(Me.OpenArgs) > 0 Then
'Split creates a zero-based array from the input string
x = Split(Me.OpenArgs, "|")
strFirstArg = x(0)
strSecondArg = x(1)
strThirdArg = x(2)

Me.FirstArg = strFirstArg
Me.SecondArg = strSecondArg
Me.ThirdArg = strThirdArg
End If
 
that is ok, only if you do have (at least) two pipe symbols

you probably ought to check that ubound(x) = 2 before trying to assign x(1) and x(2) or you may get a run time error.
 
that is ok, only if you do have (at least) two pipe symbols

you probably ought to check that ubound(x) = 2 before trying to assign x(1) and x(2) or you may get a run time error.

I think the three openargs values will always be there: two of them reference values on the form (which will always be there) and one is a word hard coded into the openargs statement.

But just in case, how would I check that (x) = 2? Something like:

If X < 2 Then
' exit
Else
' run code
End If
 
maxindex = ubound(x)

will show you the highest value index in the split array (zero bound)

if maxindex = 0 there were no pipe symbols, and you only have one entry in the array, etc.
 
The main point is, are you always going to have 2 pipe symbols when you concatenate the OpenArgs?

i.e. Me.OpenArgs = txtbox1 & "|" & txtbox2 & "|" & txtbox3

Is that what it will always be? If yes, then everything should be ok.
 

Users who are viewing this thread

Back
Top Bottom