Hi, BeeJay.
The strategy is to build an SQL string based on the user input, write the
string to an invisible textbox control on the form, and use the string as the
Where clause of an OpenForm or OpenReport action.
If you use a naming conventions where each control has a 3-digit prefix plus
the associated field name, you can conveniently loop through all of the
controls. The following example assumes there are combo boxes on the form,
which the criteria must match exactly, or textboxes, in which case, the
critieria will use the Like "*xxx*" modification. WriteFilterString is
called in the AfterUpdate event procedure of each control.
Once all the criteria has been entered, a command button displays the
filtered recordset.
Private Sub WriteFilterString()
Dim intindex As Integer
Dim ctl As Control
On Error Resume Next
'Reinitialize control
Me!txtFilterString = ""
' Loop through all form controls; if there's data, add to filter string
For Each ctl In Me.Controls
If (Nz(ctl.Value) <> 0 And Nz(ctl.Value) <> "") Then
If ctl.ControlType = acComboBox Then
Me!txtFilterString = Me!txtFilterString & _
"[" & LTrim(Right(ctl.name, Len(ctl.name) -
3)) _
& "]=" & ctl.Value & " AND "
Else
‘ Must be a text box
Me!txtFilterString = Me!txtFilterString & _
"[" & LTrim(Right(ctl.name, Len(ctl.name) -
3)) _
& "] Like " & "'" & "*" & ctl.Value & "*" &
"'" & " AND "
End If
End If
Next ctl
' Strip end of filter
Me!txtFilterString = Left(Me!txtFilterString, Len(Me!txtFilterString) - 5)
End Sub
‘ Command button code to display filtered recordset
Dim stDocName As String
Dim stWhere As String
Dim stFilterLabel As String
stDocName = ""
stWhere = Me![txtFilterString]
DoCmd.OpenForm stDocName, , , stWhere
Hope that helps.
Sprinks
BeeJayEff said:
I have a large database in Access 2003 and want to be able to select a
recordset on a number of different criteria. The main table is called
"Projects", and I want to have a form where I can enter one or more of
"Project Manager", "Status", "Location". If for example I enter a Project
Manager name and a Status, but leave Location blank then I want to retrieve
all records for that P.M. and Status, regardless of Location.
I suspect this is easy. Is it ?