-
I have an array within one form in my project containing no more than 60 entries which I need to use on another form in my project. I've tried using
frmInterface.<arrayname> = <stuff>
but that gives an error. Is there any way of referencing arrays on different forms? If so can somebody please let me know! :)
TIA
BioS
-
Here ya go: To use this, just add a public function to your destination form and pass the array as indicated below
Code:
Option Explicit
Dim intNum(1 To 5) As Integer
Private Sub Command1_Click()
Call Form2.PrintNum(intNum())
End Sub
Private Sub Form_Load()
Dim X As Integer
For X = 1 To 5
intNum(X) = X
Next
End Sub
Private Sub PrintNum(xArray() As Integer)
Dim var As Variant
For Each var In xArray
Print var
Next
End Sub
-
When declaring the array in the form, declare it as: "Public Array(20) As Integer" (or however you're array is constructed, just use Public), then you can just use it as frmInterface.Array(Item)...
-
that will also work. I just avoid using public variables.
-
Thanks both... exactly what I was looking fer :)
BioS
-
The public thing doesn't seem to work and I didn't really understand the top example!
I get a compile error :
'Constants, fixed-length strings, arrays, user-defined types and Declare statements not allowed as Public members of object modules'
Any ideas on getting round this?
TIA
BioS
-
you can't make arrays Public variables. The way you would get around that is to declare a Public variable as a Variant, then set that variable = to the array, but that's too much work for me, I'd go with Lethal's way...
-
Any chance of an explaination of Lethal's way? does all that code go on the same form? and what is .PrintNum?
BioS
-
The code I posted is just supposed to give you an example of passing arrays through multiple forms. The PrintNum function would go into your other form where you want to pass the array.
-
sure...
Code:
Option Explicit
Dim intNum(1 To 5) As Integer
Private Sub Command1_Click()
Call Form2.PrintNum(intNum()) ' sends the entire array to the sub on form2 called PrintNum
End Sub
Private Sub Form_Load() 'loads the array up with values, obviously
Dim X As Integer
For X = 1 To 5
intNum(X) = X
Next
End Sub
'on Form2
Private Sub PrintNum(xArray() As Integer)'takes in an array as a parameter
Dim var As Variant
For Each var In xArray
Print var ' prints each item in the array on the form
Next
End Sub
-
-
hehehe.....your gettin' quicker there blade...:D
-
Cheers guys... appreciate it :)
BioS