Introduction.
Over the past few weeks, we have been learning about the dot (.) separator and the exclamation (!) symbol in VBA object references. Now, we will explore some interesting techniques with Forms in VBA, specifically:
How to call a function procedure embedded in a form’s module (class module) from the code outside that form?
How to open multiple instances of a single Microsoft Access Form in memory, each displaying different information?
For example, the Screenshot below shows two instances of the Employees Form. The first instance is behind the second, displaying employee details for IDs 4 and 5. Click the image to enlarge for a clearer view.
Calling the Form's Class Module Public Function.
- How to call a Function Procedure on the Form's Class Module, from outside the Form?
Call the Function from a Standard Module, from the Module of another form, or from the VBA Debug Window (Immediate Window). The target form must be opened in memory to call the function procedure of the form from outside.
Function procedures in a form module are useful for avoiding code duplication. They can be called from subroutines in different parts of the same form, from a command button click, or from other event procedures within the form. A function procedure in a form module can perform calculations, validation checks, update information, or act as a standalone operation—such as the one we will use in our sample Employees Form.
All event procedures in a form module are automatically declared as Private Subroutines, and they begin and end with standard statements, as shown in the sample below. Any custom VBA code that performs specific tasks should be placed within this block:
Private Sub Command8_Click()
.
.
End Sub
A Private subroutine or function is limited in scope to the module in which it is declared and cannot be called from outside that module. Declaring procedures as private is essential to avoid name conflicts with procedures of the same name in other Class Modules or Standard Modules.
To call a function procedure from outside a Form, the Function must be declared as Public in the Form’s module.
To perform a trial run of the above trick, you need the Employees Table and a Form.
Import the Employees Table from the Northwind sample database.
Click on the Employees Table to select it.
Click on Create Ribbon.
Select the Form option and create a Form for Employees Table, in the format shown above.
Save the Form with the name frmEmployees.
Open the frmEmployees Form in Design View. Set the Form Property 'Has Module' Value to Yes.
Select the Design Menu and select VBA Code from the Tools button group to open the Form Module.
Copy the following VBA code and paste it into the VBA Module of the Form.
Public Function in Form Class Module.
Public Function GetFirstName(ByVal EmpID As Integer) As String Dim rst As Recordset, crit As String Dim empCount As Integer 'get total count of employees in the Table empCount = DCount("*", "Employees") 'validate employee code If EmpID > 0 And EmpID <= empCount Then crit = "ID = " & EmpID Set rst = Me.RecordsetClone rst.FindFirst crit If Not rst.NoMatch Then Me.Bookmark = rst.Bookmark GetFirstName = rst![First Name] End If rst.close Set rst = Nothing Else MsgBox "Valid Employee IDs: 1 to " & empCount End If End Function
Save and Close the Form.
Notice that the starting line of the above function is declared as Public.
The function GetFirstName() accepts EmployeeID as a parameter, locates the corresponding record on the form, and makes that record the current record. If the search is successful, the function returns the employee’s first name to the calling procedure. If the search fails, it displays a warning message indicating that the EmployeeID provided is not within the range of IDs in the Employees table.
Next, we need a program in a Standard Module that calls the GetFirstName() function from the frmEmployees form module. This program will also demonstrate how to create multiple instances of a Microsoft Access form, allowing you to open them in memory and access their properties, methods, or control contents independently.
- Open VBA Editing Window (Alt+F11).
Select the Module option from the Insert Menu and add a new Standard Module.
Copy and paste the following VBA Function code into the new Module.
Call GetFirstName() from the Standard Module.
Public Function frmInstanceTest() Dim frm As New Form_frmEmployees '1st Form instance Dim frm2 As New Form_frmEmployees '2nd instance declaration Dim Name1 As String, Name2 As String frm.Visible = True 'make the instance visible in Application Window frm2.Visible = True '2nd instance visible Name1 = frm.GetFirstName(4) 'Call the GetFirstName of Employee ID 4 Name2 = frm2.GetFirstName(5) ''Call the GetFirstName of Employee ID 5 'pause execution of this code to view 'the Employees Form instances in Application Window. Stop MsgBox "Employees " & Name1 & ", " & Name2 End Function
Trial Run of Function frmInstanceTest()
Let us run the code and view the result in the Application Window.
- Click somewhere within the body of the frmInstanceTest() function and press the F5 key to run the code.
The program will pause at the
Stop
statement, allowing you to view the Access application window, where multiple instances of thefrmEmployees
form are open in Normal View, with one instance overlapping the other. Press Alt+F11 to display the Application Window, where both instances of the Form are visible, the second form overlapping the first one.
Click and hold the Title Bar area at the top and drag it to the right to make part of the form behind visible.
Observe the employee records on both forms—they are different: one displays Employee ID 4, and the other displays Employee ID 5. Notice the title bar of both forms; they both show the same form name,
frmEmployees
. Now, return to the program and continue running the code to complete the task.Press Alt+F11 again to switch back to the VBA Window and press the F5 key one more time to continue executing the remaining lines of code.
The Message Box appears in the Application Window, displaying the Employee names Mariya and Steven together. When you click the OK MsgBox Button, the frmEmployee form instances disappear from the Application Window.
Click the OK button on the MsgBox.
Note: Pay special attention to the Stop
statement placed above the MsgBox()
function at the end of the code. The Stop
statement halts VBA execution at that point. While it is typically used during debugging to trace logical errors or make corrections, here it serves a different purpose: it pauses the program so that we can switch to the Access application window and view both open instances of the frmEmployees
form.
If we relied on the MsgBox()
function alone, the code would still pause, but the message box would remain on top of the forms. This would prevent us from dragging the front form aside to view the one behind it. By using the Stop
statement, we gain the flexibility to examine both instances directly in the application window.
If we don't create a pause in the code execution, both instances of the form are closed immediately when the program ends. In that case, we will not be able to view the forms. Since it is a trial run, we would like to know what is happening in the program. It is not necessary to make the Form instances visible before calling the Function GetFirstName ().
The VBA Code Line by Line.
Let’s take a closer look at each line of code in the frmInstanceTest() function. While brief hints are already included within the code, explaining a few points in detail will make them clearer. We’ll begin with the first two Dim statements.
Dim frm As New Form_frmEmployees Dim frm2 As New Form_frmEmployees
In the above Dim statement, you can see that the New keyword is followed by the object reference. The object name is our frmEmployees prefixed by the direct Object Class name FORM, followed by an underscore character separation (Form_) to the frmEmployees Form name (Form_frmEmployees). These Dim statements themselves open two instances of the frmEmployees in memory. Form instances opened in this way are not immediately visible in the Application Window. If we need them to be visible, then make them visible with another statement.
Next, we declared two String Variables: Name1 & Name2 to hold the names returned by the GetFirstName() method.
Next two statements: frm.Visible=True and frm2.Visible=True, makes both instances of the frmEmployees Form visible in the Application Window, for information purposes only.
In the next two lines of code, we are calling the GetFirstName() method of the first and second instances of the frmEmployees to search, find, and return the First Names of employee codes 4 and 5.
Default Instance and Other Instances.
The default instance of a Form is opened in the following manner in programs for accessing their Properties, Methods, and Controls. These styles of statements are always used to open a form in programs. The default instance of the Form will be automatically visible in the Application Window.
Dim frm as Form 'define a Form class object DoCmd.OpenForm "frmEmployees", vbNormal 'open frmEmployees in Memory Set frm3 = Forms!frmEmployees ' attach it to the frm3 object
Assume that we have opened frm & frm2 instances first in memory before the default instance through the above code. How do we address those three instances in a program to do something? Let us forget about the frm, frm2, and frm3 object references, for now, we will go with the straight method, like the one given below:
name3 = Forms![frmEmployees].GetFirstName(5) 'target form in memory is the default instance 'OR name3 = Forms("frmEmployees").GetFirstName(5) 'OR name3 = Forms(2).GetFirstName(5) ' this is the third and default instance
The other two instances in memory cannot be referenced like the first two default methods, using the name of the form. You have to use only the index number of the Forms collection to address the other two instances.
name1 = Forms(0).GetFirstName(3) name2 = Forms(1).GetFirstName(6)
A Shortcut Method.
There is a shortcut method you can use to run the GetFirstName() Method of the frmEmployees Form from the debug window (Ctrl+G). Type the following command in the Debug Window and press Enter Key:
? form_frmEmployees.GetFirstName(5) 'Result: Steven 'OR X = form_frmEmployees.GetFirstName(5)
When we execute the above command, it opens an instance of the frmEmployees form in memory and calls the GetFirstName() function with Employee Code 5 as the parameter. The GetFirstName() function searches for the record, finds it, returns the employee’s first name to the calling program, and then closes the form.
Tip: Even after the form is closed, the current record—Employee ID 5 in this case—remains the active record of the closed form’s last session.
You can verify this behavior by typing the following shortcut command in the Debug Window and pressing Enter:
? Forms!frmEmployees!EmployeeID
? form_frmEmployees![First Name]
'Result: Steven
A Fancy Approach.
In the above command, we didn't run the GetFirstName() method, but the current record's First Name field value is printed. If you want to get a little fancy with the command, then try this by typing it in the debug window and pressing the Enter Key:
MsgBox "First Name: " & form_frmEmployees.GetFirstName(8) 'OR MsgBox "First Name: " & form_frmEmployees![First Name]
Or try the above command from a Command Button Click Event Procedure from another Form's Module, as given below.
Private Sub Command8_Click() MsgBox "First Name: " & Form_frmEmployees.GetFirstName(8) End Sub
No comments:
Post a Comment
Comments subject to moderation before publishing.