Share an array between forms

Hello Experts. I think this is an easy one.

I tried to declare an array as public in my main form, as it must be accessed by various other forms. However, arrays cannot be public apparently.

What is the simplest way of making it available to other modules/forms?  

Who is Participating?
bobbit31Connect With a Mentor Commented:
insert a module into your project and declare the array there
Add a module Globals.bas

and declare your array as Global there.
Add New Module to your project, ex: modMain.bas

Declare the following:
Public garyParameterArray() as String

The above declaration is dynamic array, after declaring, you can use it anywhere in your project, just do the following:

    ReDim garyParameterArray(0 To 0)
    garyParameterArray(0) = "TEST"

So that u can redim anywhere u want with whatever size u want and use it.

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Richie_SimonettiIT OperationsCommented:
bobbit31 has the answer.
If you like to work on other way, you must reference the array with the name of the module/form that contains it:

or whatever you named both.
Valliappan ANSenior Tech ConsultantCommented:
You could also, try declaring a string as public, in your module, or the main form. And use Split and Join functions of VB6, provided, your array is not that big.

Public gStrArray As String

In your passing form,

gStrArray = Join(yourArray,",")

In your getting form,

Dim localArray
localArray = Split(gStrArray,",")

Hope this helps,

Valliappan ANSenior Tech ConsultantCommented:
If you declare it in your main form, then the public variable should be prefixed with the form name and a dot, like, mainform.gStrArray.
DapperDanAuthor Commented:
Sorry for the delay in checking back. Thanks for all your comments. I will try this out when I get home from work tonight (UK time).

DapperDanAuthor Commented:
That was annoyingly simple. I have a lot to learn about VB :).

Seems fairest to give points to the first reply, but thanks to everyone.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.