Results 1 to 6 of 6
  1. #1
    Oxygen Potassium is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2016
    Posts
    40

    Bypassing the On Close Event in a Report

    I have a report that has the following code built into the On Close event:

    Private Sub Report_Close()
    DoCmd.OpenForm "Navigation", acNormal
    End Sub

    I also have the following code built into the On No Data event:

    Private Sub Report_NoData(Cancel As Integer)
    Call Report_Load
    MsgBox "No Report Available", vbOKOnly, "No Report"
    Cancel = True
    End Sub

    The problem I'm having is, if the report has no data I want to open a form labeled "Find Records" instead of the "Navigation" form referenced in the On Close event. Is there a way to bypass the On Close Event if the report has no data?

  2. #2
    Micron is offline Very Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,372
    No you can't. But you can evaluate whether or not there was no data and open the required form from the close event. Question is what to do with the code you currently have to open the nav form?
    Edit:
    Call Report_Load??
    why are you calling this if there's no data in the report?
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    Oxygen Potassium is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2016
    Posts
    40
    I think I can make it work using from the close event. Do you have any suggestions for how to evaluate whether there is no data in the on close event? Would a DCount work?

  4. #4
    Micron is offline Very Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,372
    First, let's establish that you should have these two lines at the top of every module (the first has 3 possible settings, so your Access options may be set to provide you with a different option parameter). The second is a vb editor option that you REALLY should have turned on if you do not (Require Variable Declaration). What follows these two lines (sans comment) is a module level variable. Its scope is any code behind the report (or form if in a form module). Some caution is required when using them since their value is not only accessible throughout the module code, it is also possible to accidentally affect its value in this region. In your case, there should be little to be concerned about - this is just a little treatise on the scope of form/report module level variables.

    Option Compare Database
    Option Explicit

    then have the next line
    Dim bolNoData As Boolean

    Then modify your NoData to set the variable to True IF this event runs.
    Code:
    Private Sub Report_NoData(Cancel As Integer)
    
    Call Report_Load
    MsgBox "No Report Available", vbOKOnly, "No Report"
    Cancel = True
    bolNoData = True
    
    End Sub
    Code:
    Private Sub Report_Close()
    
    If bolNoData = False Then
      DoCmd.OpenForm "Navigation", acNormal
    Else
      DoCmd.OpenForm "Find Records", acNormal
    End If
    
    End Sub
    You did not address why the load event is being called. This makes no sense to me, so I cannot predict the effectiveness of the proposed solution since I have no idea what to expect from making this call.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  5. #5
    Oxygen Potassium is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2016
    Posts
    40
    Thank you Micron. That worked perfectly for what I was trying to achieve.

    The on load event was just closing a previously open form so that shouldn't affect any of this.

  6. #6
    Micron is offline Very Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,372
    I wouldn't do it that way, mainly because it's probable that the form could already be closed by the user, especially if you're displaying form tabs. That will generate an error.
    The close method should probably be added to the end of the event that opens the report. However, I'm used to having the form I navigated to the report from become the active window again when there were no records. Chances are that a criteria needs to be modified and the report attempted again.
    Whichever route you take, good luck with your db development.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 2
    Last Post: 03-16-2017, 08:26 AM
  2. Replies: 3
    Last Post: 09-28-2016, 07:36 AM
  3. Replies: 2
    Last Post: 07-05-2015, 09:10 AM
  4. close Form from within a Form.current event
    By alextijuca in forum Access
    Replies: 7
    Last Post: 03-19-2015, 11:31 AM
  5. Replies: 2
    Last Post: 06-20-2011, 03:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums