eusataf
eusataf

Reputation: 897

How to declare a global array in a form?

How to declare a global array in a form?
I need the array to be populated in one procedure, and read in another procedure.
With the current code in line 1 ofFillArray_1

array1 = Array( ...

I get an error message:

Expected variable or procedure, not project

Code:

Dim array1() As String                  ' Array

Private Sub Exe_btn_Click()
    PrintArray
End Sub

Public Sub FillArray_1()
    array1 = Array( _
                               "member_1", _
                               "member_2", _
                               "member_3")
End Sub

Public Sub PrintArray()
    FillArray_1                   

    Dim i As Integer

    For i = LBound(array1) To UBound(array1)
                Debug.Print array1(i)
    Next i
End Sub

Upvotes: 1

Views: 211

Answers (3)

ComputerVersteher
ComputerVersteher

Reputation: 2686

As the error message indicates,Arrayis the name of your project! Rename project or you have to useVBA.Arrayto avoid this conflict:

Public Sub FillArray_1()
    array1 = VBA.Array( _
                               "member_1", _
                               "member_2", _
                               "member_3")
End Sub

Upvotes: 2

Gustav
Gustav

Reputation: 55806

Not Dim but Public:

Public array1() As String 

and it can't be located in the form's module. Move it to a code module.

Upvotes: 1

Andre
Andre

Reputation: 27634

And to fill array1 with the Array() function, you must declare it as Variant.

Dim array1 As Variant

or

Public array1 As Variant

if these functions are in different modules.

Upvotes: 1

Related Questions