Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Pass parameter to query dynamically from form

Posted on 2014-04-27
7
Medium Priority
?
3,590 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 58
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 48

Expert Comment

by:Dale Fye
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
Independent Software Vendors: 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 58

Accepted Solution

by:
Jim Dettman (Microsoft MVP/ EE MVE) earned 2000 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

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

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

An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Explore the ways to Unlock VBA Project Password Excel 2010 & 2013 documents. Go through the article and perform the steps carefully to remove VBA Excel .xls file.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

610 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