473,473 Members | 2,282 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Using global variables in Access VB form modules...

patjones
931 Recognized Expert Contributor
Hi:

I have two forms, frmVerifyUser and frmVerifyNewPassword. In the VB module for both forms, at the very top, I try to set a global variable like

Expand|Select|Wrap|Line Numbers
  1. Public strNewPassword As String
  2. Option Compare Database
  3. Option Explicit
The problem is, when I set the variable in frmVerifyNewPassword, say

Expand|Select|Wrap|Line Numbers
  1. strNewPassword = Me!txtNewPasswordA
this assignment is not reflected in frmVerifyUser. What am I doing wrong here? Thank you...
Oct 22 '07 #1
10 46625
missinglinq
3,532 Recognized Expert Specialist
You need to place the Public statement in a standard module to explicitly declare a variable as a public or global variable, and thus available throughout your database.

Linq ;0)>
Oct 23 '07 #2
ADezii
8,834 Recognized Expert Expert
Hi:

I have two forms, frmVerifyUser and frmVerifyNewPassword. In the VB module for both forms, at the very top, I try to set a global variable like

Expand|Select|Wrap|Line Numbers
  1. Public strNewPassword As String
  2. Option Compare Database
  3. Option Explicit
The problem is, when I set the variable in frmVerifyNewPassword, say

Expand|Select|Wrap|Line Numbers
  1. strNewPassword = Me!txtNewPasswordA
this assignment is not reflected in frmVerifyUser. What am I doing wrong here? Thank you...
By Declaring a Variable as Public in a Form's Code Module, you are essentially making the Variable a Property of the Form. If strNewPassword is Declared and Initialized in Form1, you can reference it in Form2 as a Property of Form1, namely:
Expand|Select|Wrap|Line Numbers
  1. Forms!Form1.strNewPassword
NOTE: This is, however, not the acceptable Method.
Oct 23 '07 #3
patjones
931 Recognized Expert Contributor
By Declaring a Variable as Public in a Form's Code Module, you are essentially making the Variable a Property of the Form. If strNewPassword is Declared and Initialized in Form1, you can reference it in Form2 as a Property of Form1, namely:
Expand|Select|Wrap|Line Numbers
  1. Forms!Form1.strNewPassword
NOTE: This is, however, not the acceptable Method.
I take it then that Linq's method is the way to go...I'm not in front of the machine that has the database on it right now, but I'll try it when I get to work. Thank you!
Oct 23 '07 #4
ADezii
8,834 Recognized Expert Expert
I take it then that Linq's method is the way to go...I'm not in front of the machine that has the database on it right now, but I'll try it when I get to work. Thank you!
I take it then that Linq's method is the way to go
Definately, stay with Linq's method. I was just illustrating some useless information. (LOL).
Oct 23 '07 #5
patjones
931 Recognized Expert Contributor
Definately, stay with Linq's method. I was just illustrating some useless information. (LOL).
Yup, putting it in a standard module works beautifully.

This is actually the second time that I ran into problems because I didn't know to use a standard module. The first time was when I defined my own function and kept it in the form's module (to be used by another form in the project). As soon as I put the function definition into a standard module, it worked great.

Are there any general guidelines for when to use a standard module?

Thanks!
Oct 23 '07 #6
missinglinq
3,532 Recognized Expert Specialist
Well, you really just defined it yourself! You store anything (functions, variables, constants, etc) in a standard module if you want it to be available from anywhere in your database.

One thing to remember, if you place a function in a new standard module, when it comes time to name the module, do not give it the same name as the function! This confuses the Access Gnomes something terrible!

Linq ;0)>
Oct 23 '07 #7
patjones
931 Recognized Expert Contributor
Well, you really just defined it yourself! You store anything (functions, variables, constants, etc) in a standard module if you want it to be available from anywhere in your database.

One thing to remember, if you place a function in a new standard module, when it comes time to name the module, do not give it the same name as the function! This confuses the Access Gnomes something terrible!

Linq ;0)>
Thanks so much for your help!
Oct 23 '07 #8
ADezii
8,834 Recognized Expert Expert
Well, you really just defined it yourself! You store anything (functions, variables, constants, etc) in a standard module if you want it to be available from anywhere in your database.

