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 Developer Forum    Comments List
Upload Images   @Reply   Bookmark    Link   Email   Next Unseen 
Public Static Sub
Sandra Truax 
         
11 days ago
I just came across this (Public Static Sub) in the VBA Editor, and according to CoPilot, Static means that all local variables inside the Sub retain their values between calls, instead of resetting each time the Sub runs, and that they have module-level lifetime (they persist as long as the module is loaded), so I was wondering if I run a procedure a LOT that I DIM a variable in, if I would be better off using the STATIC Sub, or is this another one of the things that needs to be avoided?
Richard Rost  @Reply  
          
11 days ago
That's a great question, because this is one of those VBA features that exists but almost nobody talks about.

What Copilot told you is technically correct. A Static Sub just means that every local variable inside that procedure behaves as if it were declared Static, so it retains its value between calls instead of resetting each time the procedure runs.

However, just because you can use it doesn't mean you should. In practice, most developers avoid Static Subs and instead use Static only on the specific variables that actually need to persist. The reason is clarity and control. If you mark individual variables as Static, it's obvious what's being remembered between calls and what isn't. With a Static Sub, everything is persistent whether you intended it or not, which can lead to confusing bugs later when values don't reset like you expect. Which is why you should always initialize variables inside your subs - except of course ones you want to remain Static.

There's also the issue of hidden state. When a procedure quietly remembers values from previous runs, it can make debugging a lot harder, especially if someone else is reading your code or if you come back to it months later wondering why something isn't behaving correctly.

Static variables themselves are useful in certain cases, like counters, simple caching, or remembering the last value processed. But making an entire procedure static is usually overkill and removes a lot of control.

So the short answer is: you're better off sticking with regular Subs and only using Static on the specific variables that need it. It keeps your code more predictable, easier to read, and much easier to maintain. And honestly, I much prefer TempVars for things like this. Lots of benefits to them (video explains).

Plus TempVars make Adam happy.
Sandra Truax OP  @Reply  
         
11 days ago
Thanks for clearing it up. I will definitely avoid using these and I agree with Adam, TempVars are awesome!
Add a Reply Upload an Image
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/11/2026 1:46:38 AM. PLT: 1s