Solved

Passing Arrays to functions

Posted on 1998-06-09
24
358 Views
Last Modified: 2010-05-03
How can I pass arrays to functions?
0
Comment
Question by:Hosehead
  • 10
  • 8
  • 3
  • +2
24 Comments
 
LVL 6

Expert Comment

by:clifABB
ID: 1462930
Declare your Function Like this:
Private Function Test(MyArray() As String) As Integer

End Function

And then call it like this:
  Dim TestArray(100) As String
  nRes = Test(TestArray())

0
 

Author Comment

by:Hosehead
ID: 1462931
I do that, but it gives a compile error when I call the function saying that sub or function isn't defined and highlights the array:
var = function(array())
and it highlights array.

0
 
LVL 6

Expert Comment

by:clifABB
ID: 1462932
This is the proper method, so I would suggest there is something wrong with your code.

May I see a copy of the function's declaration and the call?
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

Author Comment

by:Hosehead
ID: 1462933
Note, All the array's used in the call are defined as public and used in other forms.  The FFSave Function hopefully will read these in and save them to an Excel spreadsheet. The call is made from a form when the user click on a save button.

Private Sub SaveReport_Click()
Dim verifyok As Integer

verifyok = FFSave(ReportTitle.Value, 0, WsF, _
            MFGarray(), MFGtitles, ConGarray, _
            ConGtitles, TTGarray, TTGtitles)
'test verifyok for value, then print a message
end sub


<------------In a different module------->
Function FFSave(ByVal RTitle As String, _
                ByVal RType As Integer, _
                ByVal Verify As String, _
                Array1() As Integer, _
                Array2() As String, _
                Array3() As Integer, _
                Array4() As String, _
                Array5() As Integer, _
                Array6() As String) As Integer





End Function

0
 

Author Comment

by:Hosehead
ID: 1462934
Adjusted points to 70
0
 
LVL 6

Expert Comment

by:clifABB
ID: 1462935
OK, I can see some of your problems.

In your call, you don't specify the empty parantheses for arrays 2 through 6.  It should look like this:

verifyok = FFSave(ReportTitle.Value, 0, WsF, _
            MFGarray(), MFGtitles(), ConGarray(), _
            ConGtitles(), TTGarray(), TTGtitles())


In your declare, personally I would be more comfortable to declare the function as public:

