We help IT Professionals succeed at work.

How to save the state of all variables

How to save the state of all variables
Comment
Watch Question

You need to provide some more information.

What programming language?
What type of application (web, stand-alone, etc)?
What are your variables used for?
How long do you need them to persist (between pages, over multiple sessions, set time, etc)?
Are you connecting to a database (MS-SQL, MySQL, Access, Oracle, etc)?

I am sure that will be more questions, but these can get us started.

Author

Commented:
Yes, I should have given more info. I am in VB6 using an Access database. Stand alone app. It is a large legacy program with hundreds of Global variables and I need to safe the state of the program.
Of course I know I could just make a list of them and save them to an Access table. What I was really hoping for probably is not possible. Something like a:
for each global variable in my prog
       (Save it)
next

The Access file would contain a number of states, probably limited to about 10 and a given user might have 5 to 50 such databases as they are really files of specific shipments of goods.. They are all quite small.
Thanks for any help. Sorry for delay, I did not see your response until now for some reason.
Brian
Michael FowlerSolutions Consultant

Commented:
To save the variables across sessions you will need to either store them in a table or you could store them in a text file. If you saved them in the format
   variablename1,variablevalue
   variablename2,variablevalue
   ...
You could load them into a Dictionary collection quite easily, access the variables by name from the the dictionary and then save them again without too much trouble

Here is a link on the Dictionary object
http://support.microsoft.com/kb/187234

Michael

Most Valuable Expert 2012
Top Expert 2012

Commented:
There are a couple ways to go at this.  First, to save the state of ALL variables, you'll need to do work on EACH variable to ensure its state is saved.

One approach:

Create a name (e.g., Excel) with the variable's name - then on workbook_close event, just update the name with the latest value.

Alternatively, and this should work for any platform, just create a Registry Setting for each variable.

Here's a couple VBA examples for Saving and Retrieving a saved Registry Setting...

http://j-walk.com/ss/excel/tips/tip60.htm
http://msdn.microsoft.com/en-us/library/aa164019(v=office.10).aspx#odc_tentipsvba_topic8


From within VBA if you're developing in Office, just type the command SaveSetting and put the cursor over it and hit F1 for complete help details.

For SaveSetting, Excel Help gives me:

SaveSetting Statement
Saves or creates an application entry in the application's entry in the Windows registry or (on the Macintosh) information in the application's initialization file.

Syntax

SaveSetting appname, section, key, setting

The SaveSetting statement syntax has these named arguments:

Part Description
appname Required. String expression containing the name of the application or project to which the setting applies. On the Macintosh, this is the filename of the initialization file in the Preferences folder in the System folder.
section Required. String expression containing the name of the section where the key setting is being saved.
key Required. String expression containing the name of the key setting being saved.
setting Required. Expression containing the value that key is being set to.


Cheers,

Dave

For GetSetting:

GetSetting(appname, section, key[, default])
The GetSetting function syntax has these named arguments:

Part Description
appname Required. String expression containing the name of the application or project whose key setting is requested. On the Macintosh, this is the filename of the initialization file in the Preferences folder in the System folder.
section Required. String expression containing the name of the section where the key setting is found.
key Required. String expression containing the name of the key setting to return.
default Optional. Expression containing the value to return if no value is set in the key setting. If omitted, default is assumed to be a zero-length string ("").

Cheers,

Dave
Most Valuable Expert 2012
Top Expert 2012

Commented:
PS - GetSetting and SaveSetting commands will also work in VB6

A couple VB6 tips:
http://msdn.microsoft.com/en-us/library/3kz7fyks(v=vs.80).aspx
http://www.vbforums.com/showthread.php?t=232113

Dave
Retired
Top Expert 2012
Commented:
There is no variables collection in VB6, so you can't do a For Each walk-through.

You convert to using a few variable arrays (one for string, one for integers, etc) and enumerate the indexes. You could use Find and Replace to achieve this with an existing project. However, it would be as much work as simply writing a save instruction for each variable.
Option Explicit

Enum strs
    FileName
    FilePath
    UserName
End Enum

Dim MyStrings(UserName) As String

Sub Setvalues()
    MyStrings(FileName) = "abc.txt"
    MyStrings(FilePath) = "C:\MyFolder"
    MyStrings(UserName) = "John Smith"
End Sub

Open in new window

Most Valuable Expert 2014

Commented:
<<Of course I know I could just make a list of them and save them to an Access table. What I was really hoping for probably is not possible. Something like a:
for each global variable in my prog
       (Save it)
next
>>

You are using VB proper so you are no novice
Look here
http://pubs.logicalexpressions.com/pub0009/LPMArticle.asp?ID=307
Basically you could write code that would parse the code for the global variables
You could then use code to write code, line by line, that would save out your variables and values--to a table or wherever.
Hope this helps



If the datasource file (the .MDB) is local to the user, then I would suggest you take a look at using database properties.  Check out the link below on the different types of properties and how to manage them with code ...

http://www.utteraccess.com/forum/index.php?s=&showtopic=1513778&view=findpost&p=1513840
PS> If you don't want to sign up at UA, and you would like the code, let me know and I will post it in this thread.

Author

Commented:
(Originsally I had given others some pounts too but the system did not seem to know I had responded and closed the question)
Graham understood my  desire for a simple way that does not exist and let me know so. The alternate he offered is styaright and understandable, and included code.
GrahamSkanRetired
Top Expert 2012

Commented:
Thank you,

Compliments of the season

All the best, Graham
Glad EE, and Graham specifically, was able to help you acheive your desired result! Good luck on your project!