Solved

Using Input Box to add Parameter to Query

Posted on 2014-10-24
7
634 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 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

 
LVL 119

Expert Comment

by:Rey Obrero
Comment Utility
what version of access are you using?
0
 
LVL 119

Expert Comment

by:Rey Obrero
Comment Utility
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
Comment Utility
This worked great! Thank you!!!
0
 
LVL 10

Expert Comment

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

ab
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now