473,326 Members | 2,175 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,326 software developers and data experts.

Access Forgets Global Variable!!

This is a strange one.

I have an app that needs to know who is using the program but there's no
need for security. When the program opens a form comes up with a listbox.
The user double-clicks his name, the data is stuffed into a variable that is
dimmed in a module, and the user is "logged in".

From time to time, though, Access forgets the value! There is no other place
in the database where the variable is written. This is happening in 2002,
but I've seen this problem in 97 too.

The work-around is no problem. I just stuff the user name into a table when
the user logs in and get the data from there. But it just blows me away that
this variable comes up blank every once in a while.

Anybody else seen this? What could be the problem?

Thanks!

Mike
Nov 13 '05 #1
8 2433

"Mike Turco" <mi*******@yahoo-no-spam-4-me.com> wrote in message
news:K10Zd.108842$bu.39614@fed1read06...
This is a strange one.

I have an app that needs to know who is using the program but there's no need
for security. When the program opens a form comes up with a listbox. The user
double-clicks his name, the data is stuffed into a variable that is dimmed in
a module, and the user is "logged in".

From time to time, though, Access forgets the value! There is no other place
in the database where the variable is written. This is happening in 2002, but
I've seen this problem in 97 too.

The work-around is no problem. I just stuff the user name into a table when
the user logs in and get the data from there. But it just blows me away that
this variable comes up blank every once in a while.

Anybody else seen this? What could be the problem?


If you have an unhanded error in an MDB (not an MDE) then variables will often
lose their values.

--
I don't check the Email account attached
to this message. Send instead to...
RBrandt at Hunter dot com
Nov 13 '05 #2
If your application (your code) encounters a error, then all global (and in
fact all) variables are re-set. You loose their values.

There is two solutions to this problem:

1) make sure you always have error handling code for each routine

2) Since I don't always follow #1, then distribute your application as a
mde, and un-handled errors will NOT reset your variables (both local, and
global).

So, while it is standard recommend and practice to distribute a mde so users
can't change, and mess with things, using a mde also have many other added
benefits in terms of reliability of your application.

--
Albert D. Kallal (Access MVP)
Edmonton, Alberta Canada
pl*****************@msn.com
http://www.members.shaw.ca/AlbertKallal
Nov 13 '05 #3

"Albert D. Kallal" <ka****@msn.com> wrote in message
news:B21Zd.666292$6l.393911@pd7tw2no...
If your application (your code) encounters a error, then all global (and
in fact all) variables are re-set. You loose their values.

There is two solutions to this problem:

1) make sure you always have error handling code for each routine

Interesting! Is "on error resume next" at the top of the sub not good
enough? What about something like the following?

Sub Test()

On Error Goto Test_err

docmd.this
docmd.that
exit sub

Test_err:
select case err.number
case 1:
msgbox "Oops, you typed that 300 digit number wrong"
resume next
case 2:
resume next
case else
msgbox err.number & ": " & err.description
resume next
end select
2) Since I don't always follow #1, then distribute your application as a
mde, and un-handled errors will NOT reset your variables (both local, and
global).


This particular app is going to be distributed run-time anyway, so I might
as well distribute the mde. I'm still a little surprised, though. I didn't
know that encountering an error reset all your variables and its even more
of a surprise that I've only seen this issue crop up twice in the past ten
or so years.

Mike
Nov 13 '05 #4
"Mike Turco" <mi*******@yahoo-no-spam-4-me.com> wrote in
news:d83Zd.109335$bu.7432@fed1read06:

"Albert D. Kallal" <ka****@msn.com> wrote in message
news:B21Zd.666292$6l.393911@pd7tw2no...
If your application (your code) encounters a error, then all
global (and in fact all) variables are re-set. You loose
their values.

There is two solutions to this problem:

1) make sure you always have error handling code for each
routine


Interesting! Is "on error resume next" at the top of the sub
not good enough? What about something like the following?

Sub Test()

On Error Goto Test_err

docmd.this
docmd.that
exit sub

Test_err:
select case err.number
case 1:
msgbox "Oops, you typed that 300 digit number
wrong" resume next
case 2:
resume next
case else
msgbox err.number & ": " & err.description
resume next
end select
2) Since I don't always follow #1, then distribute your
application as a mde, and un-handled errors will NOT reset
your variables (both local, and global).


This particular app is going to be distributed run-time
anyway, so I might as well distribute the mde. I'm still a
little surprised, though. I didn't know that encountering an
error reset all your variables and its even more of a surprise
that I've only seen this issue crop up twice in the past ten
or so years.

Mike

There is another way of holding the user's ID. Keep the form
open, and refer to the form's control.You make the form invisible
instead of closing it. It's how I handle this situation.
--
Bob Quintal

PA is y I've altered my email address.
Nov 13 '05 #5
"Mike Turco" <mi*******@yahoo-no-spam-4-me.com> wrote in
news:K10Zd.108842$bu.39614@fed1read06:
I have an app that needs to know who is using the program but
there's no need for security. When the program opens a form comes
up with a listbox. The user double-clicks his name, the data is
stuffed into a variable that is dimmed in a module, and the user
is "logged in".

