PBeezy
PBeezy

Reputation: 1272

How to save a variable in between application sessions [Access VBA]

I have a variable that stores a file path. If the file is missing, the application prompts the user to select a new file. How can I save the new file path for the next time the user opens the program?

I know I could write the file path to a table and then retrieve it at runtime, but is there a way to do it strictly in VBA code?

Upvotes: 1

Views: 972

Answers (1)

MatthewD
MatthewD

Reputation: 6761

As Alex K. said. This will save and retrieve values from the registry. For NameOfYourProject you might want to use the spreadsheet name or something.

SaveSetting "NameOfYourProject", "frmMain", "LastPath", szLastPath

szLastPath = GetSetting("NameOfYourProject", "frmMain", "LastPath", 0)

Here are the parameters for SaveSettings.

AppName
Required. String expression containing the name of the application or project to which the setting applies.

Section
Required. String expression containing the name of the section in which 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 to which Key is being set.

And these for GetSetting

AppName
Required. String expression containing the name of the application or project whose key setting is requested.

Section
Required. String expression containing the name of the section in which 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 ("").

Upvotes: 1

Related Questions