global variables cleared after ending debug

Posted on 2013-12-08
Medium Priority
Last Modified: 2013-12-15
hey guys!

when the debug dialog pops up and i choose end, my modules' global variables are cleared. why is this so?

i understand that global variables are like static variables just that the scope is global.

Question --> why are my global variables from ALL my modules lost when i click "end" on the debug dialog box?

is the below a possible explanation then?

1) modules are loaded upon access start up
2) when a bug is hit debug dialog pops up
3) if i choose end, the thread of ALL the modules are stopped
     a) ALL modules are not running anymore
     b) ALL the global variables from ALL the modules are thus lost
4a) when i run my next code by hitting a button or otherwise, the modules are then loaded again


4b) the modules are reset-ted when i hit the end in the debug dialog - e.g. they are stopped immediately and reloaded (but because they did not go through the start up process their global variables are not set)

thanks guys!! = )
Question by:developingprogrammer
LVL 24

Accepted Solution

Bitsqueezer earned 400 total points
ID: 39704035

welcome back...:-)
I thought you've given up Access in the meantime and moved on to Apple...;-)

I cannot say why MS decided to do so, but the thing is that standard modules are loaded immediately after they are compiled. "Compiled" means here not the compiler in the debug menu but the first-step compiler which always run when you enter a line of code and leave that line with the cursor.
When you add a public variable at module level for example and leave that line you can use the immediate window to directly fill a value into this variable. This value keeps intact if you start your code if your code do not change this value so the module will not be loaded again at this point.

When you run into an error and choose "end" then all loaded objects will be removed and also all static class modules (that means: standard modules). As standard modules are always loaded directly they must now be loaded again so that you can access their variables and code like usual. That of course means that any variable value of any standard module is lost.
Own class modules instances are removed so that is clear that there is no value which you can retrieve.
A special case are class modules like forms and reports: The form will not be removed, after stop your form remains intact. But it only seems to be intact, if you look deeper into the functionality you will see that your code you have written to be executed when you click a button for example will no longer run. That's because the form is not an "all in one" class module, it is an object "form" which has an inner object "code behind" - and this inner object will be removed also when you stop the debugger so you must go to design mode and back or close the form and open it again to have a working form.

If that separation would not be the case you would not be able to call a sub or fill a variable value in design mode. Whenever you do so you instantiate the "code behind" class module to a new object so you can work with it in the immediate window. But when you run the form this object is also removed and a new instance of the "code behind" class module will be loaded - you can proof that if you add a public variable to a form, give it a value in the immediate window and then print the variable to the immediate window in Form_Load. The value is empty because the instance does not exist anymore.



Author Comment

ID: 39704127
Christian!!! hello!!! = )) so good to hear from you as the first poster!! = ))

ha i indeed took some time off Access due to the canning of my project due to lack of technical support other than myself, but i've been keeping Access in the back of my mind all the time and every single time i look at an Excel file, 80% of the time i'm thinking it should have been better done in Access (but i've got handcuffs put on me at work cause i'm not from the Tech team ha)

i've spent more time reflecting and also going through articles and videos on architectures, requirements, structural frameworks, code efficiency and more, and two of the things that stand out in my head now are that - laziness is a virtue and embrace business constraints - by writing a "non-100%-reuseable" app, an app that fulfils all the business and user requirements but is 60% in the eyes of reuseability perfection, that is the biggest success for a real world programmer. if we were in academia though, then the emphasis is more towards perfection not just get it done. there of course is a balance and what i just said is an over simplication of things - but just like what you've taught me before, our customers are not paying for code perfection. they'll never even see it. they want to product and now.

my code has become a lot more streamlined with a skeletal structure first and everything built upon that (basically just the error handling module, the error logging, start up procedures, opening a static connection to the BE all these kind of things). and everything is neatly written down in Jama from Business to User to Function + Structural to Technical Requirements with Business to User to Function + Structural to Technical Test Cases and a Defects Tracker (bug tracker database) with release early release often principles in place. things which i don't need to refactor, i just leave it there. it's better to write things perhaps twice or 3 times instead of going on a never ending refactoring - just like what you've taught me previously as well = ) overall, i feel a lot more in control of programming now and the only things that will hold me back would be just experimenting with some lines of code, seeing what works etc. but by really embracing the business constraints and upholding laziness as a virtue, i feel so much more in control over coding now Christian = ))

