Hi, Abdul.
The first key to implementing such a thing is to have control over solid
data entry throughout your database, to avoid problems like having the
following Makes accumulate over time:
Chevrolet
Chevy
Chevvie
If you've done this, and you're tables are normalized, and are using combo
boxes for data entry to limit the available entries, then it makes
implementing what you want easy. If not, you'll have to do some work, so
post details of the database as it exists now.
Assuming you can proceed, how you implement your form depends on how you'll
use it. If you will *always* enter a value in each control on the form, then
your query can include a reference to the form control in its "Criteria" row,
e.g.,
= Forms!MyFilteringForm!Model
as the criteria forthe Model field. And, presumably, you'd want mileage to
be less then your entered value, so its criteria might be:
< Forms!MyFilteringForm!Mileage
If you intend to leave some items blank on occasion, the query will try to
match the Null value, and will only return records that have a Null value in
that field, rather than not filtering the records at all. In this case, you
can build a filter string to use for the query. I like to use a textbox
control on the form, which I leave visible while debugging, and make
invisible once it's working.
The general strategy is to write a custom procedure that gets called in the
AfterUpdate event of each filtering control. It loops through all the
controls on the form, and if it's a textbox or combo box control, builds a
filtering string. I use the Tag property of each control to hold the field
name I want to match. Something like:
Dim ctl As Control
Me!txtFilterString = "" ' Initialize invisible Textbox control
that holds filter
For Each ctl in Me.Controls ' Loop through form's controls collection
If (ctl.ControlType = acComboBox OR ctl.ControlType = acTextBox) Then
If (Nz(ctl.Value) <> 0 And Nz(ctl.Value) <> "") Then
Me!txtFilterString = Me!txtFilterString & _
"[" & ctl.Tag & "]=" & ctl.Value &
" AND "
End If
End If
Next ctl
' Strip off final " AND "
Me!txtFilterString = Left(Me!txtFilterString, Len(Me!txtFilterString) - 5)
Then, use a command button to preview the report, applying the filter if its
not blank:
Dim strDocName As String
Dim strFilter As String
strDocName = "YourReport"
strFilter = ""
' If no criteria selected, preview report with no filtering
If Nz(Me!txtFilterString) = "" Then
DoCmd.OpenReport strDocName, acViewPreview
Else
' Otherwise, apply the filter
strFilter = Me!txtFilterString
DoCmd.OpenReport strDocName, acViewPreview, , strFilter
End If
Hope that helps.
Sprinks
his would mean using combo boxes for