PHV,
My apologies, I don't quite understand the question, but below is the code associated with the Search button.
Private Sub cmdSearch_Click()
Dim varWhere As Variant, varDateSearch As Variant
Dim rst As DAO.Recordset
' Initialize to Null
varWhere = Null
varDateSearch = Null
' First, validate the dates
' If there's something in Contact Date From
If Not IsNothing(Me.txtTranDateStart) Then
' First, make sure it's a valid date
If Not IsDate(Me.txtTranDateStart) Then
' Nope, warn them and bail
MsgBox "The value in Contact From is not a valid date.", vbCritical, gstrAppTitle
Exit Sub
End If
' Now see if they specified a "to" date
If Not IsNothing(Me.txtTranDateEnd) Then
' First, make sure it's a valid date
If Not IsDate(Me.txtTranDateEnd) Then
' Nope, warn them and bail
MsgBox "The value in Contact To is not a valid date.", vbCritical, gstrAppTitle
Exit Sub
End If
' Got two dates, now make sure "to" is >= "from"
If Me.txtTranDateEnd < Me.txtTranDateStart Then
MsgBox "Contact To date must be greater than or equal to Contact From date.", _
vbCritical, gstrAppTitle
Exit Sub
End If
End If
Else
' No "from" but did they specify a "to"?
If Not IsNothing(Me.txtTranDateEnd) Then
' Make sure it's a valid date
If Not IsDate(Me.txtTranDateEnd) Then
' Nope, warn them and bail
MsgBox "The value in Contact To is not a valid date.", vbCritical, gstrAppTitle
Exit Sub
End If
End If
End If
' OK, start building the filter
' If specified a sold or leased value
If Not IsNothing(Me.cmdSoldorLeased) Then
' .. build the predicate
varWhere = "[SoldorLeased?] = '" & Me.cmdSoldorLeased & "'"
End If
' If specified a property city value
If Not IsNothing(Me.cmbPropertyCity) Then
' .. build the predicate
varWhere = (varWhere + " AND ") & "[PropertyCity] LIKE '" & Me.cmbPropertyCity & "'"
End If
' Do Property Type next
If Not IsNothing(Me.cmbPropertyType) Then
' .. build the predicate
' Note: taking advantage of Null propogation
' so we don't have to test for any previous predicate
varWhere = (varWhere + " AND ") & "[PropertyType] LIKE '" & Me.cmbPropertyType & "*'"
End If
' Check Transaction Date From first
If Not IsNothing(Me.txtTranDateStart) Then
' .. build the predicate
varDateSearch = "tblComparables.Transaction Date >= #" & Me.txtTranDateStart & "#"
End If
' Now do Transaction Date To
If Not IsNothing(Me.txtTranDateEnd) Then
' .. add to the predicate, but add one because ContactDateTime includes
' a date AND a time
varDateSearch = (varDateSearch + " AND ") & _
"tblComparables.Transaction Date < #" & CDate(Me.txtTranDateEnd) + 1 & "#"
End If
' Did we build any date filter?
If Not IsNothing(varDateSearch) Then
' OK, add to the overall filter
' Must use a subquery here because the value is in a linking table...
varWhere = (varWhere + " AND ") & _
"[PropID] IN (SELECT PropID FROM qryComparablesDrillDown " & _
"WHERE " & varDateSearch & ")"
End If
' Check to see that we built a filter
If IsNothing(varWhere) Then
MsgBox "You must enter at least one search criteria.", vbInformation, gstrAppTitle
Exit Sub
End If
'Open a recordset to see if any rows returned with this filter
'Set rst = DBEngine(0)(0).OpenRecordset("SELECT * FROM qryComparablesDrillDown WHERE " & varWhere)
' See if found none
'If rst.RecordCount = 0 Then
'MsgBox "No Contacts meet your criteria.", vbInformation, gstrAppTitle
'Clean up recordset
'rst.Close
'Set rst = Nothing
'Exit Sub
'End If
' Hide me to fix later focus problems
Me.Visible = False
' Move to last to find out how many
'rst.MoveLast
' Open Comparables filtered
' Note: if form already open, this just applies the filter
DoCmd.OpenForm "frmComparablesDrillDown", WhereCondition:=varWhere
' Make sure focus is on contacts
Forms!frmComparablesDrillDown.SetFocus
' Done
DoCmd.Close acForm, Me.Name
' Clean up recordset
'rst.Close
'Set rst = Nothing
End Sub