How to Use a Variable to Requery a Subform in Access VBA
Question details
The user wants to dynamically requery a subform in Microsoft Access by passing its name as a variable to a VBA procedure.

- 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.
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.
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.
Open your Microsoft Access database and press ALT + F11 to launch the Visual Basic for Applications (VBA) editor.
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)`.
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`.
Whenever a requery is needed, call the procedure by passing the subform control name as a string. For example: `RequerySub "frmAssignedEquipment"`.

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. Download WPS Office: Visit the official WPS Office website to download the free installation package for your operating system.
- 2. Install the Suite: Run the installer and follow the on-screen instructions to set up WPS Office in minutes.
- 3. Open Your Office Files: Launch WPS Office and directly open any existing .docx, .xlsx, or .pptx files with full formatting retention.

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`.




