Help with WHERE filter on opening report from dialog

D

DeDBlanK

In advance me.help = grateful AND thankful =P
I am having an issue with some code. I am trying to do a dcount on
records verifying that there are records before opening the report (in
other words, handle it in the dialog and not the reports No Data
event)
This code works without the DCount (have commented in the code)
'****CODE START****
Private Sub cmdReport_Click()

'Remove the single quote from start of next line once you have it
working.
'On Error GoTo Err_Handler
'Purpose: Filter a report to a date range.
'Documentation: http://allenbrowne.com/casu-08.html
'Note: Filter uses "less than the next day" in case the field has a
time component.
Dim strReport As String
Dim strDateField As String
Dim varWhere As Variant
Dim lngView As Integer
Dim msg As String
Dim strRecordSource As String

Const strcJetDate = "\#mm\/dd\/yyyy\#"


'DO set the CASE values.
Select Case Me.Frame18.Value
Case 1
strReport = "rptDetailRepair" 'Put your report name in
these quotes.
Case 2
strReport = "rptSummaryQAD"

Case 3
strReport = "rptSummaryReport"

Case 4
strReport = "rptSummaryReportRepair"

Case 5
strReport = "rptSummaryDateRangeDowntime"

Case 6
strReport = "rptSQCDMonthly"

Case 7
strReport = "rptOEE"

Case Null
msg = "value is null"
MsgBox msg, vbInformation, "No Report"
GoTo Exit_Handler
Case Else
msg = "No report selected"
MsgBox msg, vbInformation, "No Report"
GoTo Exit_Handler
End Select


strDateField = "[dtmDate]" 'Put your field name in the square
brackets in these quotes.
lngView = acViewPreview 'Use acViewNormal to print instead of
preview.
varWhere = Null 'Set to null to check later if value
has been assigned

'Build the filter string.
If IsDate(Me.txtStartDate) Then
varWhere = "(" & strDateField & " >= " &
Format(Me.txtStartDate, strcJetDate) & ")"
End If

'I like to use the DateValue function to strip times from date/
time Fields
If IsDate(Me.txtEndDate) Then 'Check if start date had a value,
if so add AND and end date
If varWhere <> vbNullString Then
varWhere = (varWhere + " AND ") & "(DateValue(" &
strDateField & ")<= " & Format(Me.txtEndDate, strcJetDate) & ")"
Else ' just add end date
varWhere = "(DateValue(" & strDateField & ")<= " &
Format(Me.txtEndDate, strcJetDate) & ")"
End If
End If

'build filter string for Shift
varWhere = (varWhere + " AND ") & "([strShift]" &
fnMultiList(Me.lstShift) & ")"


'build filter string for Dept
If Me.lstDept.ItemsSelected.Count >= 1 Then
varWhere = (varWhere + " AND ") & "([fkLine]" &
fnMultiList(Me.lstDept) & ")"
End If

'Close the report if already open: otherwise it won't filter
properly.
If CurrentProject.AllReports(strReport).IsLoaded Then
DoCmd.Close acReport, strReport
End If

'******CODE ISSUE*****
'check for records in the report
DoCmd.OpenReport strReport, acViewDesign, , varWhere, acHidden

strRecordSource = Reports(strReport).recordsource

If DCount("*", strRecordSource, varWhere) > 0 Then
'Open the report. ****THIS SECTION WORKS****
Debug.Print DCount("*", strRecordSource, varWhere)
Debug.Print varWhere
DoCmd.OpenReport strReport, lngView, , varWhere
'*****END THIS SECTION WORKS*******
Else
'Close report with message
msg = "Sorry, Unable to find data that matches that criteria"
MsgBox msg, vbExclamation, "No Data"
DoCmd.Close acReport, strReport, acSaveNo
End If
'***END CODE ISSUE****
Exit_Handler:
Exit Sub


