Class Modules – Global Class Modules

One of the best things about working with MS Access is the ability to use global variables, functions and procedures. This means that we are able to call globally declared code from anywhere within the application and is very useful for being able to keep code centralised yet accessible. Oh, and by the way, we can do the same with class modules…

To create a public variable, we simply add a variable to the top of a standard module (make sure it is not inside a function or sub-procedure) and use the public keyword when we declare it. We can do a very similar thing with class modules.

Globally Declaring A Class Module

First, we are going to create a class called clsFunctions. Add this code to the class:

Public Function getUserName()
On Error GoTo ErrorHandler

getUserName = Environ$("username")

Exit_Function:
    Exit Function

ErrorHandler:
    getUserName = "Unknown"
    Resume Exit_Function

End Function

Public Function getComputerName()
On Error GoTo ErrorHandler

getComputerName = "Terminal: " & Environ$("computername")

Exit_Function:
    Exit Function

ErrorHandler:
    getComputerName = "Unknown"
    Resume Exit_Function

End Function

Public Function IsLoaded(strFrmName As String) As Boolean
IsLoaded = False
Const conFormDesign = 0
Dim intX As Integer

For intX = 0 To Forms.Count - 1
    If Forms(intX).FormName = strFrmName Then
    If Forms(intX).CurrentView <> conFormDesign Then
    IsLoaded = True
    Exit Function
    End If
    End If
Next

End Function

This class module contains three functions: getUserName (returns the name of the current user of the PC), getComputerName (returns the name of the computer) and isLoaded (returns true or false depending on whether a form is loaded).

How these functions work is irrelevant! A standard idea that we have written about but worth repeating here is that the inner-workings of a class are of no concern other than to the developer who wrote it. We only want to know that the methods work correctly! (Obviously, if you want to know how they work as part of your development as a programmer, have a play around with them.)

Now add a standard module called modTestGlobalClass and add this code:

Public cfunctions As New clsFunctions

Public Sub testFunctions()

Debug.Print "The name of my computer is " & cfunctions.getComputerName
Debug.Print "My username is " & cfunctions.getUserName
Debug.Print "Is FilmsDS loaded: " & cfunctions.IsLoaded("FilmsDS")

End Sub

This code will print out some data to your immediate window. Your data will look something like this:

The name of my computer is Terminal: ROBERTAUSTIN-PC
My username is Robert Austin
Is FilmsDS loaded: False

Note: Your results will be different!

The Explanation

We are actually more concerned with how the standard module interacts with the class module than how the methods in the class module work.

 Public cfunctions As New clsFunctions

This is the line of code that ensures your clsFunctions class is visible everywhere in your application. It is declared at the top of the module, outside of any functions or sub-procedures and can actually be seen from other modules.

Note: To make a class available to a single module, declare it in the same place but use the Private modifier.

Debug.Print "The name of my computer is " & cfunctions.getComputerName
Debug.Print "My username is " & cfunctions.getUserName
Debug.Print "Is FilmsDS loaded: " & cfunctions.IsLoaded("FilmsDS")

Now the class is globally available, we can reference it from anywhere.

The Conclusion

Using classes globally can be a great way to keep your most well-used functions easily accessible. As you progress, you will build up a library of class module-based functions that you use frequently and when you build new applications, you can import these class modules and have all the functions immediately available! Now, that is smart coding.

2 Comments

  1. Hi Long,

    Take a look at video ’05/28 – Variable Scope – MS Access VBA Intermediate Tutorials’ for a good explanation of declaring Private and Public variables

  2. Hello,

    I’m new to access and just joined you as an annual-level member. this video doesn’t include the important features that teach me how to open the editor, link this codes to a form, and run and step through the codes.
    I want to be able to create a variable that I can refer to from many form in the application until I renew its value or logout. Can you show me?

    Thanks,

    Long

Leave a Reply to LONG NGUYENCancel Reply

Your email address will not be published. Required fields are marked *

Visit Us On TwitterVisit Us On FacebookVisit Us On Youtube