Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Using Input Box to add Parameter to Query

Posted on 2014-10-24
7
Medium Priority
?
800 Views
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!
0
Comment
Question by:Megin
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 10

Accepted Solution

by:
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.
0
 
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?

ET
0
 
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 & "#;"
qdf.Close



ET
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 40402898
what version of access are you using?
0
 
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"
0
 

Author Closing Comment

by:Megin
ID: 40403056
This worked great! Thank you!!!
0
 
LVL 10

Expert Comment

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

ab
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

773 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