Note: If a parameter is configured to accept text data, any input is interpreted as text, and no error message is displayed.

To specify the data type for parameters in a query, follow these steps:

  1. With the query open in Design view, on the Design tab, in the Show/Hide group, click Parameters.
  2. In the Query Parameters dialog box, in the Parameter column, type the prompt for each parameter for which you want to specify the data type. Make sure that each parameter matches the prompt that you use in the Criteria row of the query design grid.
  3. In the Data Type column, select the data type for each parameter.

Create a form that collects parameters

Although parameter queries feature a built-in dialog box that collects parameters, they provide only basic functionality. By using a form to collect parameters, you gain the following features:

Create a form that collects parameters for a report

There are several ways you could approach this scenario, but we'll show just one technique using mostly macros. Follow these steps to create a form that collects parameters for a report.

Step 1: Create a form that accepts input

  1. On the Create tab, in the Forms group, click Form Design.
  2. In Design view, press F4 to display the property sheet and then specify the form properties, as shown in the following table.
    Property
    Setting
    Caption
    Enter the name that you want to appear in the title bar of the form.
    Default View
    Single Form
    Allow Form View
    Yes
    Allow Datasheet View
    No
    Allow PivotTable View
    No
    Allow PivotChart View
    No
    Scroll Bars
    Neither
    Record Selectors
    No
    Navigation Buttons
    No
    Border Style
    Dialog
  3. For each parameter that you want the form to collect, click Text Box in the Controls group on the Design tab.
  4. Set the properties for the text boxes, as shown in the following table.
    Property
    Setting
    Name
    Enter a name that describes the parameter, for example, StartDate.
    Format
    Choose a format that reflects the data type of the parameter field. For example, select General Date for a date field.
  5. Save the form and give it a name, such as frmCriteria.

Step 2: Create a code module to check whether the parameter form is already loaded

  1. On the Create tab in the Macros & Code group, click Module.
    A new module opens in the Visual Basic Editor.
  2. Type or paste the following code into the Visual Basic Editor:
  3. Greek font download. Save the module with a unique name, and then close the Visual Basic Editor.

Step 3: Create a macro that controls the form and report

Using the submacro features of Access macros we can define all the needed steps we need to make in a single macro. We'll create four submacros - Open Dialog, Close Dialog, OK, and Cancel - to control the various tasks needed for this procedure. Using the screenshot below as a guide, create a new macro with the following submacros and actions. Note, for this example, our parameter form is called frmCriteria. Adjust your macro to match the name of the form you created earlier. You'll also need to be sure to click Show All Actions on the Design tab in order to view all macro actions.

Save and close the macro. Give the macro a name, for example, Date Range Macro.

Step 4: Add OK and Cancel command buttons to the form

  1. Reopen the parameter form you created earlier in Design view.
  2. Ensure that Use Control Wizards in the Controls group on the Design tab is not selected.
  3. On the Design tab, in the Controls group, click Button.
  4. Position the pointer below the text boxes on your form, and then drag to create an OK command button.
  5. If the property sheet is not visible, press F4 to display it.
  6. Set the OK button's properties, as shown in the following table.
    Property
    Setting
    Name
    OK
    Caption
    OK
    Default
    Yes
    OnClick
    Enter the name of the macro, for example, Date Range Macro.OK.
  7. Create a Cancel command button and set its properties, as shown in the following table.
    Property
    Setting
    Name
    Cancel
    Caption
    Cancel
    OnClick
    Enter the name of the macro, for example, Date Range Macro.Cancel.
  8. Save and close the form.

Step 5: Use the form data as query criteria

  1. Open the query you created earlier in Design view.
  2. Enter the criteria for the data. Use the Forms object, the name of the form, and the name of the control:

Step 6: Add the macro actions to the report events

Microsoft Access Parameter Query Drop Down List

  1. Open the report that you're wanting to use in Design view.
  2. If the property sheet is not visible, press F4 to display it.
  3. Make sure the Record Source property of the report is using the parameter query you defined earlier.
  4. Set these two additional report properties, as shown in the following table.
    Property
    Setting
    OnOpen
    Enter the name of the macro, for example, Date Range Macro.Open Dialog.
    OnClose
    Enter the name of the macro, for example, Date Range Macro.Close Dialog.
    In the Open event of the report, Access will run the actions defined in the Open Dialog submacro of the Date Range Macro object. Similarly, when you close the report, Access will run the actions defined in the Close Dialog submacro of the Date Range Macro object.
  5. Save and close the report.

Step 7: Try it out

Now that you're created all of the Access objects, it's time to try it out. Open your report in Report View or Print Preview and notice that before Access displays the report, your parameter form opens in dialog mode. Enter the criteria needed into the text boxes you created previously and then click the OK command button on the form. Access then hides the form (Visible = No) and opens the report with only data that matches your criteria. This works because the parameter query that the report is based on can read the values in the controls on the hidden form. When you close the report, Access will also close the parameter form.

Active1 year, 10 months ago
I have several queries in an MS Access database. Some of these use parameters. I use the following code in VBA to provide the query with these parameters:
VBA
Access Query
This is working fine. However, I am now trying to use the same code to call a passthrough query to a SQL server. This query uses a different syntax to declare and set the parameters:
SQL Server query
I can't get my VBA code to work with the different SQL syntax. I've read several options but couldn't find anything concrete. Does anyone have experience with this query structure?
In other words: How can I, in VBA, insert parameters in a stored procedure that queries on a SQL server?
Parfait
63.3k10 gold badges59 silver badges77 bronze badges
TheNiersTheNiers

4 Answers

Consider building a named stored procedure that resides in SQL Server and have MS Access call it passing parameters using ADO as opposed to your current DAO method since you require parameterization. Then bind results to a recordset:
SQL Server Stored Proc
VBA
ParfaitParfait
63.3k10 gold badges59 silver badges77 bronze badges
Simply create a pass-though query in Access and save it.
Ensure that the PT query works. It will likely look like:
Exec MySpName '2017-01-01', '2017-05-31'

Microsoft Access Query Prompt

Again: 100% Make sure the query works when you click on it in Access. At this point you not written any VBA code.
Once you have above pass through query working, then in VBA you can do this:
Albert D. KallalAlbert D. Kallal
17.7k1 gold badge24 silver badges38 bronze badges
If I remember right, in a pass-through query, you are passing the query definition directly to the engine in which it is going to run. So, you will have to use the SQL Server syntax for your query instead of the Access VBA syntax. Give that a try.
Also, the same goes for a Stored procedure. Use the syntax like you were to execute through SSMS.
'exec sp_mysp var1 var2' and so on.
RICHARD PietrzakRICHARD Pietrzak
The reply from Albert Kallal was spot on. Thanks Albert. I tried to comment after signing up but.. did not have enough points to comment so.. Hoping this goes through..
The only thing I changed was..
I replaced the Set rst = .OpenRecordset with..CurrentDb.QueryDefs('q_PTO_SubmitNewRequest').Execute
Thanks again for posting this. It really was a HUGE help. I had many complex .adp projects years ago and am working with a client needing similar functionality. It looks like I can mirror the .adp functionality using the pass through queries. Very Cool :)
Microsoft Access Query Parameters
With CurrentDb.QueryDefs('q_PTO_SubmitNewRequest')
End With
Arun Vinoth
12.3k13 gold badges30 silver badges85 bronze badges
BobBob

Not the answer you're looking for? Browse other questions tagged sql-servervbams-accessparameterspass-through or ask your own question.