Others

How to Use Access Form Dates in a Pass-Through Query

Aamir Naveed AkramAamir Naveed Akram Nov 3, 2026 868 views

Question details

The user needs to replace hard-coded dates in an Access pass-through query with dynamic start and end dates selected from form controls.

How to Use Access Form Dates in a Pass-Through Query
Product
Microsoft Access
Device & OS
not provided
Scenario
Building a dynamic pass-through query based on user input from an Access form.
Observed behavior
The query currently uses hard-coded dates, and the goal is to dynamically concatenate form values into the query's SQL property using VBA and DAO before execution.
Before you start

Ensure you have a working pass-through query with hard-coded dates to use as a template, and back up your Access database before adding or modifying VBA code.

Solution 1Recommended

Update Pass-Through Query SQL Using VBA and Form Controls

Use a VBA script to capture the start and end dates from your form controls, concatenate them into a SQL string, and update the pass-through query's SQL property via DAO.

Because pass-through queries are executed directly on the backend server (like SQL Server), they cannot interpret Microsoft Access form references (e.g., Forms!MyForm!txtStartDate). To resolve this, you must construct the SQL statement in VBA, replacing the dynamic parameters with the actual selected values, and then overwrite the saved query's SQL property.

1
Create Form Controls

Open your Access form in Design View. Add two text boxes for the dates (name them 'txtStartDate' and 'txtEndDate') and a command button (name it 'btnRunQuery') to trigger the code.

2
Open the VBA Editor

Right-click the command button, select 'Build Event...', choose 'Code Builder', and click OK. This opens the VBA editor within the button's Click event.

3
Format the Selected Dates

In VBA, declare variables and use the Format() function to ensure the dates match your backend database requirements (e.g., strStart = Format(Me.txtStartDate, "yyyy-mm-dd")).

4
Build the SQL Statement

Create a string variable that holds your complete SQL syntax. Concatenate the formatted date variables into the string where the hard-coded dates used to be. For example: strSQL = "SELECT * FROM Sales WHERE SaleDate BETWEEN '" & strStart & "' AND '" & strEnd & "'".

5
Update the Query Definition

Use DAO to assign the newly constructed SQL string to your existing pass-through query. Write: CurrentDb.QueryDefs("YourPassThroughQueryName").SQL = strSQL.

6
Execute the Query

Add code to execute the query or open a report based on the newly updated query definition using DoCmd.OpenQuery "YourPassThroughQueryName".

Update Pass-Through Query SQL Using VBA and Form Controls
Important: Make sure your VBA project has the 'Microsoft Office 16.0 Access database engine Object Library' (or DAO equivalent) reference enabled under Tools > References in the VBA editor.
Free Microsoft Office alternative

Looking for a free alternative to Microsoft Office?

While Microsoft Access handles specialized database management, if you need a free, lightweight, and highly compatible suite for your documents, spreadsheets, and presentations, WPS Office is an excellent choice. It offers a familiar interface and seamless migration for your daily office tasks without the hefty subscription fees.

  1. 1. Download the Installer: Visit the official WPS Office website and download the free installation package.
  2. 2. Install WPS Office: Run the setup file and follow the on-screen instructions for a quick, lightweight installation.
  3. 3. Open Your Office Files: Launch Writer, Spreadsheets, or Presentation to seamlessly open, edit, and save your existing Office documents.
Free and lightweight office suite for daily productivityFully compatible with Microsoft Word, Excel, and PowerPoint formatsFamiliar user interface makes switching easy with zero learning curveBuilt-in PDF editing and format conversion capabilities
microsoft office alternative - wps office

Frequently Asked Questions

Why do I need to use VBA to change dates in an Access pass-through query?

Unlike standard local Access queries, pass-through queries send SQL commands directly to the backend database server (like SQL Server or Oracle). The backend server cannot read Access form references like [Forms]![MyForm]![txtDate], so you must use VBA to inject the literal date values into the SQL text string before executing it.

How should I format the date string in my VBA code for a pass-through query?

The date format depends entirely on the requirements of your backend SQL database. For Microsoft SQL Server, it is usually safest to use the ISO format (YYYYMMDD or YYYY-MM-DD). You can enforce this in VBA using the Format function, such as Format(Me.txtStartDate, "yyyy-mm-dd").

Can I pass parameters directly to a pass-through query without modifying the SQL property?

No, traditional pass-through queries in Access do not support parameter binding natively. The most reliable and widely used method is using DAO in VBA to completely rewrite the QueryDef.SQL property with the concatenated dynamic values prior to running the query.