Can anyone tell me what could be wrong with the SQL string this code generates for strFinal variable???
When it runs, it doesn't take into account the strUser filter when cboStatus case = "ALL"... Cannot figure it out... I included strFinal output below:
When it runs, it doesn't take into account the strUser filter when cboStatus case = "ALL"... Cannot figure it out... I included strFinal output below:
Code:
Private Sub FormDef()
If ErrOn Then On Error GoTo ErrorHandler
If Len(cboStatus & vbNullString) = 0 Then
MsgBox ("Choose a Status.")
GoTo ProcedureExit
End If
If chkDateRange Then
If Len(txtFromDate & vbNullString) = 0 Or Len(txtToDate & vbNullString) = 0 Then
MsgBox ("Enter a Date Range.")
GoTo ProcedureExit
End If
End If
Dim strSQL As String
Dim strUser As String
Dim strStatus As String
Dim strDates As String
Dim strOrderby As String
Dim strFinal As String
strSQL = "SELECT tblStatusReportUsers.Username, tblStatusReport.ID, tblStatusReport.Creator, tblStatusReport.CreateDate, tblStatusReport.SubjectMatter, tblStatusReport.Status, tblStatusReport.LastUpdated " & _
"FROM tblStatusReport INNER JOIN tblStatusReportUsers ON tblStatusReport.ID = tblStatusReportUsers.ReportID_PK " & _
"GROUP BY tblStatusReportUsers.Username, tblStatusReport.ID, tblStatusReport.Creator, tblStatusReport.CreateDate, tblStatusReport.SubjectMatter, tblStatusReport.Status, tblStatusReport.LastUpdated "
strUser = "HAVING Username = '" & cboUsername & "' "
Select Case cboStatus
Case "ALL"
strStatus = "and Status = 'OPEN' or Status = 'CLOSED' "
Case "OPEN"
strStatus = "and Status = 'OPEN' "
Case "CLOSED"
strStatus = "and Status = 'CLOSED' "
End Select
Select Case chkDateRange
Case True
strDates = "and LastUpdated Between #" & txtFromDate & "# and #" & DateAdd("d", 1, txtToDate) & "# "
Case False
strDates = "and LastUpdated Between #1/1/2010# and #" & DateAdd("d", 1, Now) & "# "
End Select
strOrderby = "ORDER BY tblStatusReport.LastUpdated DESC;"
strFinal = strSQL & strUser & strStatus & strDates & strOrderby
Forms!frmStatusReports!subMain.Form.RecordSource = strFinal
'Debug.Print strFinal
ProcedureExit:
Exit Sub
ErrorHandler:
If Err.Number = 2455 Then Resume Next
MsgBox "Error" & ": " & Err.Number & vbCrLf & "Description: " _
& Err.Description, vbExclamation, Me.Name & ".FormDef"
Resume ProcedureExit
End Sub
Code:
SELECT tblStatusReportUsers.Username, tblStatusReport.ID, tblStatusReport.Creator, tblStatusReport.CreateDate, tblStatusReport.SubjectMatter, tblStatusReport.Status, tblStatusReport.LastUpdated FROM tblStatusReport INNER JOIN tblStatusReportUsers ON tblStatusReport.ID = tblStatusReportUsers.ReportID_PK GROUP BY tblStatusReportUsers.Username, tblStatusReport.ID, tblStatusReport.Creator, tblStatusReport.CreateDate, tblStatusReport.SubjectMatter, tblStatusReport.Status, tblStatusReport.LastUpdated HAVING Username = 'jmatyjr' and Status = 'OPEN' or Status = 'CLOSED' and LastUpdated Between #1/1/2010# and #2/24/2017 2:33:02 PM# ORDER BY tblStatusReport.LastUpdated DESC;