Connecting Tech Pros Worldwide Help | Site Map

Use the form name as a parameter in a query

Newbie
 
Join Date: Jun 2009
Location: Buenos Aires, Argentina
Posts: 3
#1: Jun 15 '09
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
ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,214
#2: Jun 16 '09

re: Use the form name as a parameter in a query


Quote:

Originally Posted by lennih View Post

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

  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)!
NeoPa's Avatar
Administrator
 
Join Date: Oct 2006
Location: London - UK
Posts: 15,703
#3: Jun 17 '09

re: Use the form name as a parameter in a query


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!
Newbie
 
Join Date: Jun 2009
Location: Buenos Aires, Argentina
Posts: 3
#4: Jun 17 '09

re: Use the form name as a parameter in a query


Thanks a lot to both of you!!!
ADezii, you're an excellent teacher.
Thanks for your time.
ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,214
#5: Jun 18 '09

re: Use the form name as a parameter in a query


Quote:

Originally Posted by lennih View Post

Thanks a lot to both of you!!!
ADezii, you're an excellent teacher.
Thanks for your time.

You are very welcome. All of us here are more than happy to assist you in any way we can, lennih.
Newbie
 
Join Date: Jun 2009
Location: Buenos Aires, Argentina
Posts: 3
#6: Jun 21 '09

re: Use the form name as a parameter in a query


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.
Reply