Make an Access VBA Class Instance Available Across Forms
Question details
Make a VBA class instance that stores a form name accessible to multiple Access forms.

- Product
- Microsoft Access
- Device & OS
- not provided
- Scenario
- One form stores a form name in a class instance, and another form needs to read it.
- Observed behavior
- The instance is declared inside an event procedure, so its variable goes out of scope when the procedure ends.
Open the Access database and press Alt+F11 to inspect the class module and form event code. Identify the class name and its public form-name property or member; the example below uses clsFormName and FormName.
Declare and initialize the class instance in a standard module
Use a public variable in a standard module when multiple forms or procedures need to access the same class instance.
A variable declared inside Form_Open is local to that procedure. A public variable in a standard module is available throughout the VBA project, but the object must still be initialized before its members are used.
Replace FormName in the examples with the actual name of the class's public property or member.
In the VBA editor, choose Insert > Module. In the new standard module's Declarations section, add: Public g_pFormName As clsFormName
In the source form's event procedure, initialize the object if necessary and set its public member before opening the target form: If g_pFormName Is Nothing Then Set g_pFormName = New clsFormName End If g_pFormName.FormName = Me.Name DoCmd.OpenForm "TargetForm" Replace TargetForm with the name of the form you want to open.
In the target form's Form_Open event, check that the instance exists before reading it: If g_pFormName Is Nothing Then MsgBox "The shared form name has not been initialized." Else MsgBox g_pFormName.FormName End If
In the VBA editor, choose Debug > Compile, then open the source form and trigger the event that opens the target form. Confirm that the target form displays the stored name.

Use WPS Office for compatible documents and spreadsheets
WPS Office is a free, lightweight alternative with a familiar interface for working with supported Office files. It can help you move document, spreadsheet, and presentation work from Microsoft Office, but it does not fix Access VBA scope issues or replace Access databases.

Frequently Asked Questions
Why can't another form see a variable declared in Form_Open?
A variable declared inside Form_Open has procedure scope and goes out of scope when the event ends. Declare the shared instance as Public in a standard module to make it available across the VBA project.
Does declaring a public class variable create the object?
No. After declaring Public g_pFormName As clsFormName in a standard module, use Set g_pFormName = New clsFormName before accessing the object's members.
What if only events in the same form need the instance?
Declare the variable in that form module's Declarations section, outside any procedure. Initialize it before use, and the form's event procedures can share it.
Why is the shared class variable Nothing after a VBA reset?
A project reset clears public variables. Initialize the instance again with Set before reading its properties, and check If g_pFormName Is Nothing when the target form may open independently.




