473,385 Members | 1,888 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,385 software developers and data experts.

Declaring Variables

Can someone describe for me with code how I would initialize a variable in a Private Sub, for example, a form's ON OPEN procedure and carry that value throughout ALL my other procedures used with other forms? I used the statement 'PUBLIC mUser as string' in my frmMainMenu. After the user enters her initials, I want mUser to get this value AND I want to use that value again in other forms until the user closes the access application. Thank you for any help!
Oct 20 '06 #1
6 1476
MMcCarthy
14,534 Expert Mod 8TB
You cannot declare a global variable in a private sub

Declare it in a module as:

GLOBAL mUser As String

You can then set it and use it anywhere


Can someone describe for me with code how I would initialize a variable in a Private Sub, for example, a form's ON OPEN procedure and carry that value throughout ALL my other procedures used with other forms? I used the statement 'PUBLIC mUser as string' in my frmMainMenu. After the user enters her initials, I want mUser to get this value AND I want to use that value again in other forms until the user closes the access application. Thank you for any help!
Oct 20 '06 #2
thank you for your help...I am a little confused on the proper way to declare and call for variables in modules. When a user opens my database, I want the program to ask for initials. I use the following code:

mUser = InputBox("Please sign in: ", "User Sign In", , 5000, 5000)
If mUser <> "JG" And mUser <> "jg" And mUser <> "laa" And mUser <> "LAA" Then
MsgBox "Incorrect Entry!", vbCritical
DoCmd.Close acForm, "frmMain"
End If
If mUser = "LAA" Or mUser = "laa" Then
mUser = "Leslie"
Else
If mUser = "JG" Or mUser = "jg" Then
mUser = "Galper@Vodia"
End If
End If

I am guessing that I need this to NOT run in my form, BUT create a SUB in a module. I have done this...

Option Compare Database

Global mUser As String

Public Sub DetermineUser()
mUser = InputBox("Please sign in: ", "User Sign In", , 5000, 5000)
If mUser <> "JG" And mUser <> "jg" And mUser <> "laa" And mUser <> "LAA" Then
MsgBox "Incorrect Entry!", vbCritical
DoCmd.Close acForm, "frmMain"
End If
If mUser = "LAA" Or mUser = "laa" Then
mUser = "Leslie"
Else
If mUser = "JG" Or mUser = "jg" Then
mUser = "Galper@Vodia"
End If
End If
Debug.Print mUser
End Sub

If this is correct, how do I RUN this MODULE to obtain mUser AND how do I retrieve mUser in all the forms that I need it for? Thank you in advance for all your help!!
Oct 23 '06 #3
MMcCarthy
14,534 Expert Mod 8TB
This code has to be declared in a module but it can be called from a form. I would change the procedure to a function just to make it easier to call.

In the module:

Expand|Select|Wrap|Line Numbers
  1.  
  2. Option Compare Database
  3.  
  4. Global mUser As String
  5.  
  6. Function DetermineUser()
  7. mUser = InputBox("Please sign in: ", "User Sign In", , 5000, 5000)
  8. If mUser <> "JG" And mUser <> "jg" And mUser <> "laa" And mUser <> "LAA" Then
  9. MsgBox "Incorrect Entry!", vbCritical
  10. DoCmd.Close acForm, "frmMain"
  11. End If
  12. If mUser = "LAA" Or mUser = "laa" Then
  13. mUser = "Leslie"
  14. Else
  15. If mUser = "JG" Or mUser = "jg" Then
  16. mUser = "Galper@Vodia"
  17. End If
  18. End If
  19. Debug.Print mUser
  20. End Function
  21.  
  22.  
Have a form set to open on startup of the database. This is usually the switchboard or main menu. In the On Load event of this form put the following.

Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Sub Form_OnLoad()
  3.  
  4.    DetermineUser
  5.  
  6. End Sub
  7.  
  8.  
This will run the function which will assign the value to the variable mUser. It will keep this value unless you change it or the database closes and you can reference it anywhere in the database.
Oct 23 '06 #4
Thank you again for your help...this worked perfectly!
Oct 23 '06 #5
One last question I came up with...I don't want the user to have to sign in upon the load of each form. I only want them to sign in ONCE when frmMainMenu opens. I need to tell each form what the value of mUser is. would I do this with the SET command like you had mentioned? what would the code be and where would I declare this variable in each form?

thank you again!...a very slow learner!!
Oct 24 '06 #6
MMcCarthy
14,534 Expert Mod 8TB
One last question I came up with...I don't want the user to have to sign in upon the load of each form. I only want them to sign in ONCE when frmMainMenu opens. I need to tell each form what the value of mUser is. would I do this with the SET command like you had mentioned? what would the code be and where would I declare this variable in each form?

thank you again!...a very slow learner!!
The user signs in just once. That value is available anywhere and doesn't have to be declared again. The value will not change as you open and close forms. For instance you can pass the following SQL statement anywhere in code.

"INSERT INTO tblName (UpdatedByField) VALUES (mUser);"

Or set the value on a textbox using code:

Me.textBoxName = mUSer

The only restriction is that it cannot be referenced directly in a query design. But this can be got around by using a function to return it. The following function should be put in a module.

Function getUser() As String

getUser=mUser

End Function

You still can't reference it directly in the where statement but you can get around this by referencing it in the SELECT as follows:

SELECT IIf([UpdatedByField]=getUser(),[UpdatedByField],Null) As tmpA
FROM tblName
WHERE tmpA Is Not Null;

The only way this value will change is if another user signs in or the database closes.
Oct 25 '06 #7

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

Similar topics

10
by: Stefanie | last post by:
Hi, I have a question about declaring variables for writing data to a file for random access. I have two string variables declared in a module (user-defined types). The point is that for one of...
2
by: Oliver Corona | last post by:
I am wondering if anyone has any insights on the performance benefit (or detriment) of declaring local variables instead of referencing members. Is allocating memory for a new variable more...
2
by: ross.oneill | last post by:
Hi, I am having trouble with a simple task of declaring a variable. Is this possible? Here is what I want to do. DECLARE start_date date; DECLARE end_date date; SET start_date =...
1
by: ColinWard | last post by:
Hi guys. I have a question about declaring variables. I do a lot of re-querying of controls in my database and I use the Set statement with a variable set to the name of the control to tell the...
5
by: param | last post by:
Declaring struct as static is creating problem with newer version of CC compiler 5.7 in solaris. e.g. static struct new_str { int a; int b; };
6
by: Mark A. Sam | last post by:
Hello, I am using Visual Web Developer 2005 Express. I want to declare a varible, using Visual Basic as the language and can't get anywhere. For example Public Test1 as String I'll get en...
3
by: jbeteta | last post by:
Hello, I have a problem declaring variables. I need to create an object oRpte as ReportClass on WebForm1.aspx and be able to use its value on WebForm2.aspx. For declaring the property oRpte()...
2
by: TARUN | last post by:
I have a question about declaring the Global.ascx: In VS.NET I create a Solution, and there are 4 projects under it. They are put under 4 folders, but web pages would be called across the 4...
8
by: SM | last post by:
I've always wonder if there is diference when declaring and initializing a varible inside/outside a loop. What's a better practice? Declaring and initializing variables inside a loop routine,...
6
by: =?Utf-8?B?QUw=?= | last post by:
Hi I usually stick to the convention of not declaring variables in my bodies of "loops" (including foreach) ie int x; for (int i = 0; i < 10; i++) {
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...

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.