Results 1 to 4 of 4

Thread: How to SOMETIMES pass an array to a function

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2010
    Posts
    764

    How to SOMETIMES pass an array to a function

    I need to pass an array to a function.
    However, sometimes I don't want that to happen.

    In other words, I need to write a function that should look at the input array to see if it exists, do something, and if it doesn't exist, do other things.

    Currently, I have written something like this:
    Code:
    Private Sub Command15_Click()
       
       Dim XArray()      As String
       Dim N           As Long
       
       N = proc1_Command15("abc", Null)
       
    End Sub
    Code:
    Private Function proc1_Command15(ByRef XVal As String, ByRef XArray() As String) As Long
       
       Dim L       As Long
       Dim s       As String
       
       If IsNull(XArray()) Then
          s = "XArray is null. Nothing added!"
       Else
          L = Array_Length(XArray())
          
          ReDim Preserve XArray(L) As String
          XArray(L) = XVal
          
          s = "XVal added." & vbCrLf & vbCrLf & "Number of records:" & vbTab & (L + 1)
       End If
       
       MsgBox s
       
    End Function
    But, that doesn't work.
    It gives me a compile error (doesn't compile)

    I am not sure if the concept of Null that I am using is the right concept to use in this case or not, and if not, then how to do what I am trying to do.

    Please help.
    Thanks.

  2. #2
    Frenzied Member
    Join Date
    Jun 2015
    Posts
    1,068

    Re: How to SOMETIMES pass an array to a function

    try something like this

    Code:
    Private Sub Form_Load()
        
       Dim XArray()    As String
       Dim N           As Long
       
       N = proc1_Command15("abc")
       proc1_Command15 "111", XArray
       proc1_Command15 "222", XArray
       Debug.Print "Xarray contents: " & Join(XArray, ",")
       
    End Sub
    
    Private Function proc1_Command15(ByRef XVal As String, Optional ByRef XArray As Variant) As Long
       
       Dim L       As Long
       Dim s       As String
       On Error Resume Next
       
       If IsArray(XArray) Then
          L = UBound(XArray)     'error if empty
          
          If Err.Number = 9 Then 'subscript out of range array empty
             ReDim Preserve XArray(0)
          Else
             ReDim Preserve XArray(L + 1)
          End If
          
          XArray(UBound(XArray)) = XVal
          
          s = "XVal added. records: " & UBound(XArray)
       Else
          s = "XArray not given. Nothing added!"
       End If
       
       Debug.Print s
       
    End Function

  3. #3
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: How to SOMETIMES pass an array to a function

    Look up how to use the keyword OPTIONAL...You want an optional parameter in this ccase.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    New Member L'mars's Avatar
    Join Date
    Mar 2021
    Location
    Bolivia
    Posts
    5

    Re: How to SOMETIMES pass an array to a function

    Hello
    Visit the next webs
    They uses the param PARAMARRAY on functions for arrays

    https://analystcave.com/vba-tip-of-the-day-paramarray/

    http://www.vb-helper.com/howto_param_array.htmlhttp://www.vb-helper.com/howto_param_array.html

    http://www.tushar-mehta.com/publish_...ramArray.shtml

    Bye

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width