Combo Box Values Dependent on Criteria (1 Viewer)

xyba

Registered User.
Local time
Today, 14:08
Joined
Jan 28, 2016
Messages
189
This may be a bit ambitious but I'm hoping it's something that can be achieved.

On a form I have two combo boxes. In the first the user can select a report. In the second they can select a Department to filter the report by.

What I would like is for the list of departments in that combo box to be restricted to only those that occur in the report that's selected.

So, for example, the user selects Stock report then selects from the full list of departments. But some of the departments may not have any stock, therefore wouldn't appear on the report. I want to be able to have only those departments that do have stock to appear on the drop down.

At present a user has to either download an unfiltered report and view which departments are listed to go back and download the separate reports or select every department to draw down the report regardless of whether there are any records or not.

Hope this makes sense. Let me know if it doesn't :)
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 06:08
Joined
Aug 30, 2003
Messages
36,131
No problem, post back if you get stuck.
 

xyba

Registered User.
Local time
Today, 14:08
Joined
Jan 28, 2016
Messages
189
I've just taken another look at this and, I'm guessing it is somewhere along the lines of what I'm looking for.

However, I need to check the values in Dept field to each query linked to each report. As there are many I created a TempVar. So, the report would be selected in combo1 and the after update event would create a tempvar of the query to be referenced that holds the unique dept values to be populated in combo2.

My problem is I get a syntax error in the FROM clause when calling the query in the rowsource of combo2.

The code I've used is:

Code:
SELECT Dept FROM [TempVar]![DeptVar] ORDER BY Dept;

Maybe using a TempVar is not the right move? Maybe I've just coded incorrectly?
 

Gasman

Enthusiastic Amateur
Local time
Today, 14:08
Joined
Sep 21, 2011
Messages
14,217
I believe you would need to concatenate the TempVars?

Code:
="SELECT Dept FROM " &  TempVars("DeptVar") & " ORDER BY Dept;"

I have never tried it in a row source in a form though?
Have you tried it via VBA?

Edit: Just tried it and could not get it to work directly in forrm control properties.
 
Last edited:

xyba

Registered User.
Local time
Today, 14:08
Joined
Jan 28, 2016
Messages
189
I believe you would need to concatenate the TempVars?

Code:
="SELECT Dept FROM " &  TempVars("DeptVar") & " ORDER BY Dept;"

I have never tried it in a row source in a form though?
Have you tried it via VBA?

Edit: Just tried it and could not get it to work directly in forrm control properties.

I just tried your code in an expression in the before update event for combo2 but it didn't work. I haven't tried it via vba as I was under the impression it wasn't good practice to use Select queries in vba.
 

Gasman

Enthusiastic Amateur
Local time
Today, 14:08
Joined
Sep 21, 2011
Messages
14,217
I just tried your code in an expression in the before update event for combo2 but it didn't work. I haven't tried it via vba as I was under the impression it wasn't good practice to use Select queries in vba.

It works if you set the rowsource for the combo in code.? I tested both ways.
I'd also be using it in the AfterUpdate of combo1

As for best practice, no idea, but you are just setting in code what you cannot hard code on the form.?:confused:
 

xyba

Registered User.
Local time
Today, 14:08
Joined
Jan 28, 2016
Messages
189
It works if you set the rowsource for the combo in code.? I tested both ways.
I'd also be using it in the AfterUpdate of combo1

As for best practice, no idea, but you are just setting in code what you cannot hard code on the form.?:confused:

What did you set the rowsource to? May be this is why I can't get it to work.

I'm not sure how I could hard code it in the form.
 

Gasman

Enthusiastic Amateur
Local time
Today, 14:08
Joined
Sep 21, 2011
Messages
14,217
Code:
Me.Combo2.Rowsource = ="SELECT Dept FROM " &  TempVars("DeptVar") & " ORDER BY Dept;"
 

xyba

Registered User.
Local time
Today, 14:08
Joined
Jan 28, 2016
Messages
189
Code:
Me.Combo2.Rowsource = ="SELECT Dept FROM " &  TempVars("DeptVar") & " ORDER BY Dept;"

I kept getting errors but, long story short, I had spaces in the references which I've resolved now and your code works a treat.

Thanks for your help :)
 

Users who are viewing this thread

Top Bottom