Solved

Passing array through subs and functions.

Posted on 2002-03-18
4
164 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 69

Accepted Solution

by:
Éric Moreau earned 50 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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 69

Expert Comment

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

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now