 |
Access Discussion of Microsoft Access database design and programming. See also the forums for Access ASP and Access VBA. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Access section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|

May 12th, 2005, 02:38 PM
|
Authorized User
|
|
Join Date: Jul 2004
Posts: 46
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Global/Public Constants
I hope someone can suggest a way around a seemingly intractable problem. I have database that opens a form (frm_progress) from which the user selectes an area (North, South, etc) from a combo box (Combo13). This area is then used to point the subsequent VBA, SQL queries, Forms and Reports to the relavnt data tables. I would like to store the value of the combo box in a variable that is available to all modules (Class or otherwise). I have tried
Public Const area As String = Forms!frm_progress!combo13
but, of course, that is rejected. I am sure there MUST be a way to do it rather than putting 'area = Forms!frm_...' etc in each module after declaring 'Public area as String'
Can anyone help?
|

May 12th, 2005, 03:02 PM
|
Friend of Wrox
|
|
Join Date: Oct 2003
Posts: 168
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi
Declare a public variable named Area
Then in Combo13 AfterUpdate event assign value to Area Variable
Public Area As String
Area=combo13
-vemaju
|

May 12th, 2005, 03:11 PM
|
Friend of Wrox
|
|
Join Date: Oct 2004
Posts: 564
Thanks: 0
Thanked 4 Times in 4 Posts
|
|
I always find it good to declare all my global variables at the top of a module, outside of any function or sub procedure. This just helps me keep them all together and controlled - well as much control as you can have with a global variable. Vemaju is right on the money!
Mike
EchoVue.com
|

May 12th, 2005, 04:27 PM
|
Authorized User
|
|
Join Date: Jul 2004
Posts: 46
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Thank you both. When it's written down, it's obvious - it just necer occurred to me that when you set a global variable, it's value remains set. Back to school for me, i think
|

May 13th, 2005, 12:31 AM
|
Friend of Wrox
|
|
Join Date: Oct 2003
Posts: 168
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi
It is a good practice to have one module in your project, where you declare all public constants and public variables and nothing else.
I always name this module with name Globals
-vemaju
|

May 13th, 2005, 10:06 AM
|
Friend of Wrox
|
|
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
|
|
Hi,
This discussion is very instructive. Can you tell me more about what the code in the module would look like that would take the value from the combo13 after update event?
Thanks,
mmcdonal
|

May 13th, 2005, 12:22 PM
|
Friend of Wrox
|
|
Join Date: Oct 2004
Posts: 564
Thanks: 0
Thanked 4 Times in 4 Posts
|
|
The Global variable would be declared in the Globals module, however the value would need to be assigned based on some event. You could just have a on onClick even associated with the combo box and then set that value = to your Global Variable. The Global variable is initialized when the application is started and is available from anywhere in the application...
This brings up an interesting problem though. While Global variables definitely make life a little easier. In programming circles particularly with the advanced object oriented languages like Java, the belief is that Global variables should be avoided where ever possible. The reason being that because the variable is accessible from everywhere, in complex programs, you run the risk of the value not being updated or being changed while the variable is 'en-route' to where you intend to reference it.
Mike
Mike
EchoVue.com
|
|
 |