query (on the fly) doesnt show empty records

arsh

New member
Local time
Today, 07:51
Joined
Nov 30, 2006
Messages
7
In my database I have filter form with 3 combos.when I press ok button on form it shows all records in a query (on the fly).But it doesnt show empty records,so I added
strOffice = " Like '*' OR 'Is Null' " but it doesnt work.
Please help and thanks in advance.

Private Sub cmdOK_Click()
' Pointer to error handler
On Error GoTo cmdOK_Click_err
' Declare variables
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim strOffice As String
Dim strDepartment As String
Dim strGender As String
Dim strSQL As String
' Identify the database and assign it to the variable
Set db = CurrentDb
' Check for the existence of the query, create it if not found,
' and assign it to the variable
If Not QueryExists("qryStaffListQuery") Then
Set qdf = db.CreateQueryDef("qryStaffListQuery")
Else
Set qdf = db.QueryDefs("qryStaffListQuery")
End If
' Get the values from the combo boxes
If IsNull(Me.cboOffice.Value) Then
strOffice = " Like '*' "
Else
strOffice = "='" & Me.cboOffice.Value & "' "
End If
If IsNull(Me.cboDepartment.Value) Then
strDepartment = " Like '*' "
Else
strDepartment = "='" & Me.cboDepartment.Value & "' "
End If
If IsNull(Me.cboGender.Value) Then
strGender = " Like '*' "
Else
strGender = "='" & Me.cboGender.Value & "'"
End If
' Build the SQL string
strSQL = "SELECT tblStaff.* " & _
"FROM tblStaff " & _
"WHERE tblStaff.Office" & strOffice & _
"AND tblStaff.Department" & strDepartment & _
"AND tblStaff.Gender" & strGender & _
"ORDER BY tblStaff.LastName,tblStaff.FirstName;"
' Pass the SQL string to the query
qdf.SQL = strSQL
' Turn off screen updating
DoCmd.Echo False
' Check the state of the query and close it if it is open
If Application.SysCmd(acSysCmdGetObjectState, acQuery, "qryStaffListQuery") = acObjStateOpen Then
DoCmd.Close acQuery, "qryStaffListQuery"
End If
' Open the query
DoCmd.OpenQuery "qryStaffListQuery"
cmdOK_Click_exit:
' Turn on screen updating
DoCmd.Echo True
' Clear the object variables
Set qdf = Nothing
Set db = Nothing
Exit Sub
cmdOK_Click_err:
' Handle errors
MsgBox "An unexpected error has occurred." & _
vbCrLf & "Please note of the following details:" & _
vbCrLf & "Error Number: " & Err.Number & _
vbCrLf & "Description: " & Err.Description _
, vbCritical, "Error"
Resume cmdOK_Click_exit
End Sub
 
Good to see my QueryExists() function getting some use =) Attach the database and I can help debug.
 

Users who are viewing this thread

Back
Top Bottom