I have a search form that users choose criteria. they can choose a date range (startdate, enddate), priority level, subject, and there is also an option where they can EITHER choose a Region OR a Country - and in the country list box they can hold down the ctrl key and choose one, or two, or however many they want, then on the Search click function it gathers all that criteria and puts it in a select statement that filters the record source. Here's the code:
Private Function BuildFilter() As Variant
Dim varWhere As Variant
Dim varCountry As Variant
Dim varItem As Variant
Dim intIndex As Integer
varWhere = Null
varCountry = Null
Select Case [optRC]
'Option Region
Case Is = 1
Me.cmbCountry.Visible = False
Me.cmbRegion.Visible = True
Me.cmbRegion.SetFocus
' Check for StartDate
If Me.txtStartDate > "" Then
varWhere = varWhere & "[fldLegisEffDate] >= " & "#" & Me.txtStartDate & "#" & " AND "
End If
' Check for EndDate
If Me.txtEndDate > "" Then
varWhere = varWhere & "[fldLegisEffDate] <= " & "#" & Me.txtEndDate & "#" & " AND "
End If
' Check for Priority
If Me.cmbPriority > 0 Then
varWhere = varWhere & "[fldPriorityID] = " & Chr(34) & Me.cmbPriority & Chr(34) & " AND "
End If
' Check for Region
If Me.cmbRegion > 0 Then
varWhere = varWhere & "[fldRegionID] = " & Chr(34) & Me.cmbRegion & Chr(34) & " AND "
End If
' Check for Subject
If Me.cmbSubject > 0 Then
varWhere = varWhere & "[fldSubjectID] = " & Chr(34) & Me.cmbSubject & Chr(34) & " AND "
End If
' Check if there is a filter to return...
If IsNull(varWhere) Then
varWhere = ""
Else
varWhere = "WHERE " & varWhere
' strip off last "AND" in the filter
If Right(varWhere, 5) = " AND " Then
varWhere = Left(varWhere, Len(varWhere) - 5)
End If
End If
BuildFilter = varWhere
'Option Country
Case Is = 2
Me.cmbRegion.Visible = False
Me.cmbCountry.Visible = True
Me.cmbCountry.SetFocus
' Check for StartDate
If Me.txtStartDate > "" Then
varWhere = varWhere & "[fldLegisEffDate] >= " & "#" & Me.txtStartDate & "#" & " AND "
End If
' Check for EndDate
If Me.txtEndDate > "" Then
varWhere = varWhere & "[fldLegisEffDate] <= " & "#" & Me.txtEndDate & "#" & " AND "
End If
' Check for Priority
If Me.cmbPriority > 0 Then
varWhere = varWhere & "[fldPriorityID] = " & Chr(34) & Me.cmbPriority & Chr(34) & " AND "
End If
' Check for Subject
If Me.cmbSubject > 0 Then
varWhere = varWhere & "[fldSubjectID] = " & Chr(34) & Me.cmbSubject & Chr(34) & " AND "
End If
' Check for Country in multiselect list
For Each varItem In Me.cmbCountry.ItemsSelected
varCountry = varCountry & "[fldLegisCountryCode] = " & Chr(34) & Me.cmbCountry.ItemData(varItem) & """ OR "
Next
' Test to see if we have subfilter for Country..
If IsNull(varCountry) Then
' do nothing
Else
' strip off last "OR" in the filter
If Right(varCountry, 4) = " OR " Then
varCountry = Left(varCountry, Len(varCountry) - 4)
End If
' Add some parentheses around the subfilter
varWhere = varWhere & "( " & varCountry & " )"
End If
' Check if there is a filter to return...
If IsNull(varWhere) Then
varWhere = ""
Else
varWhere = "WHERE " & varWhere
' strip off last "AND" in the filter
If Right(varWhere, 5) = " AND " Then
varWhere = Left(varWhere, Len(varWhere) - 5)
End If
End If
BuildFilter = varWhere
Case Else
End Select
End Function
I then filter the forms recordset with the function...
Me.Form.RecordSource = "SELECT * FROM qryLegislation " & BuildFilter
The ultimate goal is to take the result of the criteria, which in the detail datasheet lists the records matching the selection criteria, and make a report out of it. Now I'm way out of practice doing this stuff (taken 7 years off to raise my kids), but I can't figure out how to get the records into a report. My thought was to export the records into a table, then run the query off the result, then clear the table after the report was run, ready for the form to Search again.
I'm really pulling my hair out on this. Am I just forgetting the easy way to do this???
THANKS!!