Passing Arrays

J

Joe Delphi

Hi,

I need help passing arrays to a subroutine. When my subroutine looks like
this:

Public Sub CalcIndNetSalesRevenue(ByRef rst As Recordset, ParamArray
Array1() As Variant)

everything is OK.

But I need to pass two arrays to the subroutine, so when I attempt to pass
Array2 by altering my subroutine to look like this:

Public Sub CalcIndNetSalesRevenue(ByRef rst As Recordset, ParamArray
Array1() As Variant, ParamArray Array2() As Variant)

I get an error message. Is there a limitation on the number of ParamArrays
that can be passed to a subroutine?

If yes, does anyone know how I can pass more than one array to my
subroutine?

Any help appreciated.

JD
 
A

Albert D.Kallal

ParmArry is used for a OPTIONAL list of parms.

If you simply are passing two arrays, then just use:

Public Sub CalcIndNetSalesRevenue(rst As Recordset,
Array1() As Variant,
Array2() As Variant)

I broken the above line to prevent word wrap and confusion.

So, you are allowed only ONE parmarray. Note that a parmarray is a SPECIAL
feature to parse out optional parameters. Since in you case, you are tying
to pass two arrays, then I suspect that you DO NOT need pram array.

ParamArray is for creating commands that have a un-known amount of
parameters. Eg:

SearchFor "apple"

SearchFor "apple","pear","banana"

The sub "SearchFor" would thus be:

Sub SearchFor(ParamArray vlist() As Variant)

Dim strMsg As String
Dim i As Integer
For i = 0 To UBound(vlist)
strMsg = strMsg & vlist(i) & vbCrLf
Next i

MsgBox "parms passed = " & strMsg
' code here to build sql or whatever....

End Sub

Note how we used:

SearchFor "a","b","c" and NOT

SearchFor "a,b,c"

(the above would result in only ONE paramater).

So, ParamArray is a nice way to make custom commands that accept variable
parameters, but is NOT a way of passing an array to a sub!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top