473,289 Members | 1,945 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,289 software developers and data experts.

Use the form name as a parameter in a query

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
5 3880
ADezii
8,834 Expert 8TB
@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
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!
Jun 17 '09 #3
lennih
3
Thanks a lot to both of you!!!
ADezii, you're an excellent teacher.
Thanks for your time.
Jun 17 '09 #4
ADezii
8,834 Expert 8TB
@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
lennih
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

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

Similar topics

3
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...
7
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...
6
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...
3
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...
5
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...
2
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...
5
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...
8
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...
6
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...
3
scubak1w1
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...
2
isladogs
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...
0
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"....
0
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...
0
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...
0
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...
0
isladogs
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...
1
isladogs
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...
0
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...
1
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)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.