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
5 3880 @lennih - 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)!
NeoPa 32,554
Expert Mod 16PB
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!
Thanks a lot to both of you!!!
ADezii, you're an excellent teacher.
Thanks for your time.
@lennih
You are very welcome. All of us here are more than happy to assist you in any way we can, lennih.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Mike Cocker |
last post by:
Hello,
I'm quite weak at PHP, so I was hoping to get some help understanding the
below code. First off, I'm trying to create a "query form" that will allow
me to display the results on my...
|
by: rickcheney |
last post by:
I just changed my Access 2002 database to a SQL Server ADP project. I
had a form where the user entered a value into a text box and when a
command button on the form was clicked a Report was...
|
by: sheree |
last post by:
I would like to create a query where one of the columns of the queries
comes from a combo list box on a form.
For example, if my table has the following fields:
id
name
interest1
interest2...
|
by: DFS |
last post by:
I've been working around this for years (I believe), so I figured someone
here might know:
Why won't a crosstab query accept a value from a form reference?
TRANSFORM...
|
by: hfk0 |
last post by:
Hi,
I'm new to ASP.net, SQL Server and visual studio.net, and I'm having
problem inserting and storing data from a web form to a SQL database.
I created a simple ASP.NET web form, a simple SQL...
|
by: fstenoughsnoopy |
last post by:
I have a customer order database and I need to pull a customers
information, ie first name, last name, address, city, state, zip,
phone, etc, into the oder table. i don't know how to go about...
|
by: eholz1 |
last post by:
Hello PHP,
I am having a problem. I know the area of the problem, but not how to
solve it.
It has to do with a php page with a form on it, and I am trying to
perform an insert query into
my...
|
by: hoofbeats95 |
last post by:
I don't think this should be this complicated, but I can't figure it
out. I've worked with C# for several years now, but in a web
environment, not with windows form. I have a form with a query...
|
by: Dave |
last post by:
On my form I have combo boxes. These combo boxes, after updating
them, populate respective listboxes that are located below the combo
boxes on the same form. I am trying to use a "generate...
|
by: scubak1w1 |
last post by:
Hello,
I posted this in the HTML forum, but it was suggested I post it over here as well by a moderator.
I have a form that will not 'fire' in non-IE browsers?? I have poked and poked at the code...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM).
In this month's session, the creator of the excellent VBE...
|
by: MeoLessi9 |
last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: Aftab Ahmad |
last post by:
Hello Experts!
I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
| |