By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,307 Members | 1,929 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,307 IT Pros & Developers. It's quick & easy.

Use the form name as a parameter in a query

P: 3
I've got 7 forms with different names, but some of the controls in them are the same. In some of these controls (particularly in the combo boxes) I am filtering the row source with a query. In the query, the criteria [Forms]![FORMX]![CONTROLX] filters the records I'm interested in.
It works perfectly. The problem is that I have to create 7 different queries and just change the name of the form, because everything else (even the name of the control) doesn't change.

Is there a way in which I can send the form name as a parameter to the query to filter the table?
I imagine there is, but how should I fill the criteria field?
[Forms]![PARAMETERX]![CONTROLX] wouldn't work, I suppose.

Thanks a lot.
Lenni
Jun 15 '09 #1
Share this Question
Share on Google+
5 Replies


ADezii
Expert 5K+
P: 8,679
@lennih
  1. Do not set any Parameters in the Query itself, the SQL will be dynamically assigned, as in:
    Expand|Select|Wrap|Line Numbers
    1. SELECT DISTINCTROW [Order Details].OrderID, [Order Details].*
    2. FROM [Order Details];
  2. Create a Public Function in a Standard Code Module. This Function will accept 1 Argument, the Name of a Form. The underlying SQL of the Query will be modified to point to a Control on the passed Form Name.
    Expand|Select|Wrap|Line Numbers
    1. Public Function fOpenQueryWithFormArgument(strFormName As String)
    2. Dim strSQL As String
    3.  
    4. strSQL = "SELECT DISTINCTROW [Order Details].OrderID, [Order Details].* " & _
    5.          "FROM [Order Details] WHERE [Order Details].OrderID = " & Forms(strFormName)![OrderID] & ";"
    6.  
    7.          CurrentDb.QueryDefs("qryOrders").SQL = strSQL
    8. End Function
  3. Call the Function, passing the appropriate Form Name, then Open the Query.
    Expand|Select|Wrap|Line Numbers
    1. Call fOpenQueryWithFormArgument("Orders")
    2.  
    3. DoCmd.OpenQuery "qryOrders", acViewNormal, acReadOnly
P.S. - In the above illustration, the Form Name 'Orders' is passed to the fOpenQueryWithFormArgument() Function. The SQL of 'qryOrders' is now modified to set the Criteria of [OrderID] to the [OrderID] Control on the Orders Form, namely: Forms("Orders")![OrderID]. Any questions, feel free to ask. Either myself or someone else will be more than happy to help you.

More P.S. - You could also pass Me.Name, if the Control on the 'Active Form' will 'always' be used as the Criteria. I'm done PSssssing! (LOL)!
Jun 16 '09 #2

NeoPa
Expert Mod 15k+
P: 31,707
In your situation I would consider some code in your Open event procedure that sets up a filter. Alternately, this same event procedure could replace the existing (defined) QueryDef in .RecordSource with SQL grabbed from the QueryDef but doctored to reflect the references to the current form.

Welcome to Bytes!
Jun 17 '09 #3

P: 3
Thanks a lot to both of you!!!
ADezii, you're an excellent teacher.
Thanks for your time.
Jun 17 '09 #4

ADezii
Expert 5K+
P: 8,679
@lennih
You are very welcome. All of us here are more than happy to assist you in any way we can, lennih.
Jun 17 '09 #5

P: 3
ADezii, I did exactly as you said, and everything went fine. But I've just realised that I can't manage to get the filter to update once the form has loaded.
To be more precise, I've got a form with two combo boxes - the first one to choose a category and the second one to choose a subcategory depending on the category previously selected.
When the form loads, an "onActivate" event calls the function I've written following your steps, so that the SQL of the query is modify to filter the subcategories, according to the category selected on that particular record. In this form I'm not suppose to be able to navigate through the records, but I am able to change the category, so I need the Row Source of the subcategory to update.
I tried calling the same function AfterUpdate on the category combo box, so that the new category value is passed to the query and the list of subcategories should change accordingly. This last thing doesn't happen.
Me.Requery, Me.Refresh or Sendkeys{F5} won't do the trick either.
Nor will Me.SUBCATEGORY.Requery
Once the form is loaded and the original value in the CATEGORY field is passed to the query, I cannot update/refresh the filter without reloading the form.
Any help will be greatly appreciated.
Jun 21 '09 #6

Post your reply

Sign in to post your reply or Sign up for a free account.