Hi, Justin.
I omitted a couple of important points in explaining my code. Once you
understand what's going on, I think you'll find this approach much more
straightforward than the series of command buttons you're thinking of--the
user simply fills in controls on a form.
- Firstly, "txtFilterString" is the name of a textbox on the form in which
the filter string will be built, i.e., the value of the SQL string will be
assigned to the textbox.
- I use a naming convention for each control that consists of a 3-digit
prefix (txt for textbox, cbo for combo box, etc.) plus the name of the table
field that corresponds to this data. All controls on the form are *unbound*.
- Using this naming convention, for each control, if it has a non-null
value, a snippet of filter string is written: first an open bracket, then
the name of the field (snipping off the first 3 characters of the control
name), then a close bracket, an equal sign, and finally the value the user
has selected. So, for example, if the user entered a value of 4 in a combo
box called cboCategory, the code generated would be:
[Category] = 4 AND
The For loop loops through all controls in the form's controls collection,
adding similar snippets for each non-null value entered, resulting in, say:
[Category] = 4 AND [Item] = 623 AND [Region] = 2 AND
Then after the loop the " AND " is snipped off, resulting in the final string:
[Category] = 4 AND [Item] = 623 AND [Region] = 2
So, in your case, three combo boxes--cboCategory, cboSupplier, and
cboItem--would enable the user to select one or more of these values in any
combination.
To display the filtered records on a form, use the Openform method, using
the value of Me!txtFilterString as the link critieria:
Private Sub YourCommandButton_Click()
On Error GoTo Err_YourCommandButton_Click
Dim stDocName As String
Dim stLinkCriteria As String
stDocName = "YourForm"
stLinkCriteria = Me!txtFilterString
DoCmd.OpenForm stDocName, , , stLinkCriteria
Exit_YourCommandButton_Click:
Exit Sub
ErrYourCommandButton_Click:
MsgBox Err.Description
Resume Exit_YourCommandButton_Click
End Sub
Hope that helps.
Sprinks