Use the form name as a parameter in a query | Newbie | | Join Date: Jun 2009 Location: Buenos Aires, Argentina
Posts: 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
|  | Expert | | Join Date: Apr 2006 Location: Philadelphia
Posts: 5,214
| | | re: Use the form name as a parameter in a query Quote:
Originally Posted by lennih 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 - Do not set any Parameters in the Query itself, the SQL will be dynamically assigned, as in:
- SELECT DISTINCTROW [Order Details].OrderID, [Order Details].*
-
FROM [Order Details];
- 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.
- Public Function fOpenQueryWithFormArgument(strFormName As String)
-
Dim strSQL As String
-
-
strSQL = "SELECT DISTINCTROW [Order Details].OrderID, [Order Details].* " & _
-
"FROM [Order Details] WHERE [Order Details].OrderID = " & Forms(strFormName)![OrderID] & ";"
-
-
CurrentDb.QueryDefs("qryOrders").SQL = strSQL
-
End Function
- Call the Function, passing the appropriate Form Name, then Open the Query.
- Call fOpenQueryWithFormArgument("Orders")
-
-
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)!
|  | Administrator | | Join Date: Oct 2006 Location: London - UK
Posts: 15,703
| | | 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
| | | 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.
|  | Expert | | Join Date: Apr 2006 Location: Philadelphia
Posts: 5,214
| | | re: Use the form name as a parameter in a query Quote:
Originally Posted by lennih 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
| | | 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.
|  | Similar Microsoft Access / VBA bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,327 network members.
|