C
cityscaper
I have a form for user to pick multiple and/or parameters for a report and
can't quite get the programming for the between dates working correctly. I
keep getting the error: Run Time error 3075
Syntax error (missing operator) in query expression ‘strDateField = &
tblSession.StartDate ([tblSession.StartDate] BETWEEN 1/1/2008) AND
([tblSession.StartDate] 12/31/2008) AND tblEvent.EventName Like ‘*’ AND
tblPersonnel.BusinessUnit Like ‘*’
It seems I've placed the strDateField incorrectly, but can't figure out
where. Below is the code behind the OK button on the form.
Private Sub cmdOK_Click()
Dim varItem As Variant
Dim strClass As String
Dim strDept As String
Dim strDateField As String
Dim strClassCondition As String
Dim strDeptCondition As String
Dim strSQL As String
Const conJetDate = "\#mm\/dd\/yyy\#"
If Not IsNull(Me.txtStartDate) Then
strDateField = "([tblSession.StartDate] BETWEEN " &
Format(Me.txtStartDate, conDateFormat) & ") AND "
End If
If Not IsNull(Me.txtEndDate) Then
strDateField = strDateField & "([tblSession.StartDate] " &
Format(Me.txtEndDate, conDateFormat) & ") "
End If
For Each varItem In Me.lstClass.ItemsSelected
strClass = strClass & ",'" & Me.lstClass.ItemData(varItem) _
& "'"
Next varItem
If Len(strClass) = 0 Then
strClass = "Like '*'"
Else
strClass = Right(strClass, Len(strClass) - 1)
strClass = "IN (" & strClass & ")"
End If
For Each varItem In Me.lstDept.ItemsSelected
strDept = strDept & ",'" & Me.lstDept.ItemData(varItem) _
& "'"
Next varItem
If Len(strDept) = 0 Then
strDept = "Like '*'"
Else
strDept = Right(strDept, Len(strDept) - 1)
strDept = "IN (" & strDept & ")"
End If
If Me.optAndClass.Value = True Then
strClassCondition = " AND "
Else
strClassCondition = " OR "
End If
If Me.optAndDept.Value = True Then
strDeptCondition = " AND "
Else
strDeptCondition = " OR "
End If
strSQL = "SELECT tblPersonnel.BusinessUnit, tblSession.StartDate,
tblEvent.EventName, tblPersonnelAndSession.EmpID, tblPersonnel.LastName,
tblPersonnel.FirstName, tblPersonnel.EmpID, tblPersonnel.BusinessUnit,
tblPersonnel.ActiveBSCEmployee, tblPersonnel.ActivePM, " & _
"tblPersonnel.PMLevelCode, tblPersonnelAndSession.SessionID,
tblPersonnelAndSession.AttendanceStatus, tblSession.StartDate,
tblSession.SessionLocation, tblSession.InstructorID,
tblSession.SessionCancelled, tblEvent.EventID " & _
"FROM tblPersonnel INNER JOIN (tblPersonnelAndSession INNER JOIN (tblEvent
INNER JOIN tblSession ON tblEvent.EventID = tblSession.EventID) ON
tblPersonnelAndSession.SessionID = tblSession.SessionID) ON
tblPersonnel.EmpID = tblPersonnelAndSession.EmpID " & _
"WHERE strDateField = & tblSession.StartDate " & strDateField & _
strClassCondition & "tblEvent.EventName " & strClass & _
strDeptCondition & "tblPersonnel.BusinessUnit " & strDept & ";"
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Set db = CurrentDb
Set qdf = db.QueryDefs("qryOption")
qdf.sql = strSQL
Set qdf = Nothing
Set db = Nothing
DoCmd.OpenQuery "qryOption"
DoCmd.OpenReport "rptCourseBU", acViewPreview
DoCmd.Close acForm, Me.Name
DoCmd.Close acQuery, "qryOption"
End Sub
Private Sub optAndClass_Click()
If Me.optAndClass.Value = True Then
Me.optOrClass.Value = False
Else
Me.optAndClass.Value = True
End If
End Sub
Private Sub optAndDept_Click()
If Me.optAndDept.Value = True Then
Me.optOrDept.Value = False
Else
Me.optAndDept.Value = True
End If
End Sub
-- Any help in correcting my code would be appreciated.
Many Thanks for any assistance
can't quite get the programming for the between dates working correctly. I
keep getting the error: Run Time error 3075
Syntax error (missing operator) in query expression ‘strDateField = &
tblSession.StartDate ([tblSession.StartDate] BETWEEN 1/1/2008) AND
([tblSession.StartDate] 12/31/2008) AND tblEvent.EventName Like ‘*’ AND
tblPersonnel.BusinessUnit Like ‘*’
It seems I've placed the strDateField incorrectly, but can't figure out
where. Below is the code behind the OK button on the form.
Private Sub cmdOK_Click()
Dim varItem As Variant
Dim strClass As String
Dim strDept As String
Dim strDateField As String
Dim strClassCondition As String
Dim strDeptCondition As String
Dim strSQL As String
Const conJetDate = "\#mm\/dd\/yyy\#"
If Not IsNull(Me.txtStartDate) Then
strDateField = "([tblSession.StartDate] BETWEEN " &
Format(Me.txtStartDate, conDateFormat) & ") AND "
End If
If Not IsNull(Me.txtEndDate) Then
strDateField = strDateField & "([tblSession.StartDate] " &
Format(Me.txtEndDate, conDateFormat) & ") "
End If
For Each varItem In Me.lstClass.ItemsSelected
strClass = strClass & ",'" & Me.lstClass.ItemData(varItem) _
& "'"
Next varItem
If Len(strClass) = 0 Then
strClass = "Like '*'"
Else
strClass = Right(strClass, Len(strClass) - 1)
strClass = "IN (" & strClass & ")"
End If
For Each varItem In Me.lstDept.ItemsSelected
strDept = strDept & ",'" & Me.lstDept.ItemData(varItem) _
& "'"
Next varItem
If Len(strDept) = 0 Then
strDept = "Like '*'"
Else
strDept = Right(strDept, Len(strDept) - 1)
strDept = "IN (" & strDept & ")"
End If
If Me.optAndClass.Value = True Then
strClassCondition = " AND "
Else
strClassCondition = " OR "
End If
If Me.optAndDept.Value = True Then
strDeptCondition = " AND "
Else
strDeptCondition = " OR "
End If
strSQL = "SELECT tblPersonnel.BusinessUnit, tblSession.StartDate,
tblEvent.EventName, tblPersonnelAndSession.EmpID, tblPersonnel.LastName,
tblPersonnel.FirstName, tblPersonnel.EmpID, tblPersonnel.BusinessUnit,
tblPersonnel.ActiveBSCEmployee, tblPersonnel.ActivePM, " & _
"tblPersonnel.PMLevelCode, tblPersonnelAndSession.SessionID,
tblPersonnelAndSession.AttendanceStatus, tblSession.StartDate,
tblSession.SessionLocation, tblSession.InstructorID,
tblSession.SessionCancelled, tblEvent.EventID " & _
"FROM tblPersonnel INNER JOIN (tblPersonnelAndSession INNER JOIN (tblEvent
INNER JOIN tblSession ON tblEvent.EventID = tblSession.EventID) ON
tblPersonnelAndSession.SessionID = tblSession.SessionID) ON
tblPersonnel.EmpID = tblPersonnelAndSession.EmpID " & _
"WHERE strDateField = & tblSession.StartDate " & strDateField & _
strClassCondition & "tblEvent.EventName " & strClass & _
strDeptCondition & "tblPersonnel.BusinessUnit " & strDept & ";"
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Set db = CurrentDb
Set qdf = db.QueryDefs("qryOption")
qdf.sql = strSQL
Set qdf = Nothing
Set db = Nothing
DoCmd.OpenQuery "qryOption"
DoCmd.OpenReport "rptCourseBU", acViewPreview
DoCmd.Close acForm, Me.Name
DoCmd.Close acQuery, "qryOption"
End Sub
Private Sub optAndClass_Click()
If Me.optAndClass.Value = True Then
Me.optOrClass.Value = False
Else
Me.optAndClass.Value = True
End If
End Sub
Private Sub optAndDept_Click()
If Me.optAndDept.Value = True Then
Me.optOrDept.Value = False
Else
Me.optAndDept.Value = True
End If
End Sub
-- Any help in correcting my code would be appreciated.
Many Thanks for any assistance