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!

Defining a global variable 1

Status
Not open for further replies.

bscs1963

Programmer
Apr 7, 2009
22
US
How do I assign a variable globally so it is recognized on every form in the database. My users work under a specific account and I want each form to remember that account until the user changes the account.

The plan is that when the user goes to the main screen, they will select the account number they want to work in. Every form will remember the account number, and code will control the data by account. When they are ready they will be able to go back to the main screen and change the account.
 

Add a standard Module, and do:
Code:
Option Explicit
...
Public lngAccNo As Long
...
lngAccNo will be available everywhere in your application, you can assign the value to it and be able to see its value.

Have fun.

---- Andy
 
or you could write the data to a table

HTH << MaZeWorX >> "I have not failed I have only found ten thousand ways that don't work" <<Edison>>
 
I want to set the value as a string rather than a number.

I set up the following module
_________________________________________
Option Explicit

Public txtAccountNo As String

txtAccountNo = "8610"
_________________________________________

I get a response "Invalid Outside Procedure".

I am rather new to definging gloabal functions so any help would be great
 
I would actually recommend Tempvars, introduced in Access 2007 over variables as they are less volatile as they do not clear under certain error states. I am sure there is plenty of help inside Access on it.

As for your error... your last line is not in a procedure of any kind.
 
You define what a global variable is outside of a procedure, but you must set it to a value inside a procedure

Set the value on the initial load of your main form.
If you want to be fancy, and save the value between sessions, you could use the GetSetting/SaveSetting commands to safe the info in the registry
 

I have txt as a prefix for my text boxes, so I use str for my variables instead.
Code:
Option Explicit

Public strAccountNo As String

Publis Sub SomethingOrOther()

strAccountNo = "8610"

End Sub
So when you call the sub called SomethingOrOther, you can assign value to strAccountNo, which will not be hard-coded, I hope :)

BTW, hard-coded values are kept in Constants:
[tt]
Public Const txtAccNo As String = "8610"[/tt]

Have fun.

---- Andy
 
Two clarifications... First I would use the TempVars collection instead of GLOBAL variables (I left global out above).

Andy's code has a typo... "Publis Sub" should be "Public Sub".
 

Yes, it is a type.
I am not good at typing, but can code faster than my co-workers who are a lot better typist than me. Go figure....
:)

Have fun.

---- Andy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top