When to use global variable

BiigJiim

Registered User.
Local time
Today, 13:22
Joined
Jun 7, 2012
Messages
114
Hi,

I have an application where several different procedures are run repeatedly every xx seconds to get live updates from a server (different procedures depending which form the user currently has open.) Within each procedure, a separate single procedure is called only if a stored boolean setting is set as true. The setting itself is only changed very infrequently, such as on application startup, but needs to be initially set (i.e. it cannot just have a default of False if it is not set.)

Because of this I am storing the setting in a single record table and have a function 'UsingWPilot' to return the boolean value. However, given the frequency that the setting needs to be accessed, and the fact that speed is crucial in the running of the app, I am considering adding a global string variable and modifying the function to look at the variable (possible values 'N' and 'Y') and only going to the table if the variable has not been set.

I have heard various things about how you should never if possible use global variables. My question is: is it faster to use a global variable than to open the table record each time? And is there a better way of doing this?

e.g. Option 1

Code:
Public Function UsingWPilot() As Boolean

    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    
    Set db = CurrentDb
    Set rs = db.OpenRecordset("rbl_Settings_System")
    
    If rs.EOF Then
        MyMsgBox "WPilot Settings Error", "WPilot Use settings have not been set.", "OK"
    Else
        rs.MoveFirst
        UsingWPilot = rs("UseWPilot")
    End If
    
    rs.Close
    Set rs = Nothing
    Set db = Nothing
    
End Function

Option 2:

Code:
Public Function UsingWPilot() As Boolean

    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    
    If Len(g_WPilot) > 0 Then
        UsingWPilot = (g_WPilot = "Y")
    Else
        Set db = CurrentDb
        Set rs = db.OpenRecordset("tbl_Settings_System")
        
        If rs.EOF Then
            MyMsgBox "WPilot Settings Error", "WPilot settings not found.", "OK"
        Else
            rs.MoveFirst
            If rs("UseWPilot") = True Then
                g_WPilot = "Y"
                UsingWPilot = True
            Else
                g_WPilot = "N"
                UsingWPilot = False
            End If
        End If
        
        rs.Close
        Set rs = Nothing
        Set db = Nothing
    End If
    
End Function
 
It is an appropriate use for a Global variable because its intended scope is the whole project.

However, since there are only two values, I would use a Boolean rather than a string.
 
Thanks guys. Reassuring to know I am heading in the right direction.

I would probably use one in this instance. Just discovered ChrisO's CacheDatabaseProperties which may be an option too:

http://www.baldyweb.com/ChrisOSamples.htm

Some really interesting an useful snippets here!


It is an appropriate use for a Global variable because its intended scope is the whole project.

However, since there are only two values, I would use a Boolean rather than a string.

Thanks Galaxiom. The only reason I am not using a Boolean is because I need to differentiate between the variable not being set and the variable being set to False. Boolean variables have a default value of false and it is impossible to differentiate.
 

Users who are viewing this thread

Back
Top Bottom