1

I am building a report for an MS Access database. The report contains a subform that lists information about items, including an attachment field that holds images.

If there is no attachment, the row space is still used by the object, resulting in a lot of unnecessary empty space in the report.

I need to resize the object to 0x0 and also resize the Details section of the subreport if there is no image.

  1. I have found that I could use the following command to determine if there is an image present but do not know which Event to put the statement in or what additional syntax will be required:

    Me.attachPhoto.AttachmentCount
    
  2. I found the following example code for resizing the attachment/image object that I think I could modify to get working, but do not know which Event to place it in:

    Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
        If IsNull(Me!Image20) Then
            Me!Image20.Visible = False
            Me!Image20.Height = 0
            Me!Image20.Width = 0
        ElseIf Not IsNull(Me!Image20) Then
            Me!Image20.Visible = True
            Me!Image20.Height = 2880
            Me!Image20.Width = 2880
        End If
    End Sub 
    
  3. Assuming I can figure out where to place the code and can get it working, I still don't know how to change the size of the Details section of the subreport.

June7
  • 19,874
  • 8
  • 24
  • 34
jsatwm
  • 11
  • 1
  • That code is in the Detail section Format event as shown by `Detail_Format`. Format events trigger only for PrintPreview or direct to printer. I think changing the Detail section height would go in the Report Format event. Use twips, 1440 twips = 1 inch: `Me.Detail.Height = 1440` – June7 Apr 29 '19 at 22:57
  • But maybe just need to set the Detail section CanGrow and CanShrink properties to Yes. – June7 Apr 29 '19 at 23:06
  • I tried dropping the resizing code into the Report Format event (after correcting the object names). That broke the report with the error: "A problem occurred while [database name] was communicating with the OLE server or ActiveX Control. This error occurs when an event has failed to run because the location of the logic for the event cannot be evaluated. For example, if the OnOpen property of a form is set to =[Field], this error occurs because a macro or event name is expected to run when the event occurs." – jsatwm Apr 30 '19 at 16:32
  • Also, I tried setting the CanGrow/CanShrink properties in the Detail section. It didn't seem to change the behavior at all. – jsatwm Apr 30 '19 at 16:33
  • Sorry, Report does not seem to have Format event. I had used Report Header Format event for testing. Does your subreport retrieve only 1 record? – June7 Apr 30 '19 at 17:10
  • I just tested code in Detail Format to set its own height and that did work after all. However, don't really see purpose if there are multiple records. – June7 Apr 30 '19 at 17:18
  • There are multiple records in the (Site Visit Tasks) SubReport for each record in the (Site Visits) Report. I do expect to only report on one Site Visit at a time, but it would contain multiple task records. – jsatwm Apr 30 '19 at 18:33
  • This is what I'm trying to achieve: https://i.imgur.com/vqQInZF.png – jsatwm Apr 30 '19 at 19:03
  • Here's a mock-up of the final report: https://i.imgur.com/7kxOktG.png – jsatwm Apr 30 '19 at 19:04
  • Easiest approach may be to go the other way. Design the Image control with a minimal height (such as 0.25") and therefore the Detail section can be minimal. Then resize Image control with the Detail Format event only when there is an image. The Detail section will grow to fit. – June7 Apr 30 '19 at 19:19
  • Interesting idea! I will see what I can do with it. – jsatwm Apr 30 '19 at 19:36
  • I did some testing. Not finding AttachmentCount as a property or method. I used Not IsNull(). What I found is that once the control is upsized, all subsequent instances of the control are upsized, won't reduce. – June7 Apr 30 '19 at 21:52
  • I know you wanted to save paper but just might not be possible. Just might have to use a "No Image Available" image. – June7 Apr 30 '19 at 23:16

1 Answers1

0

A few things:

First up, you don’t need code to do this.

The problem of course is that picture control does not have a built can grow, and can shrink setting.

However, most controls do, and especially a sub-report.

In fact, I recommend the following trick not only for pictures, but often you have a “group” of controls to shrink if they don’t have values. The problem is while “one” box can be collapsed very easy by the report rendering system, several controls on a line often can’t, because with multiple controls, then Access can’t figure out how to move up multiple controls.

Worse, picture controls can’t shrink. But text boxes, AND ALSO sub-reports do shrink rather nice.

Given the above?

Move the offering "items" or group or picture into a sub-report.

Try this approach: It takes no code. It is an easy and FAST drag + drop approach.

Simply create blank new report. Base the report on the same table as existing report.

Launch the query builder on this report – yes to create a query.

Then in the query builder do this:

enter image description here

Now, this sub report will ONLY return records if there is a picture.

Cut the picture control from your main report (in design mode). Paste it into this report. Now you have a report with JUST the picture control. And ONLY records with a picture will appear (because of above condition).

Now, save this sub report – CLOSE it.

Now, with your main report in design mode, simply drag + drop in the sub report from the nav pane.

The result will should look like this:

enter image description here

Access should (will) set the master link child as the PK by default (this is fine). (just in case - do check the link master child settings)

Make sure the sub report on main is can grow/can shrink. Size it to ONE line like above.

So a few simple mouse clicks, and some drag + drop, and you off to the races.

Edit

I also missed that on the main report - you want the keep together for the main detail section set = no. You also need to use print preview - this does not seem to work with report view.

Community
  • 1
  • 1
Albert D. Kallal
  • 42,205
  • 3
  • 34
  • 51
  • Albert -- I'm already using a subreport, but I think I see what you're getting at. Will give it a try. – jsatwm May 01 '19 at 14:07
  • Albert - I followed your instructions, but cannot get the picture subreport to shrink if the picture is empty. – jsatwm May 01 '19 at 15:12
  • Set detail section of main report can shrink = yes. Set sub from control as can shrink = yes. I did miss that you want the keep together on the main report = no - that is likely what you are missing. I had added this edit comment to the end of my post. – Albert D. Kallal May 02 '19 at 00:22
  • You also have to use print preview - for some reason report view this trick does not work. – Albert D. Kallal May 02 '19 at 00:28