Solved Run Query Based on Parameters (1 Viewer)

Pop_Access

Member
Local time
Today, 06:51
Joined
Aug 19, 2019
Messages
66
Hi,

I want to enter a parameters (ex. date range), then to click on a command "Execute Query" (All of them are available on form header) to run a query on a form detail.
kindly see attached Pic.

Thank you
 

Attachments

  • Access.png
    Access.png
    17.1 KB · Views: 96
Last edited:

jdraw

Super Moderator
Staff member
Local time
Today, 09:51
Joined
Jan 23, 2006
Messages
15,379
Perhaps you can provide more detail and some sample data to ensure we're talking about the same thing.
Seems you have a form with some controls. You enter some values into the controls and click a button. Behind the scene the control values are part of query criteria that requests some info from your underlying table(s); and the result of the query is placed on your form or a subform on that form.
These may be helpful.
1:
2:
or
3: this on Access/vba by Steve Bishop

I used this google query to find some examples:


Good luck with your project.
 
Last edited:

Gasman

Enthusiastic Amateur
Local time
Today, 14:51
Joined
Sep 21, 2011
Messages
14,269
Thank you @jdraw, I knows how to set a parameter in query, but I want to run the query ( after entering the parameters) and to show the results in the form detail
So create a form with controls for the parameters and a button to set the recordsource for the form?
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 09:51
Joined
Feb 19, 2002
Messages
43,264
Make the query bound to the form reference the unbound form fields with the criteria:

Select ... From ...
Where (fld1 = Forms!myform!cbofld1 OR Forms!myform!cbofld1 Is Null) AND (fld2 = Forms!myform!cbofld2 OR Forms!myform!cbofld2 Is Null)

The form will open up empty. The user chooses something from either or both unbound combos and presses the "run" button. The code behind the "run" button is simply:

Me.Requery

Pay attention to the parentheses in the example if you want to use multiple criteria fields and you want them to be optional. The parentheses create two conditions that will be and'd
 

Pop_Access

Member
Local time
Today, 06:51
Joined
Aug 19, 2019
Messages
66
Make the query bound to the form reference the unbound form fields with the criteria:

Select ... From ...
Where (fld1 = Forms!myform!cbofld1 OR Forms!myform!cbofld1 Is Null) AND (fld2 = Forms!myform!cbofld2 OR Forms!myform!cbofld2 Is Null)

The form will open up empty. The user chooses something from either or both unbound combos and presses the "run" button. The code behind the "run" button is simply:

Me.Requery

Pay attention to the parentheses in the example if you want to use multiple criteria fields and you want them to be optional. The parentheses create two conditions that will be and'd
Thank you @Pat Hartman;
I have tried your solution, but the query results will not be displayed on the form detail; I attached a picture from the database, it may help to explain my post.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 09:51
Joined
Feb 19, 2002
Messages
43,264
It doesn't matter what you are going to do with the query results, the method is still the same.
 

Pop_Access

Member
Local time
Today, 06:51
Joined
Aug 19, 2019
Messages
66
It doesn't matter what you are going to do with the query results, the method is still the same.
It's very important to display the query result on "Form Detail", because I need to select some of the students (from the query result) to send an Email for them
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 09:51
Joined
Feb 19, 2002
Messages
43,264
I feel like we're talking in circles. WHATEVER form you want to display the data on should use the query I described.
 

Pop_Access

Member
Local time
Today, 06:51
Joined
Aug 19, 2019
Messages
66
I feel like we're talking in circles. WHATEVER form you want to display the data on should use the query I described.
Thank you @Pat Hartman, you've described the perfect solution.
Since I'm not a programmer 🥲, I thought that if I connect the parameter inside query in "Criteria" OR by writing VBA code inside the form, I will got the same results.
 

Users who are viewing this thread

Top Bottom