Solved

Passing array through subs and functions.

Posted on 2002-03-18
4
169 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

696 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