From time to time, though, Access forgets the value! There is no
other place in the database where the variable is written. This is
happening in 2002, but I've seen this problem in 97 too.


Why not hide the form that the user double-clicks on and instead of
referring to the global variable, either refer to that form, or, if
you worry about performance, then use a function, something like
this (which is self-healing if the global variable is reset):

Public Function GetUser() As String
Static strUser As String

If Len(strUser) = 0 Then
strUser = Forms!dlgUserName!lstUsers
End If

GetUser = strUser
End If

You'll note that it will look it up from the hidden form only if the
Static variable gets reset. I prefer this method to using global
variables when the variable is set only once or can be retrieved
from some other source (such as a lookup or a Windows API call).

Of course, you'll want to handle the case where the dialog form gets
closed.

Another alternative if you're intending to store it in a table (I'd
never do that -- I might consider storing it as a custom property of
the MDB file, though) is to have this function do the table (or
customer property) lookup if the static variable has been reset.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #6

"David W. Fenton" <dX********@bway.net.invalid>
Another alternative if you're intending to store it in a table (I'd
never do that -- I might consider storing it as a custom property of
the MDB file, though) is to have this function do the table (or
customer property) lookup if the static variable has been reset.


The variable does go into a table, anyway, because I want the database to
remember the variable between sessions. Rather than refer to the variable
directly I call a function. If the variable is empty then the function looks
up the value in the table. The variable only dumps out occasionally so its
not a performance issue.


Nov 13 '05 #7
On Sun, 13 Mar 2005 23:19:37 -0800, "Mike Turco"
<mi*******@yahoo-no-spam-4-me.com> wrote:

"David W. Fenton" <dX********@bway.net.invalid>
Another alternative if you're intending to store it in a table (I'd
never do that -- I might consider storing it as a custom property of
the MDB file, though) is to have this function do the table (or
customer property) lookup if the static variable has been reset.


The variable does go into a table, anyway, because I want the database to
remember the variable between sessions. Rather than refer to the variable
directly I call a function. If the variable is empty then the function looks
up the value in the table. The variable only dumps out occasionally so its
not a performance issue.


Well, you've stumbled across a very good answer for handling cached values in
VBA. Populate the variable unless it's already populated. That way, if you
ever reset the program, the program will recover transparently.
Nov 13 '05 #8

"Steve Jorgensen" <no****@nospam.nospam> wrote in message
news:i9********************************@4ax.com...
On Sun, 13 Mar 2005 23:19:37 -0800, "Mike Turco"
<mi*******@yahoo-no-spam-4-me.com> wrote:

"David W. Fenton" <dX********@bway.net.invalid>
Another alternative if you're intending to store it in a table (I'd
never do that -- I might consider storing it as a custom property of
the MDB file, though) is to have this function do the table (or
customer property) lookup if the static variable has been reset.


The variable does go into a table, anyway, because I want the database to
remember the variable between sessions. Rather than refer to the variable
directly I call a function. If the variable is empty then the function
looks
up the value in the table. The variable only dumps out occasionally so its
not a performance issue.


Well, you've stumbled across a very good answer for handling cached values
in
VBA. Populate the variable unless it's already populated. That way, if
you
ever reset the program, the program will recover transparently.


That works well for me.

I was trying to create that mde, by the way. Turns out I can't do it because
I'm using A2002 and the file needs to stay in A2000 format. Since I'm
distributing the run-time for some of the computers, I hope there are no
other little surprises :-)

Mike
Nov 13 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

44
by: Mohanasundaram | last post by:
int i = 10; int main() { int i = 20; return 0; } Hi All, I want to access the global variable i inside the main. Is there
1
by: Machi | last post by:
let say i have a web form with a button on it ... when i click the button, it will pop up one small window to allow me to save a pdf file, everything is done using code behind as below ... however,...
9
by: ruca | last post by:
How can I declare a global variable in my .js file, that I can preserve her value each time I need to call any function of .JS file in my ASP.NET application? Example: var aux=null; function...
1
by: anjud | last post by:
I have an external javascript file which contains a global variable - arrNames var arrNames=new Array(); arrNames="FirstName"; arrNames="LastName"; In my html page I had given the path of...
9
by: Shilpa | last post by:
Hi, I just wanted to know whether we can access global variable within a local block , where both variables are having same name. For ex: int temp=5 ; { int temp=10;
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...
20
by: teddysnips | last post by:
Weird. I have taken over responsibility for a legacy application, Access 2k3, split FE/BE. The client has reported a problem and I'm investigating. I didn't write the application. The...
4
Dheeraj Joshi
by: Dheeraj Joshi | last post by:
Hi, I was wondering is there any technique available, so we can access the global variable inside a function if we have a local variable inside the function with the same name as global variable. ...
3
by: jay manu | last post by:
i have two buttons, which are disabled on page load, when i select one radio button i have to make that button enable. But when i write button1.Enable() in the radibutton click event, its showing...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.