How to trigger the Format event for a report section

Collapse
This topic has been answered.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Petrol
    Contributor
    • Oct 2016
    • 285

    How to trigger the Format event for a report section

    Does anyone know the magic trick to make the Format event fire for a report section? I have a simple event procedure linked to the On Format property of the Detail section, but it doesn't work.
    learn.microsoft .com assures me "For report detail sections, the Format event occurs for each record in the section just before Microsoft Access formats the data in the record"; but setting a breakpoint confirms that in my case the event procedure never gets called. Is there something else I need to do?

    I have tried the usual - close/reboot/reopen, compact and repair, decompile and recompile, copy all controls to a brand new report - all to no avail.
    I will insert the procedure code below for completeness, but since it is never invoked I doubt that the content of the procedure has any bearing of the problem.

    Code:
    Private Sub Detail_Format(C ancel As Integer, FormatCount As Integer)
    '
    ' If the pilgrim withdrew during the event, add an extra line to say so.
    '
    Dim tpcm As Variant
    tpcm = 1440 / 2.54 ' twips per centimetre
    On Error GoTo ErrorProc
    
    If txt.Response = "W" Then
    lblWithdrawalNote.Caption = "NB: " & "[First_name]" & " attended initially, but withdrew during the event."
    lblWithdrawalNote.Visible = True
    Me.Detail.height = 1.8 * tpcm ' Set section height to 1.8cm for this record only
    Else
    lblWithdrawalNote.Visible = False
    Me.Detail.height = 1.2 * tpcm ' Set section height back to 1.2cm for other records
    End If
    
    ByeBye:
    Exit Sub
    
    ErrorProc:
    MsgBox "Error " & Err.Number & " - " & Err.Description , , "Error closing F52"
    Resume ByeBye
    End Sub
    Please ignore the ectopic spaces above; they were inserted by Post.Bytes - they are definitely not there in my original code.
    Last edited by NeoPa; Oct 20 '25, 06:39 AM.
  • Answer selected by NeoPa at Oct 23 '25, 01:32 PM.
    NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32656

    Well that's weird :-(

    I just picked up a random old database & set up some code behind the Detail Section's Format Event but it only triggered the code (fired the Event) when I ran it in Print Preview mode. Nothing when I simply opened it normally (Report View) or even in Layout View.

    Have you tried opening it in Print Preview mode?

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32656

      #2
      Hi Petrol.

      Is the OnFormat property set to "[Event Procedure]"? This can sometimes get lost if you're playing around with Copy/Pasting &/or recreating.

      (Re-)Pasting the actual code into the module will always set this for you.

      Comment

      • Petrol
        Contributor
        • Oct 2016
        • 285

        #3
        Yes, Ade, it is - both in the original and in the newly-created copy of the report.

        Comment

        • Petrol
          Contributor
          • Oct 2016
          • 285

          #4
          UPDATE: I created an entirely new report and set an event procedure into the On Format property. It also didn't work - the procedure was not invoked.
          Then I copied the example from the old Microsoft Learn site (https://learn.microsoft .com/en-us/previous-versions/office/aa211384(v=offi ce.11)) and pasted it into the On Format event procedure of a very basic new database containing only a table and a report - see attached screenshots.
          It still didn't work, and a breakpoint confirmed that the procedure was never entered. Here is the code:
          Code:
          Private Sub SalesDetail_Format(Cancel As Integer, _
          FormatCount As Integer)
          Const conSalesGoal = 1000
          If Me!GrandTotal > conSalesGoal Then
          Me!Message.Visible = True
          Else
          Me!Message.Visible = False
          End If
          End Sub
          To be honest, I thought I did see that it had worked, once, out of many attempts to run it, but I have been unable to reproduce it.

          Has anybody ever seen an Format event procedure work?
          Can anybody suggest a further line of enquiry?
          As stated in the OP above, I have tried the usual - close/reboot/reopen, compact and repair, decompile and recompile, copy all controls to a brand new report - all to no avail.
          Attached Files

          Comment

          • Petrol
            Contributor
            • Oct 2016
            • 285

            #5
            Hmm, that attachment doesn't seem to have worked too well. Let me try it as a PDF:
            Attached Files

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32656

              #6
              Well that's weird :-(

              I just picked up a random old database & set up some code behind the Detail Section's Format Event but it only triggered the code (fired the Event) when I ran it in Print Preview mode. Nothing when I simply opened it normally (Report View) or even in Layout View.

              Have you tried opening it in Print Preview mode?

              Comment

              • Petrol
                Contributor
                • Oct 2016
                • 285

                #7
                Hmm, that must have been what happened on the one occasion that I did get it to work. I had been playing around and must have tried Print Preview but didn't realise that that had made the difference.

                Yes, it does work in my application and in the test database, both in Print Preview and when I actually prnt it. Not quite what I want, because users usually look at reports in report view first, but at least we're getting somewhere.

                When I say "it does work", I should qualify that. The event fires and the procedure runs - that is, it makes lblWithdrawalNo te visible or invisible (see OP), but it doesn't change the height of the record's entry; but that must be another problem. In fact, I notice the Access hasn't even capitalised "height" in the VBA, so there must be something illegal abou that.

                Comment

                • NeoPa
                  Recognized Expert Moderator MVP
                  • Oct 2006
                  • 32656

                  #8
                  That seems likely ;-)
                  I'd need to see the code to know more.

                  Comment

                  • Petrol
                    Contributor
                    • Oct 2016
                    • 285

                    #9
                    Thanks, NeoPa. Actually, the code is in the Original Post, above.

                    Comment

                    • NeoPa
                      Recognized Expert Moderator MVP
                      • Oct 2006
                      • 32656

                      #10
                      I checked there, I saw/see no reference to height in that code Peter.

                      Comment

                      • Petrol
                        Contributor
                        • Oct 2016
                        • 285

                        #11
                        It's in the last line of each clause of the IF statement, Ade. But maybe it's illegally formatted, since VBA hasn't auto-capitaliseed the word "height".
                        But although what I've written appears to conform to Microsoft's syntax in https://learn.microsoft .com/en-us/office/vba/api/access.section. height,
                        I've just found something in an AI answer that suggests I should use Me.Section(acDe tail).Height instead of Me.Detail.Heigh t. So I'll try that next.

                        Comment

                        • Petrol
                          Contributor
                          • Oct 2016
                          • 285

                          #12
                          Nope. That didn't work either.

                          Comment

                          • NeoPa
                            Recognized Expert Moderator MVP
                            • Oct 2006
                            • 32656

                            #13
                            I'm getting old & daft Peter :-( :-D
                            It is there clearly.
                            As that's a separate issue from the original one that this thread is for, why not (/please do ;-)) post a new thread for it & we'll see what we can discover & resolve.

                            Comment

                            • Petrol
                              Contributor
                              • Oct 2016
                              • 285

                              #14
                              OK, point taken.
                              I presume I must just treat the original problem as an undocumented restriction of the use of the Format property, and program around it (i.e. tell everybody to use Prinnt Preview).

                              Comment

                              • NeoPa
                                Recognized Expert Moderator MVP
                                • Oct 2006
                                • 32656

                                #15
                                Yes indeed.
                                You're free to explore the thinking that might exist behind the choice to do it that way, but at the end of the day it is what we find it to be so we all just have to deal with it I suppose ;-)

                                Comment

                                Working...