Good morning, Dave,
I admit to being a purist, and maybe a bit of a perfectionist. I prefer to
take the initiative rather than to confuse Access into doing something
unexpected. The way I would do this is to have a second form that contains
only an option group. You would give the user a choice between sorting either
by
* Contract or
* End Item (of course the * here is only to depict the checkbox).
Now you are fore-armed with what you need. Say the user selects Contract. In
this form's (the preliminary form) DoCmd.OpenForm statement to open the main
form, you would put the word "Contract" in the OpenArgs position (see the
OpenForm method for details).
In the main form's OnLoad event, you would put code to capture the OpenArgs
argument (OA = Me.OpenArgs, where OA is declared as a GLOBAL variable (type
string) in the form's Declarations section). By the way, notice I don't use
the Forms!FormName!....... form. The keyword Me is a shortcut meaning the
current object, in this case the form.
In the form's OnCurrent event, put a simple If statement:
If OA = "Contract" Then
Me.Contract.RowSource = "SELECT [END ITEM AND CONTRACT].[Contract] FROM
[END ITEM AND CONTRACT] GROUP BY Contract ORDER BY Contract;"
(notice no WHERE clause)
Me.Contract.Enabled = True
Me.End_Item.Enabled = False
Me.Contract.SetFocus
Me.Contract.Requery
Else 'OA = "End_Item"
Me.End_Item.RowSource = "SELECT [END ITEM AND CONTRACT].[End_Item] FROM
[END ITEM AND CONTRACT] GROUP BY End_Item ORDER BY End_Item;"
Me.End_Item.Enabled = True
Me.Contract.Enabled = False
Me.End_Item.SetFocus
Me.End_Item.Requery
End If
(As David Epsom says, set the Row Source property for both comboboxes to
"Table/Query" in their respective property sheets and leave them alone; they
never change.)
Now the user is forced to first pick a contract. In the contract combobox's
AfterUpdate event, put the following code:
If OA = "Contract" Then
Me.End_Item.RowSource = "SELECT [END ITEM AND CONTRACT].[End_Item] FROM
[END ITEM AND CONTRACT] WHERE Contract = '" & Me.Contract & "' GROUP BY
[End_Item] ORDER BY End_Item;"
Me.End_Item.Enabled = True
Me.End_Item.SetFocus
Me.Contract.Enabled = False
Me.End_Item.Requery
Exit Sub
End If
Note the Exit Sub above. This code is in addition to and BEFORE any
processing code you had in the combobox's AfterUpdate event before, as at
this time you're not ready to process - you don't have both Contract and
End_Item yet. After you pick the End_Item is the time to process.
Now the user must choose an End Item, as the contract combobox has been
disabled. When you close the form you don't need to reset anything; the next
time you open the form you'll again encounter the option group first which
will segue into the main form.
In the End_Item combobox's AfterUpdate event, put the following code:
If OA = "End_Item" Then
Me.End_Item.RowSource = "SELECT [END ITEM AND CONTRACT].Contract FROM
[END ITEM AND CONTRACT] WHERE End_Item = '" & Me.End_Item & "' GROUP BY
Contract ORDER BY Contract;"
Me.Contract.Enabled = True
Me.Contract.SetFocus
Me.End_Item.Enabled = False
Me.Contract.Requery
Exit Sub
End If
Again, this code is in addition to and BEFORE any other code you may have in
this event.
Like I hinted before, this is a bit to break off and chew. But if you want to
know exactly what Access is gonna do you need to be as explicit as possible.
For any foreign terms, like option group, etc., you can find entries in the
help file. I suggest you read them thoroughly, especially if they give you
problems initially.
Hope this was helpful,
Sam
Sam thanks for the feedback. Here is the senario. My list boxes can have up
to 100 hundred records in them even with the distinct. ListBox1- Contracts,
ListBox2- End_Items. I have 2 listboxes that basically feed each other. I
can have one contract that can have many end_items (The 2nd list box) or I
can have one End_Item with many contracts(The 1st List box) The user
determines what they want to sort on either Contract, End_Item or None.
Example- If contract is selected the contract listbox (The 1st List box) is
populated with all contracts. When a user selects a proper contract Access
takes the value (ON_CLICK) and puts in the proper information needed to
populate the other list box End_Item (The second list box).
If users selects End_Item it is just works backwards, it will populate
Contracts. I need to make sure that every user has these options I want to
make sure all users start with blank listboxes hence why I was wanted to
blank out the row source type and row source.
If there is a better way I am open to suggestions.
Example for Populating End Items.
Forms!testform!End_Item.RowSourceType = "Table/Query"
Forms!testform!End_Item.RowSource = SELECT [END ITEM AND
CONTRACT].[Contract] FROM [END ITEM AND CONTRACT] WHERE End_Item = '" &
Me.End_Item & "' GROUP BY Contract;"
[quoted text clipped - 53 lines]