logo
search
Others

How to Use a Variable to Requery a Subform in Access VBA

Huma Ashraf ChHuma Ashraf Ch Oct 22, 2026 868 views

Question details

The user wants to dynamically requery a subform in Microsoft Access by passing its name as a variable to a VBA procedure.

How to Use a Variable to Requery a Subform in Access VBA
Product
Microsoft Access
Device & OS
not provided
Scenario
Developing an Access database where multiple subforms need to be refreshed dynamically without hardcoding their control names in the VBA script.
Observed behavior
A text string variable cannot be executed directly as VBA code, requiring a method to reference the subform control using the string variable to trigger the requery action.
Before you start

Ensure you have a basic understanding of Microsoft Access form architecture, specifically the difference between a subform control name and the form object it contains.

Solution 1Recommended

Pass the Variable using the Form's Controls Collection

Use the Controls collection of the parent form to reference the subform dynamically using a string variable instead of attempting to execute the string directly as a command.

In Access VBA, a text variable cannot be executed directly as code. To target a specific subform dynamically, you must pass the subform's control name to a procedure and utilize the parent form's Controls collection to locate and requery it.

1
Open the VBA Editor

Open your Microsoft Access database and press ALT + F11 to launch the Visual Basic for Applications (VBA) editor.

2
Create the Subroutine

Insert a new module or navigate to your main form's code module. Define a subroutine that accepts a string parameter: `Sub RequerySub(SubFormName As String)`.

3
Add the Controls Collection Logic

Inside the subroutine, reference the subform using the Controls collection of the main form. For example, if your main form is 'frmLoggedIn', write: `Forms!frmLoggedIn.Controls(SubFormName).Form.Requery`.

4
Call the Procedure

Whenever a requery is needed, call the procedure by passing the subform control name as a string. For example: `RequerySub "frmAssignedEquipment"`.

Pass the Variable using the Form's Controls Collection
Subform Control Name Requirement: Ensure the string you pass is exactly the 'Name' property of the subform control container on the main form, which may differ from the actual source form object's name.
Free Microsoft Office alternative

Try WPS Office for Your Document and Spreadsheet Needs

While Microsoft Access is designed for database management, WPS Office is an exceptional, free, and lightweight alternative for all your document, spreadsheet, and presentation needs, offering seamless compatibility with MS Office formats.

  1. 1. Download WPS Office: Visit the official WPS Office website to download the free installation package for your operating system.
  2. 2. Install the Suite: Run the installer and follow the on-screen instructions to set up WPS Office in minutes.
  3. 3. Open Your Office Files: Launch WPS Office and directly open any existing .docx, .xlsx, or .pptx files with full formatting retention.
Free and lightweight complete office suiteHighly compatible with Microsoft Word, Excel, and PowerPoint filesFamiliar user interface for a zero-learning-curve transitionBuilt-in PDF editor and tabbed window management
QA img-9

Frequently Asked Questions

Why do I get an error when I try to run a string variable as a VBA command?

VBA does not natively evaluate strings as executable code commands like some dynamic scripting languages do. You must use object collections, such as the Controls collection, to reference objects by their string names.

Can I use this method to requery subforms on different main forms?

Yes, but you will need to specify the main form dynamically as well. You can pass the main form name as a second string variable and use `Forms(MainFormName).Controls(SubFormName).Form.Requery`, or use the `Me` keyword if the code resides in the active form's module.

What is the difference between a Subform Control Name and a Source Object Name?

The Subform Control Name is the name of the physical container control placed on your main form. The Source Object is the name of the actual Access form displayed inside that container. The Controls collection requires the Subform Control Name.

How do I requery a specific combobox inside a subform using a variable?

You can extend the object hierarchy in your VBA code to target the specific control inside the subform like this: `Forms!MainForm.Controls(SubFormName).Form.Controls("ComboBoxName").Requery`.