subscript out of range

John Sh

Member
Local time
Today, 19:26
Joined
Feb 8, 2021
Messages
513
I have an array with 3 elements.
Using the code below, if I call getargs(Args, 4), the code runs through the "if /else/end if" section error free but returns "subscript out of range" when it encounters the "IIf() statement.
Is this a peculiarity of arrays, that they don't like the iif() statement or is there something else at play here?

Code:
Private Sub Command100_Click()
    Dim Args As String
    Dim str As String
    Args = "tablelegs~Windows~letter box"
    str = getArgs(Args, 4)
End Sub

Private Function getArgs(Args As String, nNum As Integer) As String
    Dim aArgs() As String
    nNum = nNum - 1
    aArgs() = Split(Args, "~")
    If nNum > UBound(aArgs) Then
        getArgs = "xxx"
    Else: getArgs = aArgs(nNum)
    End If
MsgBox getArgs
    getArgs = IIf(nNum > UBound(aArgs), "", aArgs(nNum))
End Function
 
Arrays are zero-based by default. If array has 3 elements their index values are 0 - 2.

Why are you passing 4 to the nNum argument?

This line errors:

getArgs = IIf(nNum > UBound(aArgs), "", aArgs(nNum))

because both parts of IIf() must be able to evaluate, aArgs(nNum) fails when it gets index 3.
 
Last edited:
because both parts of IIf() must be able to evaluate, aArgs(nNum) fails when it gets index 3.
I thought it might be something like that.
I am testing out a bit of code to parse out openargs and the 4 was to see what happened when the number - 1 exceeded the element count.
I'll just stick with the if - end if construct.
Thank you for a swift reply.
John
 
Will there always be 3 parts to this string separated by ~ character? If you only want 3rd element, don't need an array.
 
Will there always be 3 parts to this string separated by ~ character? If you only want 3rd element, don't need an array.
The number of elements will vary from 1 to 3 and possibly more as I develop the code, so an array is the most versatile way to go.
 
You may find this function a generalised useful method to handle/construct arrays from strings - it is easy to adapt - both the delimiter and the max size of the array.

Code:
Public Function getOpenArgsArray(StrOpenArgs As String)

'   To pass more than one value to a form or report using the openargs parameter
'
'   Sample: from the form/button to open a report or form:
'   Dim strOpenArgs as string
'   Define the string (the pipe char can be any char not passed in the openarg values: set in this routine -
'   strOpenArgs = Me.ctlname + "|" + me.ctlname2 or any other string value that needs to be passed
'   Numeric and dates need to be coerced to strings to be passed as strings
'   use the command to pass strOpenArgs in the commandline
'       DoCmd.OpenReport "rptname", acViewPreview, , strWhere, , strOpenArgs
'   In the Load procedure of the Report or Form
'   Dim strOpenArgs as string
'   Dim strValues() As String
'   strOpenArgs = Me.OpenArgs
'   strValues = getOpenArgsArray(strOpenArgs)   'call function to parse the string
'   Values passed to this report/form: Value for RecordId for FinYr (for eg), SQL for Where clause
'   eg Me!SelectFinYr.Caption = strValues(0) - reference values from the Array (0..6) as currently set
'   eg Me!SelectCriteria.Caption = "Criteria: " + strValues(1)  etc. as needed
'
'*******************************************************************************

On Error GoTo myError
    
     Dim strInput As String
' Allows up to 7 values to be passed - increase if needed

     Dim StrArgs(7) As String
     Dim strBuildString As String
     Dim i As Integer
     Dim argCounter As Integer
     argCounter = 0
    
     '********
     'Iterate through input string:
         For i = 1 To Len(StrOpenArgs)
             Dim strChr As String
             strChr = Mid(StrOpenArgs, i, 1)
             'Build sub string of input string: | is used as the delimiter
             If Not strChr = "|" Then
                 strBuildString = strBuildString + strChr
             Else
                 'Save completed substring to array element:
                 StrArgs(argCounter) = strBuildString
                 strBuildString = ""
                 argCounter = argCounter + 1
             End If
         Next i
        
    'Save the final substring to the last array element:
    StrArgs(argCounter) = strBuildString
    
    getOpenArgsArray = StrArgs
    
leave:
    Exit Function
        
myError:
    MsgBox Error$
    Resume leave
    
End Function
 
You may find this function a generalised useful method to handle/construct arrays from strings - it is easy to adapt - both the delimiter and the max size of the array.
Thanks for that but I think what I have does the job.
I will never have cause to alter the code as the array is self limiting and as long as I use the same delimiters it will continue to work. Also the error checking is built in.

Code:
Public Function getArgs(Args As String, nNum As Integer) As String
    Dim aArgs() As String
    nNum = nNum - 1
    aArgs() = Split(Args, "~")
    If nNum > UBound(aArgs) Then
        getArgs = ""
    Else: getArgs = aArgs(nNum)
    End If
End Function
 

Users who are viewing this thread

Back
Top Bottom