Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Global variable not working correctly

Status
Not open for further replies.

dan08

Technical User
Jan 7, 2013
51
US
I have been banging my head against the wall on this one.
I have a VBA function that get a users username and department. That function works fine. I have some code that needs to run when the DB is first opened so I used an autoexec macro that calls a VBA function:
Code:
public function db_open()
 user = getuser()
 dept = getdept()
DoCMD.openform "introform"

My syntax might be off but trust me that code works. And I have Global user and Global dept stated at the top of this module.

I have a form that opens and needs the user and dept variables to populate some fields. The fields get populated correctly. Using default value = user on form open.
But I put a button on the form that pops up a msgbox with the username. This msgbox is EMPTY. Same with dept.

Why are the variables not sticking around? I need these variables constantly accessible throughout the session and between multiple forms.
 
hi,

I have Global user and Global dept stated at the top of this module.

Global is not a VBA statement. Check out Public instead.

VBA_Help said:
Public Statement
Used at module level to declare public variables and allocate storage space.

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
I've tried Public originally with the same result. I just tried Global because I saw it somewhere and I'm just grasping at straws now.
 

at the top of this module

Which module is this?

What procedures do you have in this module?

Please post the code from this module.

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
>Global is not a VBA statement

It is, you know. Just not documented since it is pretty much deprecated. Public variables fulfil the same role, and are more versatile (Globals can only be declared in a standard module, which I suspect is why there are erroneous claims out on the Internet that Globals is not a legitimate keyword - they've tried it in a userform, or a document module and found it generates an error, and assumed this meant it wasn't supported)

 
How are ya dan08 . . .

You may want to consider DB Properties. They're nonvolatile and stay with the db!

[blue]Your Thoughts? . . .[/blue]

See Ya . . .

Be sure to see FAQ219-2884 Worthy Reading! [thumbsup2]
Also FAQ181-2886 Worthy Reading! [thumbsup2]
 
Oh man, I'm an idiot. I was unsetting the variables myself. I had a main form that was always open. So I unset the variables when it closed, to "log out" the user when they closed the database. But I recently reconfigured all the forms and now this form closes sometimes and unsets the variables. I had a function on each form_open to set the variables if for some reason they were not set. I guess they were getting unset by the form_close after the form_open checked if they were set.

To solve this I created a logout form. Just an empty form that is opened (hidden) when the database opens and only closes when the DB closes. With the code for "logging out" in its form_close subroutine. This solved the problem and others.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top