Err_Handler:
If Err.Number <> 2501 Then
MsgBox "Error " & Err.Number & ": " & Err.Description,
vbExclamation, "Cannot open report"
End If
Resume Exit_Handler


End Sub
'****CODE END****
 
D

DeDBlanK

In advance me.help = grateful AND thankful =P
I am having an issue with some code.  I am trying to do a dcount on
records verifying that there are records before opening the report (in
other words, handle it in the dialog and not the reports No Data
event)
This code works without the DCount (have commented in the code)
'****CODE START****
Private Sub cmdReport_Click()

'Remove the single quote from start of next line once you have it
working.
'On Error GoTo Err_Handler
'Purpose:       Filter a report to a date range.
'Documentation:http://allenbrowne.com/casu-08.html
'Note: Filter uses "less than the next day" in case the field has a
time component.
    Dim strReport As String
    Dim strDateField As String
    Dim varWhere As Variant
    Dim lngView As Integer
    Dim msg As String
    Dim strRecordSource As String

    Const strcJetDate = "\#mm\/dd\/yyyy\#"

    'DO set the CASE values.
    Select Case Me.Frame18.Value
    Case 1
        strReport = "rptDetailRepair"      'Put your report name in
these quotes.
    Case 2
        strReport = "rptSummaryQAD"

    Case 3
        strReport = "rptSummaryReport"

    Case 4
        strReport = "rptSummaryReportRepair"

    Case 5
        strReport = "rptSummaryDateRangeDowntime"

    Case 6
        strReport = "rptSQCDMonthly"

    Case 7
        strReport = "rptOEE"

    Case Null
        msg = "value is null"
        MsgBox msg, vbInformation, "No Report"
        GoTo Exit_Handler
    Case Else
        msg = "No report selected"
        MsgBox msg, vbInformation, "No Report"
        GoTo Exit_Handler
    End Select

    strDateField = "[dtmDate]"  'Put your field name in the square
brackets in these quotes.
    lngView = acViewPreview     'Use acViewNormal to print instead of
preview.
    varWhere = Null             'Set to null to check later if value
has been assigned

     'Build the filter string.
    If IsDate(Me.txtStartDate) Then
         varWhere = "(" & strDateField & " >= " &
Format(Me.txtStartDate, strcJetDate) & ")"
    End If

    'I like to use the DateValue function to strip times from date/
time Fields
    If IsDate(Me.txtEndDate) Then  'Check if start date had a value,
if so add AND and end date
        If varWhere <> vbNullString Then
            varWhere = (varWhere + " AND ") & "(DateValue("&
strDateField & ")<= " & Format(Me.txtEndDate, strcJetDate) & ")"
        Else  ' just add end date
            varWhere = "(DateValue(" & strDateField & ")<= " &
Format(Me.txtEndDate, strcJetDate) & ")"
        End If
    End If

    'build filter string for Shift
    varWhere = (varWhere + " AND ") & "([strShift]" &
fnMultiList(Me.lstShift) & ")"

    'build filter string for Dept
    If Me.lstDept.ItemsSelected.Count >= 1 Then
        varWhere = (varWhere + " AND ") & "([fkLine]" &
fnMultiList(Me.lstDept) & ")"
    End If

    'Close the report if already open: otherwise it won't filter
properly.
    If CurrentProject.AllReports(strReport).IsLoaded Then
        DoCmd.Close acReport, strReport
    End If

    '******CODE ISSUE*****
    'check for records in the report
    DoCmd.OpenReport strReport, acViewDesign, , varWhere, acHidden

    strRecordSource = Reports(strReport).recordsource

    If DCount("*", strRecordSource, varWhere) > 0 Then
        'Open the report.  ****THIS SECTION WORKS****
        Debug.Print DCount("*", strRecordSource, varWhere)
        Debug.Print varWhere
        DoCmd.OpenReport strReport, lngView, , varWhere
        '*****END THIS SECTION WORKS*******
    Else
        'Close report with message
        msg = "Sorry, Unable to find data that matches that criteria"
        MsgBox msg, vbExclamation, "No Data"
        DoCmd.Close acReport, strReport, acSaveNo
    End If