One thing to remember, if you place a function in a new standard module, when it comes time to name the module, do not give it the same name as the function! This confuses the Access Gnomes something terrible!

Linq ;0)>
Besides those items already pointed out by Linq, here are a few more to remember. Do not feel bewildered because Variable Declaration and Scope is a very confusing Topic for newcomers.
  1. First and foremost, keep the tightest Variable Scope as possible. If a Variable is only going to be used in the context of a Form's Module, declare it as Private within the Form's Module, not as Public in a Standard Code Module.
  2. If a Variable is going to be used only within the context of a Standard Code Module, declare it as Private not Public, within the Standard Module.
  3. Remember, a Variable declared as Private within a Standard Code Module can only be 'seen' from within that Module and no where else.
  4. Avoid declaring Variables with the Static Keyword unless absolutely necessary - they incur a lot of overhead.
  5. The same can be said for Public (Global) Variable but they are more dangerous since they can be modified from anywhere within your Application.
  6. Avoid the tendency to declare Variables as Type Variant if applicable, you can run into problems and there is great overhead. Declare them as a specific Data Type or Object as in:
    Expand|Select|Wrap|Line Numbers
    1. Dim strTestString As String
    2. Private intCounter As Integer
    3. Public sngYearlyWage As Single
    4. Dim txtHours As TextBox
  7. If you declare a Variable without any Data Type, it is a Variant by Default.
  8. Declare Variables with the smallest Data type that will support the largest possible value. If you need a Variable to hold a person's age, you can declare it as a Byte (0 - 255) instead on an Integer or Long.
  9. These are just a couple of things off the top of my head, there is much more on the Topic of Variable Declaration, Scope, and Usage but this will suffice for now.
Oct 23 '07 #9
Kevin Wilcox
68 New Member
Can I ride on the back of this slightly - is it important / possible to close public variables once finished with them, and if so, how? I have a lot of public strings and integers in my current project and apart from setting them to zero or "" once I've used them I don't do anything else.
Thanks
Oct 25 '07 #10
ADezii
8,834 Recognized Expert Expert
Can I ride on the back of this slightly - is it important / possible to close public variables once finished with them, and if so, how? I have a lot of public strings and integers in my current project and apart from setting them to zero or "" once I've used them I don't do anything else.
Thanks
The problem is not so much in 'closing' them as it is their Scope, meaning there isn't anywhere that they can't be accessed in an Application. To the best of my knowledge, storage for Public Variables are dynamically assigned, so reinitializing them to 0 for Numerics or a Zero Length String for Strings may help. For Object Variables, always set them to Nothing (Set rstMyRecordset = Nothing) in order to release resources assigned to them. For large Public Arrays, you can always use the Erase Statement to re initializes the elements of fixed-size arrays and releases dynamic-array storage space. Besides these points, I'm not sure what else you can do.
Oct 25 '07 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Fuming Wang | last post by:
Hi, I have several modules that need to access global variables among them. I can do that by import modules: module A: gA = 'old' module B: import A
1
by: MackS | last post by:
Hello everyone Consider the following two simple files. The first is my "program", the other a file holding some global variable definitions and code shared by this program and a "twin" program...
17
by: MLH | last post by:
A97 Topic: If there is a way to preserve the values assigned to global variables when an untrapped runtime error occurs? I don't think there is, but I thought I'd ask. During development, I'm...
6
by: rlrcstr | last post by:
The DBA team at the office controls the naming conventions for the database structure, but their naming convention are rather tedious. So typically I create a global module that I use as a mapping...
3
by: frothpoker | last post by:
Guys, I'm sure this has been asked a million times but I can't seem to formulate a google search that returns what i'm looking for. I've go a dev and live environment. Currently the DB...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
1
by: Sharmistha Mukherjee | last post by:
I am trying to open my emailaccount. I am using a button on Access form to follow the hyperlink that brings up the dialogue box. How can I autofill username and password using VBA ?
1
by: Keith Hughitt | last post by:
Hi all, I was wondering if anyone had any suggestions for alternative ways for multiple objects to keep track of a single variable? Initially, I thought of just using a single (YUI) Custom event...
2
by: kstevens | last post by:
I have a global variable assigned in a module. Public MyVar as Integer Right before I open a report, I have the variable set. MyVar= InputBox("Please enter the markup percentage for oursource...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.