Storing Data in the Registry

You can store information relating to your Excel application in the registry. VBA provides an easy way to set and retrieve that information via the SaveSetting and GetSetting statements.

The arguments for these statements are AppName, Section, Key, and Setting. All of these arguments are data that you create. That is, there’s no right or wrong AppName, it’s whatever you want it to be. To be fair, there are some situations where that data can be wrong. First, when you using GetSetting, you have to specify the correct arguments ñ the same ones you used when with SaveSetting. If you don’t, you won’t get the correct information. Also, if you use the same arguments as some other program that’s storing information in the registry, you will overwrite it/get the wrong data and generally create havoc. To avoid that, make sure you are using unique AppNames.

With SaveSetting, VBA creates the registry entry if it doesn’t exist. If the entry doesn’t exist when you use GetSetting, you get an empty string.

Below are three subs that demonstrate the use of GetSetting and SaveSetting. The sub ChangeName saves a name to the registry and the function GetName retrieves it.

Sub ChangeName(sName As String)

    SaveSetting appname:=”Euchre”, _
        section:=”PlayerData”, _
        key:=”Name”, _
        setting:=sName
    
End Sub

Function GetName()

    GetName = GetSetting(appname:=”Euchre”, _
        section:=”PlayeData”, _
        key:=”Name”)
        
End Function

Sub TestIt()

    ChangeName “Dick”
    MsgBox GetName
    
End Sub

Once ChangeName is run, the registry looks like this:

Registry1

Posted in Uncategorized

3 thoughts on “Storing Data in the Registry

  1. It is a nice build-in functionality, unfortunatly it is not possible to change the location of which the place where the registry settings are being stored. I mostly use the registry class, because then I can store the settings within my own folder within the software section…

  2. section:=”PlayeData”

    is not correct, it have to be:

    section:=”PlayerData” with “r”

    Function GetName()

    GetName = GetSetting(appname:=”Euchre”, _
    section:=”PlayeData”, _
    key:=”Name”)

    End Function


Posting code? Use <pre> tags for VBA and <code> tags for inline.

Leave a Reply

Your email address will not be published.