Public Function FFSave(ByVal RTitle As String,  'etc

Finally, you note that the function is in a seperate module.  I assume this means a bas module (as opposed to a form module).  If it's bas module, that's good.  If it's a form module, you should move it to a bas module.

I am going to resubmit this as an answer because I'm fairly confident that the first problem was what was causing the error.
0
 

Expert Comment

by:eyaltil
ID: 1462936
hi,
there is a problem to send an array to a procedure/function because an array is a linked pointers list.

here is one solution:
build a module with a private variant parameter, a public sub that get the array and put it in this parameter and a public function that return this paramter.
now, before calling your function, "set" your array to the module parameter and then "get" it from the module in the function.

if u would like an example send me your e_mail to eyaltil@bigfoot.com
bye,
0
 

Author Comment

by:Hosehead
ID: 1462937
When the () are in the call it gives the error "Sub or Function not defined" and hightlights the first array in the call.
0
 

Author Comment

by:Hosehead
ID: 1462938
Adjusted points to 100
0
 

Expert Comment

by:topol
ID: 1462939
Usually, API functions need a pointer to the array, in this case, you can pass the first item of the array, for example
result=Function(array(0))
0
 
LVL 4

Expert Comment

by:yowkee
ID: 1462940
Hosehead,

  Where do you declare those array? In a .bas module? Or declare as public in a form? In latter case, you must specify the form name in front of the array. It seem VB could not find your array since you state it show "sub or function isn't defined" in your first comment.

 
0
 
LVL 6

Expert Comment

by:clifABB
ID: 1462941
topol:
You are absolutely right, except that Hosehead isn't trying to pass an array to an API call.

Yowkee:
I made that point in my "rejected" answer.

Hosehead:
Can you cut and paste here the declares for the arrays as well as the filename of the module in which they are declared.  Also, cut and paste the line of code that calls the function?

Finally, as a test, create a new VB project.  Place a command button on the form.  Add the following code to the form:
Option Explicit
Dim MyArray() As String

Function ArrayTest(TheArray() As String) As Integer
  Dim nCnt1 As Integer
 
  ReDim TheArray(10)
  For nCnt1 = 1 To 10
    TheArray(nCnt1) = Str$(nCnt1)
  Next nCnt1
End Function

Private Sub Command1_Click()
  Dim nRes As Integer
  Dim nCnt2 As Integer
 
  nRes = ArrayTest(MyArray())
  For nCnt2 = LBound(MyArray) To UBound(MyArray)
    Debug.Print nCnt2, MyArray(nCnt2)
  Next nCnt2
End Sub

I just tried it and it works.
0
 

Author Comment

by:Hosehead
ID: 1462942
Okay, I declare the arrays in a seperate module (not a form) and declare them as public, but I don't set their size.  I redim them in the form where each array is used, two arrays per form, so 3 forms.  It is as each form is exited that the arrays are assigned their value.  Then in the master form, the one that called the other three I have a save button and that button calls the function with the arrays as options.  The Function itself is declared in a module where I have all of my public procedures etc...  If I can do this an easier way I would love to know.  None of this is set in stone.
0
 
LVL 6

Expert Comment

by:clifABB
ID: 1462943
What you are doing should work.  I can't understand why it doesn't.

Have you tried my test code?  Is there any difference from yours(other than the use of seperate modules)?
0
 

Author Comment

by:Hosehead
ID: 1462944
Yes, there is a difference.  I am storing data to the array before I pass it to the function, I have the array already at a set size, but other then that, it would be the having it in different modules.
0
 
LVL 6

Expert Comment

by:clifABB
ID: 1462945
Hmmm.  Perplexing indeed.  My sample code works like this as well (without the redim in the function):

Private Sub Command1_Click()
  Dim nRes As Integer
  Dim nCnt2 As Integer
   
  ReDim MyArray(10)
  nRes = ArrayTest(MyArray())
  For nCnt2 = LBound(MyArray) To UBound(MyArray)
    Debug.Print nCnt2, MyArray(nCnt2)
  Next nCnt2
End Sub

and it even works when I specifically declare the array:
Dim MyArray(10) As String

I wonder if this is a bug?  I am using VB5/SP3, how about you?
0
 

Author Comment

by:Hosehead
ID: 1462946
VB for Excel 97
0
 
LVL 6

Expert Comment

by:clifABB
ID: 1462947
I just tested my code under VB Excel97 and it still works.  I even separated it into different modules.

A definite poser, this is.
0
 

Author Comment

by:Hosehead
ID: 1462948
Well, I was hoping it wasn't my code, but i'll take your example and try to figure out what is up, thanks all!
0
 

Accepted Solution

by:
eyaltil earned 120 total points
ID: 1462949
hi,
Please look at the example i e_mailed u

tnx,
eyaltil@bigfoot.com
0
 
LVL 6

Expert Comment

by:clifABB
ID: 1462950
eyaltil:
Considering that, once this goes to paq, there may be people who pay (10%) to see your answer, are you willing to email the example you speak of to all who request it?

Post your example.
0
 

Expert Comment

by:eyaltil
ID: 1462951
ok
here it comes:

FORM1:

Option Explicit

Private Sub Form_Load()
Dim pArray() As String

'...
'...
'...
'PASSING THE ARRAY TO THE MODULE
Set_mArray pArray

'CALLING FORM2
Form2.Show
End Sub


FORM2:
Option Explicit

Private Sub Form_Load()
Dim pArray As Variant

'GETTING THE ARRAY FROM THE MODULE
pArray = Get_mArray()
'...
'...
'...


End Sub



MODULE1:
Option Explicit

Private mArray As Variant

Public Sub Set_mArray(pArray As Variant)
mArray = pArray
End Sub

Public Function Get_mArray() As Variant
Get_mArray = mArray
End Function




ANY QUESTION?
E_MAIL: eyaltil@bigfoot.com
0
 

Expert Comment

by:topol
ID: 1462952
Yeah, passing Variant instead of array is good, but slow idea.
You then can access it as usual on arrays.
0
 

Author Comment

by:Hosehead
ID: 1462953
How would I pass single array arguments to the Set_mArray function if I so desired to do so (laziness, I know it is bad coding...)
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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 process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

776 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