473,386 Members | 1,763 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Global Variables Not Recognising

106 100+
Hi
I need to set two global values after a successful password.
These values will determine the rights for each staff.
But I am having difficulty achieving this.

StaffType is a number to determine level of security
CurrentStaff is pointer to staff name.

Your help is appreciated

This is my module
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2.  
  3. Global StaffType As Long
  4. Global CurrentStaff As Long
  5.  
  6. Public Sub init_Globals()
  7.     StaffType = 0
  8.     CurrentStaff = 0
  9. End Sub
This is the code behind the form:
if I remove lines 5,6, 7 - all works except my variables are not set to what I want.
Expand|Select|Wrap|Line Numbers
  1. Private Sub EnterPassword_AfterUpdate()
  2.  
  3.     If Me.EnterPassword = Me.StaffPassword Then
  4.         MsgBox "MATCH"
  5.         CurrentStaff = Me.StaffID
  6.         StaffType = Me.StaffTypeID
  7.         MsgBox StaffType & " " & CurrentStaff
  8.         DoCmd.Close acForm, "StaffPasswordCheck", acSaveNo
  9.         DoCmd.OpenForm "Switchboard"
  10.     Else
  11.         MsgBox "Incorrect: Please Re-Enter"
  12.         Me.EnterPassword.SetFocus
  13.     End If
  14.  
  15. End Sub
  16.  
  17. Private Sub EnterPassword_Enter()
  18. If IsNull(Me.cmbUsername) Then
  19.     Me.cmbUsername.SetFocus
  20. End If
  21. End Sub
  22.  
  23. Private Sub Form_Open(Cancel As Integer)
  24. Call init_Globals
  25. End Sub
Sep 22 '09 #1

✓ answered by missinglinq

In Access VBA, global variables are not declared like this

Global StaffType As Long
Global CurrentStaff As Long


but rather like this

Public StaffType As Long
Public CurrentStaff As Long


They go in a standard module, which it sounds like you've done.

Also be aware that you cannot name the Module with the same name used for a Sub/Function within the Module.

Linq ;0)>

6 2802
missinglinq
3,532 Expert 2GB
In Access VBA, global variables are not declared like this

Global StaffType As Long
Global CurrentStaff As Long


but rather like this

Public StaffType As Long
Public CurrentStaff As Long


They go in a standard module, which it sounds like you've done.

Also be aware that you cannot name the Module with the same name used for a Sub/Function within the Module.

Linq ;0)>
Sep 22 '09 #2
tasawer
106 100+
Thanks for your reply.
I have made the changes... but the problem remains

here are the mods: (module is called Global_Decs)
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2.  
  3. Public StaffType As Long
  4. Public CurrentStaff As Long
  5.  
  6. Sub init_Globals()
  7.     StaffType = 0
  8.     CurrentStaff = 0
  9. End Sub
Code Behind the form remains the same

I've taken the code down to basics and even the code below fails.
I do not get any errors in the compile stage either.
so I'm stumped

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Open(Cancel As Integer)
  2. Call init_Globals
  3. MsgBox StaffType & " " & CurrentStaff
  4. End Sub
HOWEVER:
within the declarations module, When I change Stafftype value from 0 to 1 or 2and run following code, it works. :)
Expand|Select|Wrap|Line Numbers
  1.  Private Sub Form_Open(Cancel As Integer)
  2. Call init_Globals
  3.  
  4. Select Case StaffType
  5.     Case 1
  6.     Me.Command23.Visible = True
  7.  
  8.     Case 2
  9.     Me.Command23.Visible = False
  10.  
  11.     End Select
  12.  
  13. End Sub
Sep 22 '09 #3
NeoPa
32,556 Expert Mod 16PB
I believe the word Global was superceded by Public a few versions ago. I thought it was still supported though, if not recommended.

Linq's suggestion should fix your problem.
Sep 22 '09 #4
NeoPa
32,556 Expert Mod 16PB
Have you tried tracing the code as it happens (See Debugging in VBA)?
What do you see when you do that?
Sep 22 '09 #5
tasawer
106 100+
HI,
Iworked on each variable one at a time and found that renaming StaffType to gblStaffType has solved the problem. I don't know why.

if anyone can shed light on this, it will be help to understand.
Sep 22 '09 #6
NeoPa
32,556 Expert Mod 16PB
I can't say for sure, but I suspect you may have an object somewhere with the same name. Possibly a control on your form.

Certainly there's nothing that depends on the name starting with a particular character sequence.
Sep 22 '09 #7

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

Similar topics

10
by: Matt | last post by:
Greetings, What are people's thoughts on global variables in C++? Why are we taught not to use them in programming? Is it true that if you are running two copies of the C program one copy can...
4
by: Andrew V. Romero | last post by:
I have been working on a function which makes it easier for me to pull variables from the URL. So far I have: <script language="JavaScript"> var variablesInUrl; var vArray = new Array(); ...
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...
33
by: MLH | last post by:
I've read some posts indicating that having tons of GV's in an Access app is a bad idea. Personally, I love GVs and I use them (possibly abuse them) all the time for everything imaginable - have...
44
by: fabio | last post by:
Why? i' ve heard about this, the usage of global vars instead of locals is discouraged, but why? thx :)
9
by: CDMAPoster | last post by:
About a year ago there was a thread about the use of global variables in A97: http://groups.google.com/group/comp.databases.ms-access/browse_frm/thread/fedc837a5aeb6157 Best Practices by Kang...
5
by: Sandman | last post by:
I dont think I understand them. I've read the section on scope in the manual inside out. I'm running PHP 5.2.0 Here is the code I'm working on: //include_me.php <?php $MYVAR = array(); global...
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...
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.