Solved

Pass parameter to query dynamically from form

Posted on 2014-04-27
7
3,176 Views
Last Modified: 2014-04-28
Dear experts -
I have a simple request, but am getting confused.
I want to open a table in datasheet view, setting filter criteria based on a field from the form (e.g., if txtDate is not empty, filter records for that date).
I also want to use default sort criteria (multiple fields).

Opentable doesn't seem to do the job.
Openquery I'm wondering about, since I can't see how I can pass a criteria to it...

Thanks for any help -
0
Comment
Question by:terpsichore
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 57
ID: 40025909
Simplest thing to do is create a copy of the query to be used just with this form, then on the criteria line, use a reference to the form and control. i.e.

=Forms![<formname>]![<controlName>]

So if you had frmCustomer and a control called txtCustID, it would be:

=Forms![frmCustomer]![txtCustID]

Jim.
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 40026026
Or you can use the main forms Load event, and the textboxes Afterupdate event to change the Filter property of the subform.  Something like:

Private Sub txt_Filter_AfterUpdate

    me.subForm_ControlName.Form.Filter = me.txt_Filter
    me.subform_ControlName.Form.FilterOn = (Len(me.txt_Filter & "") > 0)

End Sub

and in the main forms Load event:

Private Sub Form_Load

    Call txt_Filter_AfterUpdate

End Sub

And to add an order by clause:

me.subform_ControlName.OrderBy = "[field1], [field2] Desc, [Field3]"
me.subform_ControlName.OrderByOn = true

you could put that in the main forms Form_Load event.  If you want to give your users flexibility in using multiple columns to sort the datasheet, you might want to take a look at the article I wrote several months ago on sorting forms and lists.
0
 

Author Comment

by:terpsichore
ID: 40026077
actually, I am not using this query for anything on the form - it is only being used as a popup, from which the user can browse or copy/paste records.
Does that refine the way I would approach this?
Thanks!
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 31

Expert Comment

by:Helen_Feddema
ID: 40026195
Perhaps this could be done in a subform on the main form.  Depending on where the data for the query comes from, you might be able to just link the main form to the subform in the LinkMasterFields and LinkChildFields properties of the subform control, so that the subform displays records linked to the key value in the main form.

Here is a screen shot of such a form:

Main form and subform
0
 

Author Comment

by:terpsichore
ID: 40026665
sorry, maybe i'm still not being clear - the query I am running is independent of anything being used on the form EXCEPT 1 or 2 controls that I want to use as input variables in a standalone query - the user will push a button and invoke the query - if those fields are populated, it will set filter conditions based on that, and then open a query in datasheet mode that the user can browse or copy/paste.
THANKS
0
 
LVL 57

Accepted Solution

by:
Jim Dettman (Microsoft MVP/ EE MVE) earned 500 total points
ID: 40026915
In the standalone query, refer to the form controls as I showed in my first comment.

As long as the form is open, the query will be able to get the value of the controls, and filter the records based on that.

If you don't want to see the form when the datasheet pops up, you can hide it by setting it's visible property to false.   It just needs to be open, but doesn't matter if it's visible or not.

Jim.
0
 

Author Closing Comment

by:terpsichore
ID: 40026922
thank you - this works for me
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

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.
Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
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…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

697 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