Solved

Passing array through subs and functions.

Posted on 2002-03-18
4
167 Views
Last Modified: 2010-05-02
Hi.

I have a dynamic array, and would like to pass it to a sub or function, how can I do so?
sub main()
dim array() as string

redim array(1 to 10)

call subroutine1(array)
end sub


public sub subroutine1(newarray() as string)

end sub

This doesnt work!


Thanks for helping me out.
0
Comment
Question by:samsonite1023
  • 2
4 Comments
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 50 total points
ID: 6878908
Just like you started it (notice that I have changed your array variable name):

Private Sub Command1_Click()
Dim MyArray() As String
Dim i As Integer

    ReDim MyArray(1 To 10)
    For i = 1 To 10
        MyArray(i) = i
    Next i
   
    Call subroutine1(MyArray)
End Sub


Public Sub subroutine1(newarray() As String)
Dim i As Integer

    For i = LBound(newarray) To UBound(newarray)
        Debug.Print newarray(i)
    Next i
End Sub
0
 
LVL 18

Expert Comment

by:Crash2100
ID: 6878920
it should work fine the way it is, are you actually calling it array() in your program?  Because that's a reserved word, if you are try changing the name.
0
 
LVL 1

Author Comment

by:samsonite1023
ID: 6878939
Just a big mistake on my part!  I had it right on here, but in my actual code I left out the "Call" keyword.

So stupid!

Apparently without the "Call" keyword, it won't work.

Anyone know why?  I thought leaving Call out was perfectly fine.

Thanks

-Sam
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 6878946
you may omit the Call but in this case, omit the parentheses too.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

808 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