S
Scott
I have a form in which the user can pick a start and end date, an Operator
Number and a job code. After clicking the OK button they should get query
results based on their choices. I also tried to set it up so the user can
leave parameters empty like job codes and see all the codes the operator
worked. No matter what parameters are chosen or not the query comes back
with no results everytime. Is there anything wrong with this code?
Private Sub cmdOK_Click()
On Error GoTo cmdOK_Click_err
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim strDate As String
Dim strCode As String
Dim strOperNum As String
Set db = CurrentDb
If Not QueryExists("qryStaffData") Then
Set qdf = db.CreateQueryDef("qryStaffData")
Else
Set qdf = db.QueryDefs("qryStaffData")
End If
If IsNull(Me.txtEndDate.Value) Then
If IsNull(Me.txtStartDate.Value) Then
strDate = " Like '*' "
End If
Else
If IsNull(Me.txtStartDate.Value) Then
MsgBox "You have not entered a start date. " _
& "I have entered a start date one day prior " _
& "to the end date.", vbInformation, "No Start Date!"
Me.cboStartDay.Value = Me.cboEndDay.Value - 1
Me.cboStartMonth.Value = Me.cboEndMonth.Value
Me.cboStartYear.Value = Me.cboEndYear
Me.txtStartDate.Value = DateSerial(Me.cboStartYear,
Me.cboStartMonth, Me.cboStartDay)
Else
strDate = " Between '& me.txtStartDate.Value &' And '&
me.txtEndDate.value &'"
End If
End If
If IsNull(Me.cboCode.Value) Then
strCode = " Like '*' "
Else
strCode = "='" & Me.cboCode.Value & "' "
End If
If IsNull(Me.cboOperNum.Value) Then
strOperNum = " Like '*' "
Else
strOperNum = "='" & Me.cboOperNum.Value & "' "
End If
strSQL = "SELECT Data.* " & _
"FROM Data " & _
"WHERE Data.Date" & strDate & _
"AND Data.Code" & strCode & _
"AND Data.OperNum" & strOperNum & _
"ORDER BY Data.Date;"
qdf.SQL = strSQL
DoCmd.Echo False
If Application.SysCmd(acSysCmdGetObjectState, acQuery, "qryStaffData") =
acObjStateOpen Then
DoCmd.Close acQuery, "qryStaffData"
End If
DoCmd.OpenQuery "qryStaffData"
cmdOK_Click_exit:
DoCmd.Echo True
Set qdf = Nothing
Set db = Nothing
Exit Sub
cmdOK_Click_err:
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
Number and a job code. After clicking the OK button they should get query
results based on their choices. I also tried to set it up so the user can
leave parameters empty like job codes and see all the codes the operator
worked. No matter what parameters are chosen or not the query comes back
with no results everytime. Is there anything wrong with this code?
Private Sub cmdOK_Click()
On Error GoTo cmdOK_Click_err
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim strDate As String
Dim strCode As String
Dim strOperNum As String
Set db = CurrentDb
If Not QueryExists("qryStaffData") Then
Set qdf = db.CreateQueryDef("qryStaffData")
Else
Set qdf = db.QueryDefs("qryStaffData")
End If
If IsNull(Me.txtEndDate.Value) Then
If IsNull(Me.txtStartDate.Value) Then
strDate = " Like '*' "
End If
Else
If IsNull(Me.txtStartDate.Value) Then
MsgBox "You have not entered a start date. " _
& "I have entered a start date one day prior " _
& "to the end date.", vbInformation, "No Start Date!"
Me.cboStartDay.Value = Me.cboEndDay.Value - 1
Me.cboStartMonth.Value = Me.cboEndMonth.Value
Me.cboStartYear.Value = Me.cboEndYear
Me.txtStartDate.Value = DateSerial(Me.cboStartYear,
Me.cboStartMonth, Me.cboStartDay)
Else
strDate = " Between '& me.txtStartDate.Value &' And '&
me.txtEndDate.value &'"
End If
End If
If IsNull(Me.cboCode.Value) Then
strCode = " Like '*' "
Else
strCode = "='" & Me.cboCode.Value & "' "
End If
If IsNull(Me.cboOperNum.Value) Then
strOperNum = " Like '*' "
Else
strOperNum = "='" & Me.cboOperNum.Value & "' "
End If
strSQL = "SELECT Data.* " & _
"FROM Data " & _
"WHERE Data.Date" & strDate & _
"AND Data.Code" & strCode & _
"AND Data.OperNum" & strOperNum & _
"ORDER BY Data.Date;"
qdf.SQL = strSQL
DoCmd.Echo False
If Application.SysCmd(acSysCmdGetObjectState, acQuery, "qryStaffData") =
acObjStateOpen Then
DoCmd.Close acQuery, "qryStaffData"
End If
DoCmd.OpenQuery "qryStaffData"
cmdOK_Click_exit:
DoCmd.Echo True
Set qdf = Nothing
Set db = Nothing
Exit Sub
cmdOK_Click_err:
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