so long as i work in an office and they are using MS office, i will always be thinking of Access. if i do one day become good enough to release apple apps (early next year is my deadline i give to myself) and support myself without a day job, then perhaps my focus will change. but Access is where i grew up and to an extent i always think in terms of Access (just like how you'd think in terms of German before translating what you want to express in English = ))  )

whao once again Christian, nothing has changed - i still need to re-read your answer 5 times before i can connect the dots in my head ha - which is fantastic! cause you always give all the insights that normal posts on the internet miss out on = ) very fortunate me!! = ))

this inner object will be removed also when you stop the debugger so you must go to design mode and back or close the form and open it again to have a working form.

i tried this out myself to understand it better and came out with a really simple form with a msgbox. however when i included a forced error (debug.print 1/0) and ended the code, i can still click on the button. here's the database attached. could you correct my understanding so that i can get a better grasp of this? thanks so much once again Christian, i'm truly so fortunate!! = ))
LVL 53

Assisted Solution

by:Gustav Brock
Gustav Brock earned 400 total points
ID: 39704139
It's by design. Since the first versions of BASIC.

The command:


stops all execution and clears all variables.

Regard as a feature, and don't call End if you wish to keep the variables. In the debug message box press Debug.

Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

LVL 86

Assisted Solution

by:Scott McDaniel (Microsoft Access MVP - EE MVE )
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 400 total points
ID: 39704200
I can't add much to the "why", as Christian and Gustav have given you everything needed, but in specific regard to the clearing of variables:

I long ago adopted a "solution" of use a hidden form that is opened on startup, and is closed on app shutdown. On that form I keep textboxes and such that store the variables I need to use through my program (sort of like TempVars pre-2007). Those values aren't removed when you hit a debug, and you can refer to them quite easily:

If Forms("MyHiddenForm").txUserGroup < 3 Then
  MsgBox "You can't click that button!"
  '/ do something else here
End If

It's not as elegant as a class module, or reg settings, etc - but it gets the job done in Access, anyway :)
LVL 50

Assisted Solution

by:Dale Fye
Dale Fye earned 400 total points
ID: 39704238
LSM alluded to it, but I don't see any other comments about TempVars, so I will chime in.  Starting in Access 2007, they created TempVars.  TempVars are a variant collection which allows you to store "global" variables (any data type) which retain their scope until the Access application is closed, even through errors.

You instantiate a tempvar with:

TempVars.Add varName, varValue

although if you are going to set the value of the TempVar to the value of a form control, you must specify the "Value" property of the control:

Tempvars.Add "StartDate", me.txtStartDate.Value

One of the nice things about TempVars is that you can use them in queries:

WHERE [StartDate] = [tempvar]![startDate]

With global variables, you had to create a function that passed back the value of the global variable.

Author Comment

ID: 39704403
whao nice, LSMConsulting and fyed! = )

LSMConsulting i like your method of using a hidden always open form. i think that is something that i should involve in my skeletal framework for all my Access programmes moving forward. i just need to think how i would like to use it - but that way you've shared is definitely a very good way and a great starting point! = )

fyed, i've read up about tempvars before (i think it was you and a few other experts who spoke about this before) and i think it's a fantastic feature however i need the programme to be compatible with both Access 2003 and 2007 so i will have to use the "technology" common to both version of Access. but great point nonetheless and something i will bear in mind! = )

whao gustav i definitely won't have a clue about the first version of BASIC and thankfully you brought it up ha, if not it'll remain in the forest for me! yup i should be aware that the End button of the debug dialog is the same as the End statement which stops execution and clears the variables.

thanks guys!! = ))
LVL 41

Assisted Solution

PatHartman earned 400 total points
ID: 39704881
I have used the hidden form method for years.  I didn't convert to using TempVars because the hidden form has some advantages.  The most important of which is you can make it visible so you can see your current values and you can change them if you need to for testing.

Author Comment

ID: 39719891
ok guys, thanks for all your help and thanks pat too! i wanted to follow up with Christian on the code behind test database i uploaded but i guess i'll do it later in another question = ) thanks everyone once again!

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

When we develop an application in Ms Access 2016 we should also try to protect the queries, macros and table links. I know I may not have a permanent solution but for novice users, they will not manage to break your application. Below is the detail …
If you need to implement application level security in an Access database application or other VBA code, I strongly encourage you to take advantage of Active Directory groups.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…

589 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question