'***END CODE ISSUE****
Exit_Handler:
    Exit Sub

Err_Handler:
    If Err.Number <> 2501 Then
        MsgBox "Error " & Err.Number & ": " & Err.Description,
vbExclamation, "Cannot open report"
    End If
    Resume Exit_Handler

End Sub
'****CODE END****

guess I left a part out, the WHERE (if the DCOUNT is included in the
code) is being ignored when the report opens. However, the
Debug.Print varWhere is showing the criteria correctly. Don't know
why the DCount would cause this issue. Thanks, again for any help.
 
D

DeDBlanK

In advance me.help = greatful AND thankful =P
I am having an issue with some code. I am trying to do a dcount on
records verifying that there are records before opening the report
(in
other words, handle it in the dialog and not the reports No Data
event)
This code works without the DCount (have commented in the code)
'****CODE START****
Private Sub cmdReport_Click()

'Remove the single quote from start of next line once you have it
working.
'On Error GoTo Err_Handler
'Purpose: Filter a report to a date range.
'Documentation: http://allenbrowne.com/casu-08.html
'Note: Filter uses "less than the next day" in case the field has a
time component.
Dim strReport As String
Dim strDateField As String
Dim varWhere As Variant
Dim lngView As Integer
Dim msg As String
Dim strRecordSource As String


Const strcJetDate = "\#mm\/dd\/yyyy\#"


'DO set the CASE values.
Select Case Me.Frame18.Value
Case 1
strReport = "rptDetailRepair" 'Put your report name in
these quotes.
Case 2
strReport = "rptSummaryQAD"


Case 3
strReport = "rptSummaryReport"


Case 4
strReport = "rptSummaryReportRepair"


Case 5
strReport = "rptSummaryDateRangeDowntime"


Case 6
strReport = "rptSQCDMonthly"


Case 7
strReport = "rptOEE"


Case Null
msg = "value is null"
MsgBox msg, vbInformation, "No Report"
GoTo Exit_Handler
Case Else
msg = "No report selected"
MsgBox msg, vbInformation, "No Report"
GoTo Exit_Handler
End Select


strDateField = "[dtmDate]" 'Put your field name in the square
brackets in these quotes.
lngView = acViewPreview 'Use acViewNormal to print instead of
preview.
varWhere = Null 'Set to null to check later if value
has been assigned


'Build the filter string.
If IsDate(Me.txtStartDate) Then
varWhere = "(" & strDateField & " >= " &
Format(Me.txtStartDate, strcJetDate) & ")"
End If


'I like to use the DateValue function to strip times from date/
time Fields
If IsDate(Me.txtEndDate) Then 'Check if start date had a value,
if so add AND and end date
If varWhere <> vbNullString Then
varWhere = (varWhere + " AND ") & "(DateValue(" &
strDateField & ")<= " & Format(Me.txtEndDate, strcJetDate) & ")"
Else ' just add end date
varWhere = "(DateValue(" & strDateField & ")<= " &
Format(Me.txtEndDate, strcJetDate) & ")"
End If
End If


'build filter string for Shift
varWhere = (varWhere + " AND ") & "([strShift]" &
fnMultiList(Me.lstShift) & ")"


'build filter string for Dept
If Me.lstDept.ItemsSelected.Count >= 1 Then
varWhere = (varWhere + " AND ") & "([fkLine]" &
fnMultiList(Me.lstDept) & ")"
End If


'Close the report if already open: otherwise it won't filter
properly.
If CurrentProject.AllReports(strReport).IsLoaded Then
DoCmd.Close acReport, strReport
End If


'******CODE ISSUE*****
'check for records in the report
DoCmd.OpenReport strReport, acViewDesign, , varWhere, acHidden


strRecordSource = Reports(strReport).recordsource


