Solved

Different result when printing after previewing a report

Posted on 2003-11-22
12
300 Views
Last Modified: 2008-02-26
I have a report that I start from a cmd button in preview mode. If I then print the report, I may get different results from what the preview screen showed.

The report runs some vb code started from the detail on format and on print events. There are also sub-reports that do the same. The code that gets called from these events, sets the visible property to false for controls that are empty (code courtesy of Steveb and 1William).

Public Function ControlDisplay(rpt As Access.Report)
Dim ctl As Control
Dim stTest As String, stTest1 As String
    On Error Resume Next 'not all controls will have datasource (line for example)
    For Each ctl In rpt.Section(acDetail).Controls
        ctl.Visible = (Len(ctl.Value & vbNullString) > 0)
    Next
End Function

It seems that if, on a sub-report, there are 2 records and the first record has all controls populated but the 2nd record doesn't have all controls populated, that the all the controls/labels get printed anyway. It's wierd because the first time you preview then print, the empty controls print, but if you preview/print again, it's ok.

Any ideas?

ps, I did a work around, and have separate print and preview command buttons, which seems to be working ok.
0
Comment
Question by:avoorheis
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
12 Comments
 
LVL 23

Expert Comment

by:heer2351
ID: 9805697
You would only have to call the above code from the onFormat event of you report. No need to call it again from the onPrint event.

Could you post the code of the onFormat event so I can have a look?
0
 

Author Comment

by:avoorheis
ID: 9808491
It's the same code that's called from both the on format and on print.

At first, I just had the on format calling the code. And did a print preview, all looked ok. But then when trying to print (preview still displayed, using print icon in toolbar), it didn't print right. If printed a second time, it printed ok.

I tried it again with only using the on format event and if I use
DoCmd.OpenReport stDocName, acViewNormal (instead of acPreview) it prints ok, but it still does the same thing if I use acPreview (preview looks ok, but the first pass print is not ok, using the print icon in the toolbar).

Hope I'm being clear. This only happens when when the subform has 2 records and some of the fields on the second record are empty (so should not display). I'll try an example of the report printout:

this is how it should look

          SAMPLE GOOD REPORT
Date:            1/1/03
Order#:        12345
Mortgage #:  1                    (start of subreport)
Grantor:        John Doe
Date:            1/1/03
Assigned To:  B of A
Mortgage #:   2
Grantor:         Sam Smith
Date:             2/1/03

         SAMPLE WHEN FIELD PRINTS WITH NO DATA
Date:            1/1/03
Order#:        12345
Mortgage #:  1
Grantor:        John Doe
Date:            1/1/03
Assigned To:  B of A
Mortgage #:   2
Grantor:         Sam Smith
Date:             2/1/03
Assigned To:

The "Assigned To:" should not show up because it didn't have data.

0
 
LVL 23

Expert Comment

by:heer2351
ID: 9813402
To help you I will need to look at the code, could you please post this.
0
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 

Author Comment

by:avoorheis
ID: 9813479
The on format event for the main report detail and the on format for each sub report detail calls:

Public Function ControlDisplay(rpt As Access.Report)
Dim ctl As Control
    On Error Resume Next 'not all controls will have datasource (line for example)
    For Each ctl In rpt.Section(acDetail).Controls
        ctl.Visible = (Len(ctl.Value & vbNullString) > 0)
    Next
End Function
0
 
LVL 23

Expert Comment

by:heer2351
ID: 9813638
Hmm I am not able to reproduce your problem.

Let's check if the code is called when doing a print after doing a preview, place a breakpoint in the ControlDisplay function on the on error resume next line. Now preview the report you should stop in the code, hit F5 to continue running. Then click on print, are you again taken to the code?
0
 

Author Comment

by:avoorheis
ID: 9813811
I was doing that the other day and just did it again. When I print, it doesn't stop at the breakpoint. But, I put a beep right after the line where the break is. I hear the beep when I print (so it's like it goes throught the code) but it doesn't stop at the break.

When I do the preview, it stops 6 times (and beeps 6 x) at the break, but when I print there's only one beep (but doesn't stop). Also, I do the preview, I see it looks format correctly, then print and it's not formated correctly. If I print, right away, again, it's formated correctly. ????????????

(there are 4 subreports, so I expect more than 1 stop, but 6 doesn't make sense either
0
 
LVL 23

Expert Comment

by:heer2351
ID: 9813828
Hmmm very strange, would you mind sending a zipped version of your database to me so I can have a look?

heer2351 at hotmail.com
0
 
LVL 23

Expert Comment

by:heer2351
ID: 9820556
Alan I had a look at your database. The reason for the onPrint event not being fired is that you did not set the event handler, i.e. open the report in design view and have a look at the events tab you will see that the onPrint is empy. You probably copied the onFormat event and renamed it, access does not like that. If you cut and copy the already renamed event access will set the event handler. But like I said before it is not required.

I have not yet solved your problem, but I am thinking that the problem is due to the fact that the subreport is part of a footer instead of the detail section.
0
 

Author Comment

by:avoorheis
ID: 9820682
Do you think, as a general rule, it would be better to use the main reports, footer onformat event instead of the subreports, detail onformat event?

As far as the onPrint event, I was going back and forth with using/not using the onPrint event, so the code was there and I just disabled it by deleting the Event Procedure. Since onPrint event is not needed, does Access actually run through the code a second time if you print while previewing the document?
0
 
LVL 23

Accepted Solution

by:
heer2351 earned 250 total points
ID: 9820893
I think I have found the solution to your problem. Change in your onFormat event the line:

ControlDisplay Reports("reportName")
or ControlDisplay report_subreportName

with
  ControlDisplay Me

That solved the problem for me :)
0
 

Author Comment

by:avoorheis
ID: 9821291
Thanks!
Do you have any idea why?
I haven't used Me by itself before and couldn't find anything in the help files. Do you know of any documentation about it?

thanks again
0
 
LVL 23

Expert Comment

by:heer2351
ID: 9821345
My guess is that when then function is called for the second record on the subreport time access created a new instance of your report that has a different name as the first report instance. Because you only refer to this first report all records will have the same controls made visible as the first record.
By using Me you are sure that you are refering to the correct instance of the report.

From the help:

<quote>
The Me keyword behaves like an implicitly declared variable. It is automatically available to every procedure in a class module. When a class can have more than one instance, Me provides a way to refer to the specific instance of the class where the code is executing. Using Me is particularly useful for passing information about the currently executing instance of a class to a procedure in another module. For example, suppose you have the following procedure in a module:

Sub ChangeFormColor(FormName As Form)
      FormName.BackColor = RGB(Rnd * 256, Rnd * 256, Rnd * 256)
End Sub

You can call this procedure and pass the current instance of the Form class as an argument using the following statement:

ChangeFormColor Me
</quote>
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question