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
Solved

Pass parameter to query dynamically from form

Posted on 2014-04-27
7
3,110 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
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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
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…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

828 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