By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,863 Members | 879 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,863 IT Pros & Developers. It's quick & easy.

Persisting value

P: n/a
Anyone help a beginner at VBA please?

I am trying to find a way to use a variable throughout an application.

I want to take the value from a log-in form (the user) and use it in most
forms that user opens.

I have tried a number of things but none seem to work

TIA

Clive
Nov 12 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Clive,
I am trying to find a way to use a variable throughout an application. I want to take the value from a log-in form (the user) and use it in most
forms that user opens.


You have to declare the variable in a module(!) as Public:

Public YourUserID As String

HTH - Peter

--
No mails please.
Nov 12 '05 #2

P: n/a
One way would be to leave the log in form open all the time so the value can
always be retrieved directly from the form. If the "OK" button on the form
hides the form rather than closing it then this should work. To simplyfy the
code you could create a simple function that looks up the form value, and
then just use the function name whenever you need the value.

"Clive Moss" <cl***@REMOVEinstant-image.co.uk> wrote in message
news:ji*********************@news-text.cableinet.net...
Anyone help a beginner at VBA please?

I am trying to find a way to use a variable throughout an application.

I want to take the value from a log-in form (the user) and use it in most
forms that user opens.

I have tried a number of things but none seem to work

TIA

Clive

Nov 12 '05 #3

P: n/a
Thanks Andrew - I had tried a version of that idea but I must have done
something else wrong because it didn't work

I'll try it again after dinner!

Clive

"Andrew Smith" <an********@NOSPAM.ntlworld.com> wrote in message
news:5n**************@newsfep4-winn.server.ntli.net...
One way would be to leave the log in form open all the time so the value can always be retrieved directly from the form. If the "OK" button on the form
hides the form rather than closing it then this should work. To simplyfy the code you could create a simple function that looks up the form value, and
then just use the function name whenever you need the value.

"Clive Moss" <cl***@REMOVEinstant-image.co.uk> wrote in message
news:ji*********************@news-text.cableinet.net...
Anyone help a beginner at VBA please?

I am trying to find a way to use a variable throughout an application.

I want to take the value from a log-in form (the user) and use it in most forms that user opens.

I have tried a number of things but none seem to work

TIA

Clive


Nov 12 '05 #4

P: n/a
Thanks again - Just tried it out and it works fine - I was doing something
really stupid before which is too embarrassing and long winded to explain

Clive

"Clive Moss" <cl***@REMOVEinstant-image.co.uk> wrote in message
news:_5*********************@news-text.cableinet.net...
Thanks Andrew - I had tried a version of that idea but I must have done
something else wrong because it didn't work

I'll try it again after dinner!

Clive

"Andrew Smith" <an********@NOSPAM.ntlworld.com> wrote in message
news:5n**************@newsfep4-winn.server.ntli.net...
One way would be to leave the log in form open all the time so the value

can
always be retrieved directly from the form. If the "OK" button on the form
hides the form rather than closing it then this should work. To simplyfy

the
code you could create a simple function that looks up the form value, and then just use the function name whenever you need the value.

"Clive Moss" <cl***@REMOVEinstant-image.co.uk> wrote in message
news:ji*********************@news-text.cableinet.net...
Anyone help a beginner at VBA please?

I am trying to find a way to use a variable throughout an application.

I want to take the value from a log-in form (the user) and use it in

most forms that user opens.

I have tried a number of things but none seem to work

TIA

Clive



Nov 12 '05 #5

P: n/a
Leaving the login form open is a good method. Should you need to have
it closed, or if a user closes it, another method is to store the
values(s) you want to Global (Public) variables as the form closes. I
use this method for exactly the same reason, to store login information
and it has worked well with all my apps. Another alternative is to
write the value(s) to a temp file (.txt) and read the file when needed.
Look for and delete the file at application start up as well as at
application close to ensure the file is created at each application
start.

MPP
Lanexa, Virginia

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 12 '05 #6

P: n/a
I like Peter's idea. If you have to persist it across sessions
(open/close of database) then you should look into writing it to
either a table or a custom database property. Then retrieve it when
the database opens like Peter said.

HTH,
Village Idiot
Nov 12 '05 #7

P: n/a
Clive Moss wrote:
Thanks again - Just tried it out and it works fine - I was doing something
really stupid before which is too embarrassing and long winded to explain

Clive


Unhandled errors (or more exactly - the user resetting code execution) will
reset (or clear) global variables. Perhaps that is what happened to you?
Make sure you have error handlers in your code procedures.

Save the value in a table if you need persistence or consider classes to
prevent this type of problem.

--
'-------------------------------
' John Mishefske
'-------------------------------

Nov 12 '05 #8

P: n/a
Peter Doering <ne**@doering.org> wrote:
You have to declare the variable in a module(!) as Public:

Public YourUserID As String


Trouble is if you're in the middle of running some code, hit a breakpoint or
whatever, and then stop the code you lose all these variables.

Thus I use an always open hidden form to store such variables in.

Tony
--
Tony Toews, Microsoft Access MVP
Please respond only in the newsgroups so that others can
read the entire thread of messages.
Microsoft Access Links, Hints, Tips & Accounting Systems at
http://www.granite.ab.ca/accsmstr.htm
Nov 12 '05 #9

P: n/a
Tony,
Public YourUserID As String
Trouble is if you're in the middle of running some code, hit a breakpoint or
whatever, and then stop the code you lose all these variables.
Sure, that's what I want. I see it as a feature, not a bug. What's wrong
with the user having to login again after a program failure?

Your statement is true also for all other global variables (which should be
avoided in general, I know). Perfect for storage of passwords, aren't they.
Thus I use an always open hidden form to store such variables in.


There's no argument against this method either (unless one prefers global
variables ;-)

Peter

--
No mails please.
Nov 12 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.