• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 717
  • Last Modified:

Report_NoData run twice in Access

hi sir i  have this code for a button on form that will run a report for me ,
now every time
i press the button it will run Report_NoData sub rotuien massage  twice  and it will open an empty  report
the report code is
Private Sub Report_NoData(Cancel As Integer)
MsgBox "There is no data for this report", _
           vbInformation + vbOKOnly, "Information"
    Cancel = -1
End Sub
and the button code is

Private Sub btnStatusReport_Click()
On Error GoTo Err_btnStatusReport_Click

    Dim stDocName As String
    Dim stLinkCriteria As String
   
      'Open the report
    stDocName = "Status History Report"
    If Not IsNull(Me.TypeDetailsID) Then
    stLinkCriteria = "[TypeDetailsID]=" & Me![TypeDetailsID]

    DoCmd.OpenReport stDocName, acViewPreview, , stLinkCriteria
   Me.Refresh
    Else
    MsgBox "You Should Enter Type Name", vbCritical
    Me.CobType.SetFocus
       
    End If
 
Exit_btnStatusReport_Click:
    Exit Sub

Err_btnStatusReport_Click:
    MsgBox Err.Description
    Resume Exit_btnStatusReport_Click
   
End Sub
 how can prevent the report from preview  and prevent the massage to popup twice i need it only one

 
0
osama120
Asked:
osama120
  • 10
  • 6
  • 5
  • +1
1 Solution
 
Rey Obrero (Capricorn1)Commented:


try this


Private Sub Report_NoData(Cancel As Integer)

Stop     '<<<< Add this for testing

MsgBox "There is no data for this report", _
           vbInformation + vbOKOnly, "Information"
    Cancel = -1
End Sub

the code will stop in the Stop when there is nodata for the report.

to continue running the codes step by step  press F8 until you find where the second message is coming from



0
 
osama120Author Commented:
it give me  run time error 2501
the open report was cancelled then it will mark
DoCmd.OpenReport stDocName, acViewPreview, , stLinkCriteria
0
 
osama120Author Commented:
the open report action  was cancelled then it will mark
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
mpmccarthyCommented:
Replace acViewPreview with acViewNormal.  This will send the report directly to the printer without previewing it.

DoCmd.OpenReport stDocName, acViewNormal, , stLinkCriteria
0
 
Rey Obrero (Capricorn1)Commented:


Private Sub Report_NoData(Cancel As Integer)

MsgBox "There is no data for this report", _
           vbInformation + vbOKOnly, "Information"
    Cancel = -1

Stop     '<<<< Add this for testing   move here

End Sub
0
 
osama120Author Commented:
the will make loop againand again and it still mark the line
DoCmd.OpenReport stDocName, acViewNormal, , stLinkCriteria
0
 
mpmccarthyCommented:
Remove the Refresh from below the open report command
0
 
mpmccarthyCommented:
Have you by any chance got a subReport on the report?
0
 
osama120Author Commented:
no
0
 
Rey Obrero (Capricorn1)Commented:
do a Debug>Compile

fix any error that may arise

then do a Compact and Repair of the database
0
 
osama120Author Commented:
the error still there and it break the code
0
 
osama120Author Commented:
if i remove the code
Private Sub Report_NoData(Cancel As Integer)

MsgBox "There is no data for this report", _
           vbInformation + vbOKOnly, "Information"
    Cancel = -1
End Sub
 it will show an empty preview report
0
 
mpmccarthyCommented:
Did removing the refresh make any difference?
0
 
mpmccarthyCommented:
Is there code in the On format event of the report or any code behind the report at all?
0
 
osama120Author Commented:
no it make no difference
0
 
Rey Obrero (Capricorn1)Commented:


try this

Private Sub btnStatusReport_Click()
'On Error GoTo Err_btnStatusReport_Click
On error resume next

    Dim stDocName As String
    Dim stLinkCriteria As String
   
      'Open the report
    stDocName = "Status History Report"
    If Not IsNull(Me.TypeDetailsID) Then
    stLinkCriteria = "[TypeDetailsID]=" & Me![TypeDetailsID]

    DoCmd.OpenReport stDocName, acViewPreview, , stLinkCriteria
   Me.Refresh
    Else
    MsgBox "You Should Enter Type Name", vbCritical
    Me.CobType.SetFocus
       
    End If
 
'Exit_btnStatusReport_Click:
'    Exit Sub

'Err_btnStatusReport_Click:
'    MsgBox Err.Description
'    Resume Exit_btnStatusReport_Click
 

    If Err = 2501 Then Err.Clear
   
End Sub

   place the nodata code back in the report open event

0
 
osama120Author Commented:
still the same
0
 
Rey Obrero (Capricorn1)Commented:
did you do this?

do a Debug>Compile

fix any error that may arise

then do a Compact and Repair of the database
0
 
osama120Author Commented:
if i pree the button it will give me
Run Time Error:'2501'
the OpenReport Action was cancelled
then i press Debug then
it will givea yellow mark on the line
 DoCmd.OpenReport stDocName, acViewPreview, , stLinkCriteria
then i have to reset  
0
 
osama120Author Commented:
by the way if i move the mouse  on acViewPreview it will show number 2
0
 
rockiroadsCommented:
Because the cancel flag is set to True, it returns error number 2501, therefore because u use error handlers, uneed to cater for it

one way is modifying Err_btnStatusReport_CLick

e.g.


Err_btnStatusReport_Click:
    If err.Number <> 2501 then MsgBox Err.Description
    Resume Exit_btnStatusReport_Click
0
 
Rey Obrero (Capricorn1)Commented:

so you don't get two messages

Exit_btnStatusReport_Click:
    Exit Sub


Err_btnStatusReport_Click:
     '    MsgBox Err.Description
     Resume Exit_btnStatusReport_Click
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 10
  • 6
  • 5
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now