Search code examples
vbacheckboxregistry

How does a boolean control's value get handled across system locales?


I have a simple bit of VBA code in a form that saves the current boolean state of a CheckBox control to the VBA area of the Windows registry:

SaveSetting sAppName, sSection, sKey, CheckBox1

Similarly, the state of the CheckBox is initialised, with a default value of True, as follows:

CheckBox1 = GetSetting(sAppName, sSection, sKey, "True")

This works as expected until I discovered that the SaveSetting line fails with err 94 "Invalid use of null" on a Dutch user's PC.

After checking the Dutch user's registry, the value was set to "Waar" which is Dutch for True. So, when the CheckBox is initialised, it's value is being set to "Waar" and then saving the setting fails as "Waar" is not recognised as True and the CheckBox value is returned as Null.

I was surprised to see the boolean value being saved locale-aware although I have been unable to replicate the behaviour by setting my system locale to Dutch.

What is going on here and what is the correct way to avoid this issue?


Solution

  • Not come across that but maybe (I don't have a Dutch PC!)

        Private Sub UserForm_Initialize()
        Me.CheckBox1 = GetSetting("TEST", "Section", "Key", True)
        End Sub
    
        Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
        If Me.CheckBox1.Value = -1 Then
        SaveSetting "TEST", "Section", "Key", True
        Else
        SaveSetting "TEST", "Section", "Key", False
        End If
        End Sub