Private Sub cmdFind_Click()
Dim strSQL As String
Dim strOrder As String
Dim strWhere As String
Dim dbNm As Database
Dim qryDef As QueryDef
Dim ans As Integer
Set dbNm = CurrentDb()
'Constant Select statement for the RowSource
strSQL = "SELECT 'IRS-' & Right('0000' & Q.[IRS No], 4) As [IRS No], " & _
" Q.[Process Area], Q.[IRS Type], Q.Locked, Q.[For Rev], " & _
" Q.Scope, Q.lngArea, Q.lngType " & _
"FROM qryIRS As Q "
strWhere = ""
[COLOR=black cyan]' ORDER BY has problems with using a
' field alias. Use the ordinal position instead.[/color]
strOrder = " ORDER BY 1 ;"
'Set the WHERE clause for the Listbox RowSource
'if information has been entered into a field on the form
[COLOR=black cyan]' You really don't need those 'include everything' clauses.
' just use the conditions that will result in filtering.[/color]
If Not IsNull(Me.cboArea) Then
strWhere = strWhere & " (Q.lngArea = " & Me.cboArea.Value & ") AND "
End If
If Not IsNull(Me.cboType) Then
strWhere = strWhere & " (Q.lngType = " & Me.cboType.Value & ") AND "
End If
If Not IsNull(Me.txtScope) Then
strWhere = strWhere & " (Q.Scope LIKE '*" & Me.txtScope & "*') AND "
End If
'Remove the last "AND" from the SQL statment
[COLOR=black cyan]' If the WHERE clause is empty then there is
' no WHERE clause and everything is included.[/color]
If Len(strWhere) > 0 Then
strWhere = " WHERE " & strWhere
strWhere = Left$(strWhere, Len(strWhere) - 5)
End If
[COLOR=black cyan]' Copy the SQL from the Immediate window[/color]
[COLOR=black cyan]' and see if it runs from the query window.[/color]
Debug.Print strSQL & " " & strWhere & " " & strOrder
Set qryDef = dbNm.QueryDefs("qryFindIRS")
qryDef.SQL = strSQL & " " & strWhere & " " & strOrder
'Pass the SQL to the RowSource of the listbox
Me.lstIRS.RowSource = strSQL & " " & strWhere & " " & strOrder
'Display a message box if no results
If Me.lstIRS.ListCount = 0 Then
ans = MsgBox("There are no records that match your search criteria!" & vbCrLf & _
"Do you want to add a new IRS?", vbExclamation + vbYesNo, "SWIPS")
If ans = vbYes Then
DoCmd.OpenForm "frmIRS", acNormal, , , acFormAdd
DoCmd.Close acForm, "frmFindIRS"
Else
End If
End If
End Sub