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

June 22nd, 2006 - 10:59 pm ET by Bev | Report spam
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.
email Follow the discussionReplies 5 repliesReplies Make a reply

Similar topics

Replies

#1 Jezebel
June 22nd, 2006 - 11:12 pm ET | Report spam
'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.




"Bev" wrote in message
news:
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.


Replies Reply to this message
#2 Bev
June 22nd, 2006 - 11:46 pm ET | Report spam
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?

"Jezebel" wrote:

'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.




"Bev" wrote in message
news:
> 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.





Replies Reply to this message
#3 Jezebel
June 23rd, 2006 - 12:18 am ET | Report spam
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











"Bev" wrote in message
news:
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?

"Jezebel" wrote:

'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.




"Bev" wrote in message
news:
> 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.







Replies Reply to this message
#4 Bev
June 23rd, 2006 - 01:04 am ET | Report spam
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

"Jezebel" wrote:

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











"Bev" wrote in message
news:
> 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?
>
> "Jezebel" wrote:
>
>> '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.
>>
>>
>>
>>
>> "Bev" wrote in message
>> news:
>> > 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.
>>
>>
>>





Replies Reply to this message
#5 Russ
July 08th, 2006 - 05:11 am ET | Report spam
Bev,

Click on link below for:
Storing Values When a Macro Ends

http://snipurl.com/swu3

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.



Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID
email Follow the discussion Replies Reply to this message
Help Create a new topicReplies Make a reply
Search Make your own search