I have two different scenarios here that I am dealing with.
The first one is:
If I have a date in the code works fine when selecting from the list boxes. If I dont have a date then it gives me a syntax error?
Is there maybe a way of an If in that code? See below.
Code:
Private Sub CmdReport_Click()
Dim varItem As Variant
Dim strDoc As String
Dim sCriteria As String
If IsNull(Me.LstCategoryReport.Column(0)) Then
MsgBox "You must select a Report from Reprot List!"
Exit Sub
End If
On Error GoTo Err_Handler
With Me.LstCategoryReport
For Each varItem In .ItemsSelected
strDoc = .Column(1, varItem) 'assumes list box has two columns, the first one holding the reportID is the bound one and hidden
Next
End With
If Not IsNull(Me.LstCategory.Column(0)) And IsNull(Me.LstCategorySub.Column(0)) Then
sCriteria = "[CategoryID] in(" & getLBX(Me.LstCategory) & ")AND " & Forms![frmMainMenu].[sfrmMainMenu].Form.txtReportFilter
ElseIf Not IsNull(Me.LstCategory.Column(0)) And Not IsNull(Me.LstCategorySub.Column(0)) Then
sCriteria = "[SubCategoryID] in(" & getLBX(Me.LstCategorySub) & ")AND " & Forms![frmMainMenu].[sfrmMainMenu].Form.txtReportFilter
Else
sCriteria = Forms![frmMainMenu].[sfrmMainMenu].Form.txtReportFilter
End If
Debug.Print sCriteria
DoCmd.OpenReport strDoc, acViewPreview, , sCriteria
Exit_Handler:
Exit Sub
Err_Handler:
If Err.Number <> 2501 Then 'Ignore "Report cancelled" error.
MsgBox "Error " & Err.Number & " - " & Err.Description, , "cmdPreview_Click"
End If
Resume Exit_Handler
End Sub
The second issue is:
If I dont have a date the code works well, if I put in the between dates then it gives me a syntax error. Oposit of the above!
See code below:
Code:
Private Sub CmdReport_Click()
On Error GoTo Err_Handler
'Purpose: Open the report filtered to the items selected in the list box.
'Author: Allen J Browne, 2004. http://allenbrowne.com
Dim varItem As Variant 'Selected items
Dim strWhere As String 'String to use as WhereCondition
Dim strDescrip As String 'Description of WhereCondition
Dim lngLen As Long 'Length of string
Dim strDelim As String 'Delimiter for this field type.
Dim strDoc As String 'Name of report to open.
If IsNull(Me.LstAccountReport.Column(0)) Then
MsgBox "You must select a Report from Reprot List!"
Exit Sub
End If
'strDelim = """" 'Delimiter appropriate to field type. See note 1.
With Me.LstAccountReport
For Each varItem In .ItemsSelected
strDoc = .Column(1, varItem) 'assumes list box has two columns, the first one holding the reportID is the bound one and hidden
'now open the report
DoCmd.OpenReport strDoc, acViewPreview, WhereCondition:=strWhere & Forms![frmMainMenu].[sfrmMainMenu].Form.txtReportFilter, OpenArgs:=strDescrip
Next
End With
'Loop through the ItemsSelected in the list box.
With Me.LstAccountType
For Each varItem In .ItemsSelected
If Not IsNull(varItem) Then
'Build up the filter from the bound column (hidden).
strWhere = strWhere & strDelim & .ItemData(varItem) & strDelim
'Build up the description from the text in the visible column. See note 2.
strDescrip = strDescrip & """" & .Column(1, varItem) & """, "
End If
Next
End With
'Remove trailing comma. Add field name, IN operator, and brackets.
lngLen = Len(strWhere) - 0
If lngLen > 0 Then
strWhere = "[AccountTypeID] IN(" & Left$(strWhere, lngLen) & ")"
lngLen = Len(strDescrip) - 0
If lngLen > 0 Then
strDescrip = "AccountType: " & Left$(strDescrip, lngLen)
End If
End If
'Report will not filter if open, so close it. For Access 97, see note 3.
If CurrentProject.AllReports(strDoc).IsLoaded Then
DoCmd.Close acReport, strDoc
End If
'Omit the last argument for Access 2000 and earlier. See note 4.
'DoCmd.OpenReport strDoc, acViewPreview, WhereCondition:=strWhere, OpenArgs:=strDescrip
DoCmd.OpenReport strDoc, acViewPreview, WhereCondition:=strWhere & Forms![frmMainMenu].[sfrmMainMenu].Form.txtReportFilter, OpenArgs:=strDescrip
Debug.Print strWhere
Exit_Handler:
Exit Sub
Err_Handler:
If Err.Number <> 2501 Then 'Ignore "Report cancelled" error.
MsgBox "Error " & Err.Number & " - " & Err.Description, , "CmdReport_Click"
End If
Resume Exit_Handler
End Sub
The report filter is this:
Group filter options are
1 "Show betweek these dates
2 "Dont apply filter
Code:
=Choose([grpFilterOptions],"[TransDate] Between #" & [Forms]![frmMainMenu].[sfrmMainMenu].[Form].[BeginningTransDate] & "# AND #" & [Forms]![frmMainMenu].[sfrmMainMenu].[Form].[EndingTransDate] & "#","")