If DCount("*", strRecordSource, varWhere) > 0 Then
'Open the report. ****THIS SECTION WORKS****
Debug.Print DCount("*", strRecordSource, varWhere)
Debug.Print varWhere
DoCmd.OpenReport strReport, lngView, , varWhere
'*****END THIS SECTION WORKS*******
Else
'Close report with message
msg = "Sorry, Unable to find data that matches that criteria"
MsgBox msg, vbExclamation, "No Data"
DoCmd.Close acReport, strReport, acSaveNo
End If
'***END CODE ISSUE****
Exit_Handler:
Exit Sub


Err_Handler:
If Err.Number <> 2501 Then
MsgBox "Error " & Err.Number & ": " & Err.Description,
vbExclamation, "Cannot open report"
End If
Resume Exit_Handler


End Sub
'****CODE END****

guess I left a part out, the WHERE (if the DCOUNT is included in the
code) is being ignored when the report opens. However, the
Debug.Print varWhere is showing the criteria correctly. Don't know
why the DCount would cause this issue. Thanks, again for any help.
 
D

Douglas J. Steele

You're opening a new instance of the report, not making the one you opened
hidden visible.

Try replacing with the line indicated:

'******CODE ISSUE*****
'check for records in the report
DoCmd.OpenReport strReport, acViewDesign, , varWhere, acHidden


strRecordSource = Reports(strReport).recordsource


If DCount("*", strRecordSource, varWhere) > 0 Then
'Open the report. ****THIS SECTION WORKS****
Debug.Print DCount("*", strRecordSource, varWhere)
Debug.Print varWhere
Reports(strReport).Visible = True ' <** CHANGE TO THIS
'*****END THIS SECTION WORKS*******
Else
'Close report with message
msg = "Sorry, Unable to find data that matches that criteria"
MsgBox msg, vbExclamation, "No Data"
DoCmd.Close acReport, strReport, acSaveNo
End If
'***END CODE ISSUE****


--
Doug Steele, Microsoft Access MVP
http://www.AccessMVP.com/DJSteele
Co-author: Access 2010 Solutions, published by Wiley
(no e-mails, please!)

DeDBlanK said:
In advance me.help = greatful AND thankful =P
I am having an issue with some code. I am trying to do a dcount on
records verifying that there are records before opening the report
(in
other words, handle it in the dialog and not the reports No Data
event)
This code works without the DCount (have commented in the code)
'****CODE START****
Private Sub cmdReport_Click()

'Remove the single quote from start of next line once you have it
working.
'On Error GoTo Err_Handler
'Purpose: Filter a report to a date range.
'Documentation: http://allenbrowne.com/casu-08.html
'Note: Filter uses "less than the next day" in case the field has a
time component.
Dim strReport As String
Dim strDateField As String
Dim varWhere As Variant
Dim lngView As Integer
Dim msg As String
Dim strRecordSource As String


Const strcJetDate = "\#mm\/dd\/yyyy\#"


'DO set the CASE values.
Select Case Me.Frame18.Value
Case 1
strReport = "rptDetailRepair" 'Put your report name in
these quotes.
Case 2
strReport = "rptSummaryQAD"


Case 3
strReport = "rptSummaryReport"


Case 4
strReport = "rptSummaryReportRepair"


Case 5
strReport = "rptSummaryDateRangeDowntime"


Case 6
strReport = "rptSQCDMonthly"


Case 7
strReport = "rptOEE"


Case Null
msg = "value is null"
MsgBox msg, vbInformation, "No Report"
GoTo Exit_Handler
Case Else
msg = "No report selected"
MsgBox msg, vbInformation, "No Report"
GoTo Exit_Handler
End Select


strDateField = "[dtmDate]" 'Put your field name in the square
brackets in these quotes.
lngView = acViewPreview 'Use acViewNormal to print instead of
preview.
varWhere = Null 'Set to null to check later if value
has been assigned


'Build the filter string.
If IsDate(Me.txtStartDate) Then
varWhere = "(" & strDateField & " >= " &
Format(Me.txtStartDate, strcJetDate) & ")"
End If


'I like to use the DateValue function to strip times from date/
time Fields
If IsDate(Me.txtEndDate) Then 'Check if start date had a value,
if so add AND and end date
If varWhere <> vbNullString Then
varWhere = (varWhere + " AND ") & "(DateValue(" &
strDateField & ")<= " & Format(Me.txtEndDate, strcJetDate) & ")"
Else ' just add end date
varWhere = "(DateValue(" & strDateField & ")<= " &
Format(Me.txtEndDate, strcJetDate) & ")"
End If
End If


'build filter string for Shift
varWhere = (varWhere + " AND ") & "([strShift]" &
fnMultiList(Me.lstShift) & ")"


'build filter string for Dept
If Me.lstDept.ItemsSelected.Count >= 1 Then
varWhere = (varWhere + " AND ") & "([fkLine]" &
fnMultiList(Me.lstDept) & ")"
End If


'Close the report if already open: otherwise it won't filter
properly.
If CurrentProject.AllReports(strReport).IsLoaded Then
DoCmd.Close acReport, strReport
End If


'******CODE ISSUE*****
'check for records in the report
DoCmd.OpenReport strReport, acViewDesign, , varWhere, acHidden


strRecordSource = Reports(strReport).recordsource


If DCount("*", strRecordSource, varWhere) > 0 Then
'Open the report. ****THIS SECTION WORKS****
Debug.Print DCount("*", strRecordSource, varWhere)
Debug.Print varWhere
DoCmd.OpenReport strReport, lngView, , varWhere
'*****END THIS SECTION WORKS*******
Else
'Close report with message
msg = "Sorry, Unable to find data that matches that criteria"
MsgBox msg, vbExclamation, "No Data"
DoCmd.Close acReport, strReport, acSaveNo
End If
'***END CODE ISSUE****
Exit_Handler:
Exit Sub


Err_Handler:
If Err.Number <> 2501 Then
MsgBox "Error " & Err.Number & ": " & Err.Description,
vbExclamation, "Cannot open report"
End If
Resume Exit_Handler


End Sub
'****CODE END****

guess I left a part out, the WHERE (if the DCOUNT is included in the
code) is being ignored when the report opens. However, the
Debug.Print varWhere is showing the criteria correctly. Don't know
why the DCount would cause this issue. Thanks, again for any help.
 
D

DeDBlanK

You're opening a new instance of the report, not making the one you opened
hidden visible.

Try replacing with the line indicated:

    '******CODE ISSUE*****
    'check for records in the report
    DoCmd.OpenReport strReport, acViewDesign, , varWhere, acHidden

    strRecordSource = Reports(strReport).recordsource

    If DCount("*", strRecordSource, varWhere) > 0 Then
        'Open the report.  ****THIS SECTION WORKS****
        Debug.Print DCount("*", strRecordSource, varWhere)
        Debug.Print varWhere
        Reports(strReport).Visible = True   ' <** CHANGE TO THIS
        '*****END THIS SECTION WORKS*******
    Else
        'Close report with message
        msg = "Sorry, Unable to find data that matches that criteria"
        MsgBox msg, vbExclamation, "No Data"
        DoCmd.Close acReport, strReport, acSaveNo
    End If
 '***END CODE ISSUE****

--
Doug Steele, Microsoft Access MVPhttp://www.AccessMVP.com/DJSteele
Co-author: Access 2010 Solutions, published by Wiley
(no e-mails, please!)




In advance me.help = greatful AND thankful =P
I am having an issue with some code.  I am trying to do a dcount on
records verifying that there are records before opening the report
(in
other words, handle it in the dialog and not the reports No Data
event)
This code works without the DCount (have commented in the code)
'****CODE START****
Private Sub cmdReport_Click()
'Remove the single quote from start of next line once you have it
working.
'On Error GoTo Err_Handler
'Purpose:       Filter a report to a date range.
'Documentation:http://allenbrowne.com/casu-08.html
'Note: Filter uses "less than the next day" in case the field has a
time component.
   Dim strReport As String
   Dim strDateField As String
   Dim varWhere As Variant
   Dim lngView As Integer
   Dim msg As String
   Dim strRecordSource As String
   Const strcJetDate = "\#mm\/dd\/yyyy\#"
   'DO set the CASE values.
   Select Case Me.Frame18.Value
   Case 1
       strReport = "rptDetailRepair"      'Put your report name in
these quotes.
   Case 2
       strReport = "rptSummaryQAD"
   Case 3
       strReport = "rptSummaryReport"
   Case 4
       strReport = "rptSummaryReportRepair"
   Case 5
       strReport = "rptSummaryDateRangeDowntime"
   Case 6
       strReport = "rptSQCDMonthly"
   Case 7
       strReport = "rptOEE"
   Case Null
       msg = "value is null"
       MsgBox msg, vbInformation, "No Report"
       GoTo Exit_Handler
   Case Else
       msg = "No report selected"
       MsgBox msg, vbInformation, "No Report"
       GoTo Exit_Handler
   End Select
   strDateField = "[dtmDate]"  'Put your field name in the square
brackets in these quotes.
   lngView = acViewPreview     'Use acViewNormal to print instead of
preview.
   varWhere = Null             'Set to null to check later if value
has been assigned
    'Build the filter string.
   If IsDate(Me.txtStartDate) Then
        varWhere = "(" & strDateField & " >= " &
Format(Me.txtStartDate, strcJetDate) & ")"
   End If
   'I like to use the DateValue function to strip times from date/
time Fields
   If IsDate(Me.txtEndDate) Then  'Check if start date had a value,
if so add AND and end date
       If varWhere <> vbNullString Then
           varWhere = (varWhere + " AND ") & "(DateValue(" &
strDateField & ")<= " & Format(Me.txtEndDate, strcJetDate) & ")"
       Else  ' just add end date
           varWhere = "(DateValue(" & strDateField & ")<= " &
Format(Me.txtEndDate, strcJetDate) & ")"
       End If
   End If
   'build filter string for Shift
   varWhere = (varWhere + " AND ") & "([strShift]" &
fnMultiList(Me.lstShift) & ")"
   'build filter string for Dept
   If Me.lstDept.ItemsSelected.Count >= 1 Then
       varWhere = (varWhere + " AND ") & "([fkLine]" &
fnMultiList(Me.lstDept) & ")"
   End If
   'Close the report if already open: otherwise it won't filter
properly.
   If CurrentProject.AllReports(strReport).IsLoaded Then
       DoCmd.Close acReport, strReport
   End If
   '******CODE ISSUE*****
   'check for records in the report
   DoCmd.OpenReport strReport, acViewDesign, , varWhere, acHidden
   strRecordSource = Reports(strReport).recordsource
   If DCount("*", strRecordSource, varWhere) > 0 Then
       'Open the report.  ****THIS SECTION WORKS****
       Debug.Print DCount("*", strRecordSource, varWhere)
       Debug.Print varWhere
       DoCmd.OpenReport strReport, lngView, , varWhere
       '*****END THIS SECTION WORKS*******
   Else
       'Close report with message
       msg = "Sorry, Unable to find data that matches that criteria"
       MsgBox msg, vbExclamation, "No Data"
       DoCmd.Close acReport, strReport, acSaveNo
   End If
'***END CODE ISSUE****
Exit_Handler:
   Exit Sub
Err_Handler:
   If Err.Number <> 2501 Then
       MsgBox "Error " & Err.Number & ": " & Err.Description,
vbExclamation, "Cannot open report"
   End If
   Resume Exit_Handler
End Sub
'****CODE END****
guess I left a part out, the WHERE (if the DCOUNT is included in the
code) is being ignored when the report opens.  However, the
Debug.Print varWhere is showing the criteria correctly.  Don't know
why the DCount would cause this issue.  Thanks, again for any help.- Hide quoted text -

- Show quoted text -

Thanks for your solution, however, it opened the report in design
view.
I actually, literally, just now figured this one.

'check for records in the report

DoCmd.OpenReport strReport, acViewDesign, , varWhere, acHidden

strRecordSource = Reports(strReport).recordsource


If DCount("*", strRecordSource, varWhere) > 0 Then
'Open the report.
Debug.Print DCount("*", strRecordSource, varWhere)
Debug.Print varWhere
'close report if already open: prevent mis-filter
'<added
If CurrentProject.AllReports(strReport).IsLoaded Then '<added
DoCmd.Close acReport, strReport
'<added
End
If
'<added
DoCmd.OpenReport strReport, lngView, , varWhere
Else
'Close report with message
msg = "Sorry, Unable to find data that matches that criteria"
MsgBox msg, vbExclamation, "No Data"
DoCmd.Close acReport, strReport, acSaveNo
End If


Issue with this solution is that it opens the report a couple time
before it's visible.
 
D

Douglas J. Steele

Why not just close the report after you've opened it in Design view to get
its recordsource?

Or better yet, use the report's NoData event, like it's meant for!

--
Doug Steele, Microsoft Access MVP
http://www.AccessMVP.com/DJSteele
Co-author: Access 2010 Solutions, published by Wiley
(no e-mails, please!)

You're opening a new instance of the report, not making the one you opened
hidden visible.

Try replacing with the line indicated:

'******CODE ISSUE*****
'check for records in the report
DoCmd.OpenReport strReport, acViewDesign, , varWhere, acHidden

strRecordSource = Reports(strReport).recordsource

If DCount("*", strRecordSource, varWhere) > 0 Then
'Open the report. ****THIS SECTION WORKS****
Debug.Print DCount("*", strRecordSource, varWhere)
Debug.Print varWhere
Reports(strReport).Visible = True ' <** CHANGE TO THIS
'*****END THIS SECTION WORKS*******
Else
'Close report with message
msg = "Sorry, Unable to find data that matches that criteria"
MsgBox msg, vbExclamation, "No Data"
DoCmd.Close acReport, strReport, acSaveNo
End If
'***END CODE ISSUE****

--
Doug Steele, Microsoft Access MVPhttp://www.AccessMVP.com/DJSteele
Co-author: Access 2010 Solutions, published by Wiley
(no e-mails, please!)




In advance me.help = greatful AND thankful =P
I am having an issue with some code. I am trying to do a dcount on
records verifying that there are records before opening the report
(in
other words, handle it in the dialog and not the reports No Data
event)
This code works without the DCount (have commented in the code)
'****CODE START****
Private Sub cmdReport_Click()
'Remove the single quote from start of next line once you have it
working.
'On Error GoTo Err_Handler
'Purpose: Filter a report to a date range.
'Documentation:http://allenbrowne.com/casu-08.html
'Note: Filter uses "less than the next day" in case the field has a
time component.
Dim strReport As String
Dim strDateField As String
Dim varWhere As Variant
Dim lngView As Integer
Dim msg As String
Dim strRecordSource As String
Const strcJetDate = "\#mm\/dd\/yyyy\#"
'DO set the CASE values.
Select Case Me.Frame18.Value
Case 1
strReport = "rptDetailRepair" 'Put your report name in
these quotes.
Case 2
strReport = "rptSummaryQAD"
Case 3
strReport = "rptSummaryReport"
Case 4
strReport = "rptSummaryReportRepair"
Case 5
strReport = "rptSummaryDateRangeDowntime"
Case 6
strReport = "rptSQCDMonthly"
Case 7
strReport = "rptOEE"
Case Null
msg = "value is null"
MsgBox msg, vbInformation, "No Report"
GoTo Exit_Handler
Case Else
msg = "No report selected"
MsgBox msg, vbInformation, "No Report"
GoTo Exit_Handler
End Select
strDateField = "[dtmDate]" 'Put your field name in the square
brackets in these quotes.
lngView = acViewPreview 'Use acViewNormal to print instead of
preview.
varWhere = Null 'Set to null to check later if value
has been assigned
'Build the filter string.
If IsDate(Me.txtStartDate) Then
varWhere = "(" & strDateField & " >= " &
Format(Me.txtStartDate, strcJetDate) & ")"
End If
'I like to use the DateValue function to strip times from date/
time Fields
If IsDate(Me.txtEndDate) Then 'Check if start date had a value,
if so add AND and end date
If varWhere <> vbNullString Then
varWhere = (varWhere + " AND ") & "(DateValue(" &
strDateField & ")<= " & Format(Me.txtEndDate, strcJetDate) & ")"
Else ' just add end date
varWhere = "(DateValue(" & strDateField & ")<= " &
Format(Me.txtEndDate, strcJetDate) & ")"
End If
End If
'build filter string for Shift
varWhere = (varWhere + " AND ") & "([strShift]" &
fnMultiList(Me.lstShift) & ")"
'build filter string for Dept
If Me.lstDept.ItemsSelected.Count >= 1 Then
varWhere = (varWhere + " AND ") & "([fkLine]" &
fnMultiList(Me.lstDept) & ")"
End If
'Close the report if already open: otherwise it won't filter
properly.
If CurrentProject.AllReports(strReport).IsLoaded Then
DoCmd.Close acReport, strReport
End If
'******CODE ISSUE*****
'check for records in the report
DoCmd.OpenReport strReport, acViewDesign, , varWhere, acHidden
strRecordSource = Reports(strReport).recordsource
If DCount("*", strRecordSource, varWhere) > 0 Then
'Open the report. ****THIS SECTION WORKS****
Debug.Print DCount("*", strRecordSource, varWhere)
Debug.Print varWhere
DoCmd.OpenReport strReport, lngView, , varWhere
'*****END THIS SECTION WORKS*******
Else
'Close report with message
msg = "Sorry, Unable to find data that matches that criteria"
MsgBox msg, vbExclamation, "No Data"
DoCmd.Close acReport, strReport, acSaveNo
End If
'***END CODE ISSUE****
Exit_Handler:
Exit Sub
Err_Handler:
If Err.Number <> 2501 Then
MsgBox "Error " & Err.Number & ": " & Err.Description,
vbExclamation, "Cannot open report"
End If
Resume Exit_Handler
End Sub
'****CODE END****
guess I left a part out, the WHERE (if the DCOUNT is included in the
code) is being ignored when the report opens. However, the
Debug.Print varWhere is showing the criteria correctly. Don't know
why the DCount would cause this issue. Thanks, again for any help.- Hide
quoted text -

- Show quoted text -

Thanks for your solution, however, it opened the report in design
view.
I actually, literally, just now figured this one.

'check for records in the report

DoCmd.OpenReport strReport, acViewDesign, , varWhere, acHidden

strRecordSource = Reports(strReport).recordsource


If DCount("*", strRecordSource, varWhere) > 0 Then
'Open the report.
Debug.Print DCount("*", strRecordSource, varWhere)
Debug.Print varWhere
'close report if already open: prevent mis-filter
'<added
If CurrentProject.AllReports(strReport).IsLoaded Then '<added
DoCmd.Close acReport, strReport
'<added
End
If
'<added
DoCmd.OpenReport strReport, lngView, , varWhere
Else
'Close report with message
msg = "Sorry, Unable to find data that matches that criteria"
MsgBox msg, vbExclamation, "No Data"
DoCmd.Close acReport, strReport, acSaveNo
End If


Issue with this solution is that it opens the report a couple time
before it's visible.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top