dbTalk Databases Forums  

Newbie - Assign Global variable? Module help?

comp.databases.ms-access comp.databases.ms-access


Discuss Newbie - Assign Global variable? Module help? in the comp.databases.ms-access forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
M Davidson
 
Posts: n/a

Default Newbie - Assign Global variable? Module help? - 09-28-2004 , 12:07 PM






Hello all,

I'm new to using VBA in Access. I have a series of forms that are
sequentially presented to the user for data entry purposes (used code to
present, open/close, and properly assign results to the different tables...
very happy with that). I want to precede this series of forms with another
form who's sole purpose is to assign values to several variables that I can
later call upon globally (in forms, queries and reports).

I'm having difficulty understanding global variables. I can assign values
to variables and call on them within different forms but I can't figure out
how to assign them globally. In reading, it appears that I'm supposed to
declare the variable using "Public" instead of "Dim" in a "module."

I'm wanting to use a form bound to a table to restrict the possible value
selections that will be assigned to the global variable. (Is that
acceptable?)

Where, in the module's code window, do I make the declarations.

Do I treat the module's code window the same way that I do a form's code
window? (except using the "Public" vs. "Dim" commands)

The event oriented coding makes sense to me... assign the code to whatever
event occurs. My problem is that this "module" is politely sitting over to
the side, and I understand it's purpose, (create blocks of code to execute
different processes that can be called upon by name for efficiency's sake)
The problem is I don't know how to create one (module).

The books I have are a little verbose, I know I'd learn much more quickly if
I could see an example of how to pass some variables back and forth between
a form and a module.

If someone could provide an explantion or a link to information regarding
this topic, I'd greatly appreciate it.

Thanks,
Mike D.




---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.764 / Virus Database: 511 - Release Date: 9/16/2004




Reply With Quote
  #2  
Old   
David Schofield
 
Posts: n/a

Default Re: Newbie - Assign Global variable? Module help? - 09-28-2004 , 12:33 PM






On Tue, 28 Sep 2004 13:07:43 -0400, "M Davidson" <MD (AT) Replytogroup (DOT) com>
wrote:
.....
Quote:
I'm wanting to use a form bound to a table to restrict the possible value
selections that will be assigned to the global variable. (Is that
acceptable?)

.....
Hi
Good idea. If you use a form to set the values, selected from table
values, you might as well assign them to controls on the form instead
of using global variables. To keep them to hand, just make the form
invisible instead of closing it.

One side benefit of this is that the values do not get cleared if
there is a program error, and you can always check them by unhiding
the form.
David




Reply With Quote
  #3  
Old   
Tim Marshall
 
Posts: n/a

Default Re: Newbie - Assign Global variable? Module help? - 09-28-2004 , 01:40 PM



Mike,

David suggests using a form, which stays open throughout the session, to
hold your variables instead of using global variables (which I'll show
you how to set up in a moment). I think this is good advice - one thing
I've learned from the more experienced folks here is that if you throw
an error that is not trapped (ie, some kind of handling in your
procedures' on error section), your global variables get reset, losing
them and possibly messing up your app completely, requiring a restart.

You could have an opening form (under the start up properties) in which
you set your variables - give this a short name, because you'll be
writing it a lot, I always use "frmV". The a click of a button on frmV
to start the users off, instead of having a docmd.close for frmV, do the
following:

me.Visible = false 'Note this is on frmV itself.

Which makes the form invisible. Any button or menu item anywhere else
in which you want to make frmV visible again, use the code:

Forms!frmV.Visible = True

When you are developing, you can put a ' in front of the command making
frmV invisible so that you can see that it is properly populating.

To refer to the value in a control (I use mostly text boxes, but you can
use other things - in fact, instead of using a table to limit values,
one option could be to use combo boxes with value lists!) from another
form, do it as follows (for the text box, txtTIMMY on frmV) in code or
queries:

forms!frmV.txtTIMMY

With respect to global variables and how to set them:

Go to your database window and select the "modules" tab.

Create a new module and name it something - the name does not matter
except as a means by which you can look at your list of standard modules
and determine what is in them. "Standard" module refers to the modules
in the modules tab of the database window. The modules that are
directly associated with forms and reports are called "class" modules (I
think - I stand to be corrected on that).

Inside your new module, type:

Public strGlbTest as String 'a test string

Save the module and strGlbTestString is available to ALL forms, reports
and other modules.

Create a new form and call it "frmTest".

In frmTest, create an unbound text box, txtGlobalValue, and a command
button, btnGlobalValue.

For the after update event of the text box write this:

Private Sub txtGlobalValue_AfterUpdate

strGlbTest = me.txtGlobalValue 'assigns what you type to strGlbTest

End Sub

For the on click event of the button write:

Private Sub btnAdd_Click()

msgbox strGlbTest 'message box with the new global variable value

End Sub

Have fun... 8)

