josephbupe
Registered User.
- Local time
- Today, 14:07
- Joined
- Jan 31, 2008
- Messages
- 247
Hi,
I am having a tough time figuring out what is wrong with my project on running queries with the SQL statements.
The function is imported into my project from somewhere, modified a bit and now looks like this:
And the search code:
The problem is that the criterias in the two red list boxes cant return the results even when I have data to return in the columns TYPE and PURPOSE in the lstIncidents list box above.
Please, see attached sample for easy reference and help.
I will appreciate your help.
Joseph
I am having a tough time figuring out what is wrong with my project on running queries with the SQL statements.
The function is imported into my project from somewhere, modified a bit and now looks like this:
Code:
Private Function BuildFilter() As Variant
Dim varWhere As Variant
Dim strField As String [COLOR=Green]'for the date field[/COLOR]
varWhere = Null [COLOR=Green] ' Main filter[/COLOR]
[COLOR=Green]'..............................................................................................................................
[/COLOR]
[COLOR=Green]' Const conDateFormat = "\#mm\/dd\/yyyy\#"[/COLOR]
Const conDateFormat = "\#dd\/mm\/yyyy\#"
[COLOR=Green] ' strReport = "R_Programmes"[/COLOR]
strField = "[Incident_Date]"
varWhere = ""
[COLOR=Green] ' ================================================================================
' CHECK FOR DATE RANGE
' ================================================================================[/COLOR]
If IsNull(Me.txtIncidentStartDate) Then
If Not IsNull(Me.txtIncidentEndDate) Then [COLOR=Green] 'End date, but no start.[/COLOR]
varWhere = "(" & strField & " <= " & Format(Me.txtIncidentEndDate, conDateFormat) & ") "
Else
[COLOR=Green] 'neither start nor end dates, do nothing to varWhere[/COLOR]
End If
Else
If IsNull(Me.txtIncidentEndDate) Then[COLOR=Green] 'Start date, but no End.[/COLOR]
varWhere = "(" & strField & " >= " & Format(Me.txtIncidentStartDate, conDateFormat) & ") "
Else [COLOR=Green]'Both start and end dates present.[/COLOR]
varWhere = "(" & strField & " Between " & Format(Me.txtIncidentStartDate, conDateFormat) _
& " AND " & Format(Me.txtIncidentEndDate, conDateFormat) & ")"
End If
End If
[COLOR=Green] ' Check for LIKE Other Names[/COLOR]
If Me.txtIncident <> "" Then
varWhere = varWhere & "[Incident] LIKE '*" & Me.txtIncident & "*' AND "
End If
[COLOR=Green] ' Check for LIKE Ref[/COLOR]
If Me.lstStatus <> "" Then
varWhere = varWhere & "[Status] LIKE '*" & Me.lstStatus & "*' AND "
End If
[COLOR=Green] ' Check for Incident Type[/COLOR]
If Me.lstIncidentType <> "" Then
varWhere = varWhere & "[Type] Like '*" & Me.lstIncidentType & "*' AND "
End If
[COLOR=Green] ' Check for Incident Type[/COLOR]
If Me.lstPurpose <> "" Then
varWhere = varWhere & "[Purpose] Like '*" & Me.lstPurpose & "*' AND "
End If
[COLOR=Green] ' Check if there is a filter to return...[/COLOR]
If IsNull(varWhere) Then
varWhere = ""
Else
varWhere = "WHERE " & varWhere
[COLOR=Green] ' strip off last "AND" in the filter[/COLOR]
If Right(varWhere, 5) = " AND " Then
varWhere = Left(varWhere, Len(varWhere) - 5)
End If
End If
Debug.Print varWhere
BuildFilter = varWhere
End Function
Code:
Private Sub cmdSearchIncident_Click()
' Update the record source
Me.lstIncidents.RowSource = "SELECT * FROM Query_Incidents " & BuildFilter
[COLOR=Green] ' Requery the form[/COLOR]
Me.lstIncidents.Requery
End Sub
Please, see attached sample for easy reference and help.
I will appreciate your help.
Joseph
Attachments
Last edited: