Solved

Using Input Box to add Parameter to Query

Posted on 2014-10-24
7
670 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
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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

856 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