Solved

global variables cleared after ending debug

Posted on 2013-12-08
8
790 Views
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

OR

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!! = )
0
Comment
Question by:developingprogrammer
8 Comments
 
LVL 24

Accepted Solution

by:
Bitsqueezer earned 100 total points
Comment Utility
Hi,

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.

Cheers,

Christian
0
 

Author Comment

by:developingprogrammer
Comment Utility
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!! = ))
Form-and-Code-Behind.mdb
0
 
LVL 49

Assisted Solution

by:Gustav Brock
Gustav Brock earned 100 total points
Comment Utility
It's by design. Since the first versions of BASIC.

The command:

  End

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.

/gustav
0
 
LVL 84

Assisted Solution

by:Scott McDaniel (Microsoft Access MVP - EE MVE )
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 100 total points
Comment Utility
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!"
Else
  '/ 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 :)
0
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 
LVL 47

Assisted Solution

by:Dale Fye (Access MVP)
Dale Fye (Access MVP) earned 100 total points
Comment Utility
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.
0
 

Author Comment

by:developingprogrammer
Comment Utility
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!! = ))
0
 
LVL 34

Assisted Solution

by:PatHartman
PatHartman earned 100 total points
Comment Utility
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.
0
 

Author Comment

by:developingprogrammer
Comment Utility
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!
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now