Results 1 to 9 of 9
  1. #1
    VSCurtis is offline Advanced Beginner
    Windows 7 64bit Access 2016
    Join Date
    Dec 2014
    Posts
    65

    Passing Arrays

    Hi Guys,



    I am having a bit of a problem. I have a function which passes an array by reference. The Array is unallocated. The purpose of the function is to increase the array size by one as needed by the program. Arrays passed by reference should reflect any changes made by the function. This is not happening. The redim and redim preserve modifications made by the function are only visible inside the function. Once my code exits the function and returns to the main body of code the array shows that it is still unallocated and produces and error 9 subscript out of range. The only way I can get the function to preform as expected is to pass the array out of the function. Not sure what's going on here. Any help would be appreciated. Here's the code.

    Code:
    Private Function Test(aImageArray As Variant) As Variant
    On Error Resume Next
       If IsArray(aImageArray) Then
          If IsError(LBound(aImageArray, 1)) Then
             Select Case Err.Number
                Case 9
                  ReDim aImageArray(0)
              End Select
           Else
              ReDim Preserve aImageArray(UBound(aImageArray) + 1)
           End If
        End If
        Debug.Print UBound(aImageArray)
        Test = aImageArray
        
    End Function
    When coded like this the code functions as I want, but I should not have to pass the array out in order for the array to be resized. What's going on?

  2. #2
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    I rarely use arrays, but Chip Pearson has info about arrays:

    Functions For VBA Arrays
    http://www.cpearson.com/excel/VBAArrays.htm

    Passing And Returning Arrays With Functions
    http://www.cpearson.com/excel/Passin...ningArrays.htm


    Or maybe https://msdn.microsoft.com/en-us/lib...v=vs.100).aspx



    Maybe these will help........

  3. #3
    VSCurtis is offline Advanced Beginner
    Windows 7 64bit Access 2016
    Join Date
    Dec 2014
    Posts
    65
    Yes I am aware of Chip's website. My function is based on one his. The function originally just checked to see if the array was allocated. Mine attempts to correct the issue. I leave the array unallocated so it can grow as needed according to the needs of the program. I will give the links you provided a look. Thanks

  4. #4
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,411
    You might also check out vba collections. I find them easier to understand and manage than arrays.
    http://alystcave.com/vba-collection/

  5. #5
    VSCurtis is offline Advanced Beginner
    Windows 7 64bit Access 2016
    Join Date
    Dec 2014
    Posts
    65
    Thanks for your info but the page cannot be displayed

  6. #6
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,411
    http://analystcave.com/vba-collection/

    Well, spelling counts...

  7. #7
    VSCurtis is offline Advanced Beginner
    Windows 7 64bit Access 2016
    Join Date
    Dec 2014
    Posts
    65
    Thanks for the info I'll check it out. I've used then before but it's been awhile.

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,919
    The only way I have made an array available to multiple procedures is to declare it as Global in general module. Then I don't have to 'pass' it. Any procedure can find the array. One procedure redims and sets the array elements. Another procedure uses the array elements.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  9. #9
    VSCurtis is offline Advanced Beginner
    Windows 7 64bit Access 2016
    Join Date
    Dec 2014
    Posts
    65
    Of this I am already aware but thanks for the input

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Arrays - varinats and alternatives
    By Middlemarch in forum Programming
    Replies: 4
    Last Post: 07-27-2016, 05:04 PM
  2. Array class for working with Arrays
    By Ruegen in forum Programming
    Replies: 7
    Last Post: 03-11-2015, 10:01 PM
  3. sql query arrays
    By drewdrew in forum Programming
    Replies: 3
    Last Post: 07-03-2014, 03:35 AM
  4. passing arrays io a report
    By Seamus59 in forum Reports
    Replies: 3
    Last Post: 05-21-2013, 10:51 AM
  5. Passing ID's
    By Trojnfn in forum Access
    Replies: 3
    Last Post: 08-01-2012, 11:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums