Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Using Input Box to add Parameter to Query

Posted on 2014-10-24
Medium Priority
Last Modified: 2014-10-24
I have some VBA in Access that runs a query. I want the user to enter a date in an inputbox in order to filter that query.

How do I do that?

I have coded the input box, and that is working. I have Dim'd rptdate as Date and set that equal to what the user puts into the inputbox.

But my SQL is set to just to the name of the query I am using.

Previously, I put the parameter directly into the query in the query builder, but now I want the user to be able to choose.

The Query name is "qry_TO."  I am not sure how to work in what the user enters into the box into the query.

My apologies if this isn't reading clearly. So tired!
Question by:Megin
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
LVL 10

Accepted Solution

Anthony Berenguel earned 2000 total points
ID: 40402860
1. In VBA, create a function that returns the user input.
2. In the query, qry_TO, enter the name of the function in the criteria field for the relevant column.
LVL 19

Expert Comment

by:Eric Sherman
ID: 40402862
Can you post your code or load up a sample db that shows your exact steps?

LVL 19

Expert Comment

by:Eric Sherman
ID: 40402878
Without seeing your db ... The easiest approach would be to update the query sql using VBA as shown below if you already have the date you want stored in the variable rptdate.

Dim qdf As QueryDef
Set qdf = CurrentDb.QueryDefs("qry_TO")
qdf.SQL = "SELECT * FROM YourTableName WHERE YourDateField = #" & rptdate & "#;"

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 40402898
what version of access are you using?
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 40402975
if you are using A2007 or higher, you can use the Tempvars collection

open your query "qry_TO" in design view

in the date field that you are filtering, place this line in the criteria

Field  :  [nameOfDateField]

criteria : TempVars!varDate

now modify the codes for the input box you are using to;

Tempvars.add "varDate", InputBox("Enter Date")

docmd.openquery "qry_TO"

Author Closing Comment

ID: 40403056
This worked great! Thank you!!!
LVL 10

Expert Comment

by:Anthony Berenguel
ID: 40403068
Awesome! I'm glad you're all sorted out :) Enjoy your weekend!


Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question