Solved

Using Input Box to add Parameter to Query

Posted on 2014-10-24
7
655 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

776 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