Basically, I would like to design a query that will Group By multiple fields depending on the fields a user selects in 2 Combo Boxes. I would like to try to avoid doing this in VBA code, if at all possible.
So I have a form that has 2 combo boxes. The user can select a field from one combo box (i.e. Name, Account#, Source, Location, PaymentType, etc.), and another field from an identical combo box.
I would like two levels of grouping in my query based on what the user selects, but it just isn't feasible to have hundreds of queries for every possible combination of groupings.
I was hoping to have 2 "FieldName" tables that would be populated by the combo box. Then the query, based on what the user selects from those combo boxes, will group by those 2 "fields".
My FieldName Table1 looks like this:
Field: TransactionFieldName
Row1: [TransactionTable].[Name]
Instead of "[TransactionTable].[Name]", the [TransactionFieldName] field could also have the values
[TransactionTable].[Owner]
[TransactionTable].[Company]
[TransactionTable].[Facility]
[TransactionTable].[Location]
- depending on what the user selects in the Combo Box.
I originally tried doing nested IIF statements in my query, like
SelectedGroup1: IIF([Forms]![Viewer]![Group1]="No Grouping", "No Grouping", IIF([Forms]![Viewer]![Group1]="Name", [TransactionTable].[Name], IIF([Forms]![Viewer]![Group1]="Facility", [TransactionTable].[Facility], IIF([Forms]![Viewer]![Group1]="Location", [TransactionTable].[Location], IIF([Forms]![Viewer]![Group1]="Company", [TransactionTable].[Company], IIF([Forms]![Viewer]![Group1]="Month", [TransactionTable].[Month].......
and so on and so on...
However, I get an error message saying that my expression is too long. Even if I shorten it to only have a limited number of fields, the query is still so much slower than if I were grouping by the same field every time, - and I was testing this against a new database.
Instead of usine the nested IIF statements, I tried this calculated field:
SelectedGroup1: [FieldName].[TransactionFieldName]
which I hoped would equal "[TransactionTable].[Name]" and therefore reference that table in the query, but my results just looked like this:
[TransactionTable].[Name]
[TransactionTable].[Name]
[TransactionTable].[Name]
[TransactionTable].[Name]
[TransactionTable].[Name]
..
When I wanted them to say:
Bob Parker
Mary Williams
John Smith
Randy Jones
Lana Maxwell
In other words, the "Name" field from the Transaction Table.
Is there an easy way to do this, and if not, what's the hard way?
So I have a form that has 2 combo boxes. The user can select a field from one combo box (i.e. Name, Account#, Source, Location, PaymentType, etc.), and another field from an identical combo box.
I would like two levels of grouping in my query based on what the user selects, but it just isn't feasible to have hundreds of queries for every possible combination of groupings.
I was hoping to have 2 "FieldName" tables that would be populated by the combo box. Then the query, based on what the user selects from those combo boxes, will group by those 2 "fields".
My FieldName Table1 looks like this:
Field: TransactionFieldName
Row1: [TransactionTable].[Name]
Instead of "[TransactionTable].[Name]", the [TransactionFieldName] field could also have the values
[TransactionTable].[Owner]
[TransactionTable].[Company]
[TransactionTable].[Facility]
[TransactionTable].[Location]
- depending on what the user selects in the Combo Box.
I originally tried doing nested IIF statements in my query, like
SelectedGroup1: IIF([Forms]![Viewer]![Group1]="No Grouping", "No Grouping", IIF([Forms]![Viewer]![Group1]="Name", [TransactionTable].[Name], IIF([Forms]![Viewer]![Group1]="Facility", [TransactionTable].[Facility], IIF([Forms]![Viewer]![Group1]="Location", [TransactionTable].[Location], IIF([Forms]![Viewer]![Group1]="Company", [TransactionTable].[Company], IIF([Forms]![Viewer]![Group1]="Month", [TransactionTable].[Month].......
and so on and so on...
However, I get an error message saying that my expression is too long. Even if I shorten it to only have a limited number of fields, the query is still so much slower than if I were grouping by the same field every time, - and I was testing this against a new database.
Instead of usine the nested IIF statements, I tried this calculated field:
SelectedGroup1: [FieldName].[TransactionFieldName]
which I hoped would equal "[TransactionTable].[Name]" and therefore reference that table in the query, but my results just looked like this:
[TransactionTable].[Name]
[TransactionTable].[Name]
[TransactionTable].[Name]
[TransactionTable].[Name]
[TransactionTable].[Name]
..
When I wanted them to say:
Bob Parker
Mary Williams
John Smith
Randy Jones
Lana Maxwell
In other words, the "Name" field from the Transaction Table.
Is there an easy way to do this, and if not, what's the hard way?