Note too, that in the standard module you can have subs and functions
that can be called from anywhere in any module, form or report and even
be used as a function in your Jet SQL!

--
Tim - http://www.ucs.mun.ca/~tmarshal/
^o<
/#) "Burp-beep, burp-beep, burp-beep?" - Quaker Jake
/^^ "What's UP, Dittoooooo?" - Ditto

Reply With Quote
  #4  
Old   
M Davidson
 
Posts: n/a

Default Re: Newbie - Assign Global variable? Module help? - 10-02-2004 , 01:50 AM



Tim and David,

THANK YOU! THANK YOU! THANK YOU!

Works like a charm! I even adapted the "hidden but still use-able" idea to
several other aspects of other forms.

Super! Couldn't ask for better advice.

Best regards,
Mike

"Tim Marshall" <TIMMY!@antarctic.flowerpots> wrote

Quote:
Mike,

David suggests using a form, which stays open throughout the session, to
hold your variables instead of using global variables (which I'll show you
how to set up in a moment). I think this is good advice - one thing I've
learned from the more experienced folks here is that if you throw an error
that is not trapped (ie, some kind of handling in your procedures' on
error section), your global variables get reset, losing them and possibly
messing up your app completely, requiring a restart.

You could have an opening form (under the start up properties) in which
you set your variables - give this a short name, because you'll be writing
it a lot, I always use "frmV". The a click of a button on frmV to start
the users off, instead of having a docmd.close for frmV, do the following:

me.Visible = false 'Note this is on frmV itself.

Which makes the form invisible. Any button or menu item anywhere else in
which you want to make frmV visible again, use the code:

Forms!frmV.Visible = True

When you are developing, you can put a ' in front of the command making
frmV invisible so that you can see that it is properly populating.

To refer to the value in a control (I use mostly text boxes, but you can
use other things - in fact, instead of using a table to limit values, one
option could be to use combo boxes with value lists!) from another form,
do it as follows (for the text box, txtTIMMY on frmV) in code or queries:

forms!frmV.txtTIMMY

With respect to global variables and how to set them:

Go to your database window and select the "modules" tab.

Create a new module and name it something - the name does not matter
except as a means by which you can look at your list of standard modules
and determine what is in them. "Standard" module refers to the modules in
the modules tab of the database window. The modules that are directly
associated with forms and reports are called "class" modules (I think - I
stand to be corrected on that).

Inside your new module, type:

Public strGlbTest as String 'a test string

Save the module and strGlbTestString is available to ALL forms, reports
and other modules.

Create a new form and call it "frmTest".

In frmTest, create an unbound text box, txtGlobalValue, and a command
button, btnGlobalValue.

For the after update event of the text box write this:

Private Sub txtGlobalValue_AfterUpdate

strGlbTest = me.txtGlobalValue 'assigns what you type to strGlbTest

End Sub

For the on click event of the button write:

Private Sub btnAdd_Click()

msgbox strGlbTest 'message box with the new global variable value

End Sub

Have fun... 8)

Note too, that in the standard module you can have subs and functions that
can be called from anywhere in any module, form or report and even be used
as a function in your Jet SQL!

--
Tim - http://www.ucs.mun.ca/~tmarshal/
^o
/#) "Burp-beep, burp-beep, burp-beep?" - Quaker Jake
/^^ "What's UP, Dittoooooo?" - Ditto

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.771 / Virus Database: 518 - Release Date: 9/28/2004





Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.