Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   TechHelp   Forums   Help   Contact   Merch   Join   Order   Logon  
 
Back to Access Forum    Comments List
Upload Images   @Reply   Bookmark    Link   Email   Next Unseen 
global variables vs public variables
William B 

12 years ago
I wanted to have the users log in and keep the username and id available for other forms and procedures. I went to my main Module (not tied to a form) and declared two public variables. I then created a small sub to initialize the values of these variables. The sub was called when the main menu loaded.

It does not work. I've tried everything I can think of and the variables do not retain their values. What am I doing wrong. Is there another way to do this without referring back to a main form or passing values with OpenArgs?

Here is my code:
Public p_strUserName As String
[in the declaration area of a separate module]
Public p_intUserID As Integer

[below in that same module]
Public Sub InitializePublicVariables()
    p_strUserName = "Bill"
    p_intUserID = 5
End Sub



Reply from Richard Rost:

You have to use the keyword GLOBAL in your variable declaration.

Global Username As String

The PUBLIC keyword is something you use in a FORM module so that other forms can use that value. If you want a variable to exist outside of, and to be used by, all objects in the database, you have to declare it in a standard module and use the Global keyword.

This thread is now CLOSED. If you wish to comment, start a NEW discussion in Access Forum.
 

Next Unseen

 
New Feature: Comment Live View
 
 

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
PCResale.NET
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2026 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 5/31/2026 10:07:09 PM. PLT: 1s