global variables for whole project not just when user form is runn

B

Bev

Can anyone help me. I've got a template project with 2 user forms. Code has
to stop running between using the two user forms to enable the user to add
unique information and check statements created to that point, before
calculating of each statement occurs. Then the user re-starts the programme,
so I'm trying to find a way to retain the variable information contained in
the first user form, to be utilised in code for the 2nd user form. I already
know about declaring global variables within modules, but I'm talking about
retaining variable input for the whole project, over all modules.
 
J

Jezebel

'Global variables within modules' suggests you haven't quite understood
variable scoping. Variables declared at the top of a module are either
global -- meaning they are available to the entire project, or private --
meaning they are available only within that module. Be aware that global
variables lose their value if you do any editing in VBA.

Global variables have a bad reputation in programming: they are major cause
of bugs; and there are nearly always better ways to move data between
objects in the application. In your case, instead of using globals for each
data value, you could use a single global with a reference to form object,
then make the data values available as properties of the form. The
programming advantage is that the form 'owns' the data and can ensure that
it is valid.
 
B

Bev

Thanks Jezebel for your quick response, but somehow I don't think I
understand enough to interpret all your suggestions. What I've learnt in
programming I've taught myself so can you please be more specific about how
to make reference to the form object and making the data values available as
properties of the form. Does that mean the input from User Form 1 will still
be available once the user re-starts the programme and is using User Form 2?
 
J

Jezebel

Here's one way to manage a global reference to a form. In an ordinary code
module --

Private mMyForm as frmMyForm 'Module-level variable

Public Function MyForm() as frmMyForm

If mMyForm is nothing then
set mMyForm = new frmMyForm
End if

MyForm = mMyForm

End Function


Within the form, instead of unloading, you Hide the form. This returns
control to the calling function. To provide a property --

Private mMyValue as whatever 'form-level variable to hold the data
value, set by using the form

'Provide the value as a read-only property
Public Property Get MyValue() as whatever
MyValue = mMyValue
End Property



'Code in form 2 (or anywhere else in the application) to retrieve the
value --
MyForm.MyValue
 
B

Bev

Thanks very much Jezebel, very helpful. I haven't put it to use yet, but I
can certainly see how, and hiding the form seems a great idea, and I'll have
to remember to unload it at the end of the project. Thanks again.
Bev
 

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