Link to home
Start Free TrialLog in
Avatar of Scott Lamond
Scott LamondFlag for United States of America

asked on

How To Display a Conditional Image in a Microsoft Access Report

In an Access report (Certificates of Analysis that display chemical properties to accompany shipments), we want to display an image (a third-party accreditation stamp) in the footer of the report only when the product number ([PNUM]) is "032401". -rptCOAprint is executed by the user, it has an Event Procedure (On Open) subroutine that runs qryCOAprint-TABLE which asks the user for the Bill of Lading number, creates a table of the data and then runs rptCOAprint2. rptCOAprint2 contains all of the report formatting with an Event Procedure (On Current) subroutine that should provide the image link info (to the only record in tblImages) and toggle the Visible setting to True if the field [PNUM] (stored in Text156)= "032401".

It prints without any error message but also without any image (the only product on the select shipment is "032401").

I've included screenshots of both the MyImage and Text156 properties.
And also text files containing the subroutines.

Once this is solved, we'd also like to learn how to print alternate stamps depending on the product number. I'd assume simply adding more IF THEN clauses to the second subroutine, but I don't understand how to define which image record from tblImages.
COAprint2-Subroutine.txt
Text156-Properties.jpg
MyImage-Properties.jpg
COAprint-Subroutine.txt
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

instead of setting the picture in the open event of the report,
try using the On Print or Format event of the section where the image control is located.
Avatar of Scott Lamond

ASKER

Rey Obrero,
Perhaps I misunderstood or am lacking in knowledge, but I right-clicked the Page Footer, selected Properties, On Print,  Code Builder and pasted the code (disabled in the report's On Current).
It printed with the entire page footer missing. I will try again by finding a Format event in that page footer section where the image box is located.
I got the same result using the On Format event, so I must be doing something wrong.
can you upload a copy of your db?
I can't upload real data. I'll try to fabricate a test.mdb.
You can use technique described in my article Images on continuous forms. It can be used in reports too. In this case you will not need code at all and can place different images, depending on reference to image file stored in some table.
Here is some sample code used in a section's Format event procedure (this code sets an image to visible or invisible depending on a condition):

Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)

   If Me![Sales] >= 10000 Then
      Me![txtSales].FontBold = True
      Me![txtSales].FontName = "Arial Black"
      Me![txtSales].FontSize = 12
      Me![txtSales].ForeColor = vbRed
      Me![txtSales].BackColor = vbYellow
      Me![imgGoldStar].Visible = True
   Else
      Me![txtSales].FontBold = False
      Me![txtSales].FontName = "Arial"
      Me![txtSales].FontSize = 9
      Me![txtSales].ForeColor = vbBlack
      Me![txtSales].BackColor = vbWhite
      Me![imgGoldStar].Visible = False
   End If
      
End Sub

Open in new window

als315,
I don't understand how to toggle the Visible option On and Off without using code.
Maybe you didn't read my whole question?
Or maybe I'm missing something in your article?
S.
Rey Obrero,
I created test.mdb and attached both it and the JPG.
rptCOAprint2-TEST is the main report.
The subreport, query and table should be easily reverse engineered.
The image in the lower right corner of the page footer is where we want that JPG to print but only when the field [PNUM] = "032401".
And we'd like to be able to expand this to print different images depending on the [PNUM] field.
test.mdb
NSF_Bleach.jpg
I tried your test database -- it had a lot 32-bit declarations, so I had to use the PtrSafe keyword to get around that.  There was also some stuff with an undeclared info variable in the report's Open procedure, which I commented out for the time being.   I am not sure what is intended by binding the Image to the ImagePath field (which was blank in any case), so I just selected the image you provided as the Picture property of the Image control, and with some syntax tweaking, and changing a few PNUM values in the table so I could test whether the image appeared or not, based on the PNUM value, the code on the Format event procedure worked.  Here is the modified code, and I am attaching the modified database, but it may not work in a 32-bit environment.

Private Sub PageFooter2_Format(cancel As Integer, FormatCount As Integer)
    
    'DoCmd.SetWarnings False
    
    If Me![PNUM] = "032401" Then
      Me![MyImage].Visible = True
    Else
       Me![MyImage].Visible = False
    End If
    
    'DoCmd.SetWarnings True
    'cancel = True

End Sub

Open in new window


You could place as many Image controls as needed in the footer (give them meaningful names), and set up a Select Case statement to make the appropriate one visible and the others invisible, depending on the PNUM value.
Here is the modified database.
HBF-Modified-test.mdb
Look at sample (I've removed missing references, table with image path was modified and PNUM field was added to report for debugging). Image should be in the same folder as DB
test.mdb
Is it your intention to load one or another image into the Image control, depending on the value of PNUM?  I think this can only be done with an Unbound Object control -- I will see what I can do with that approach.
I tried loading images from files into an unbound object frame control, but though the code ran without error, the images never displayed.  The fact that Microsoft Help on this topic only goes up to Access 97 or thereabouts might indicate that this technique is no longer supported.  See v. 1 of the modified report for this approach.  I then made another version of the report (v. 2) using the alternate technique of placing several image controls on the report, and making one or another visible depending on the value of PNUM, and that worked.  Here is the modified database.

This is the code that worked:

Private Sub PageFooter2_Format(Cancel As Integer, _
   FormatCount As Integer)
'Created by Helen Feddema 7-Jul-2016
'Last modified by Helen Feddema 7-Jul-2016

On Error GoTo ErrorHandler

   Dim strPNUM As String
   
   strPNUM = Nz(Me![PNUM])
   
   Select Case strPNUM
   
      Case "032401"
         Me![img032401].Visible = True
         Me![img032402].Visible = False
         Me![img032403].Visible = False
         Me![img032404].Visible = False
         
      Case "032402"
         Me![img032401].Visible = False
         Me![img032402].Visible = True
         Me![img032403].Visible = False
         Me![img032404].Visible = False
      
      Case "032403"
         Me![img032401].Visible = False
         Me![img032402].Visible = False
         Me![img032403].Visible = True
         Me![img032404].Visible = False
      
      Case "032404"
         Me![img032401].Visible = False
         Me![img032402].Visible = False
         Me![img032403].Visible = False
         Me![img032404].Visible = True
      
      Case Else
         Me![img032401].Visible = False
         Me![img032402].Visible = False
         Me![img032403].Visible = False
         Me![img032404].Visible = False
      
   End Select
   
ErrorHandlerExit:
   Exit Sub

ErrorHandler:
   MsgBox "Error No: " & Err.Number _
      & " in " & Me.Name & " Format procedure; " _
      & "Description: " & Err.Description
   Resume ErrorHandlerExit

End Sub

Open in new window


If the images could be named with the PNUM as part (or all) of their names, you could make the code more compact by making the matching image visible and the others invisible.
test-v2.zip
Helen! You can test my sample. Add PNUM and name of image to table tblImages and image will be displayed according to PNUM. No code needed
It did not work that way on my computer -- maybe because of the 64-bit issue (other code and controls I have used either don't work at all in a 64-bit environment, or need revision to work).  Or maybe because of my Windows/Office versions.  I am running 64-bit Windows 10 and 64-bit Office 2010 on my main computer.

Anyway, you now have an alternate method that you could use if the other one fails at some point.
ASKER CERTIFIED SOLUTION
Avatar of als315
als315
Flag of Russian Federation image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Helen,
It's printing all four images, overlapped (see the attached images to see how I placed and formatted the embedded images). I'm testing using product number "032401".

Also I will post the code here , modified to handle our actual product numbers. I assume there is something in the code changes that is causing it to fail, although you did mention something about the 32 v. 64-bit issue.

A more telling issue is when I attempt to print the report I get this error message:
"A custom macro in this report has failed to run, and is preventing the report from rendering."

The code (in the Page Footer's On Format section):
************************************************************************************
Private Sub PageFooter2_Format(Cancel As Integer, _
   FormatCount As Integer)
'Created by Helen Feddema 7-Jul-2016
'Last modified by Helen Feddema 7-Jul-2016

On Error GoTo ErrorHandler

   Dim strPNUM As String
   Dim strControlName As String
   Dim ctl As Access.Control
   
   strPNUM = Nz(Me![PNUM])
   
   Select Case strPNUM
   
      Case "032401"
         Me![img032401].Visible = True
         Me![Img032412].Visible = False
         Me![img026009].Visible = False
         Me![img026001].Visible = False
         
      Case "032413"
         Me![img032401].Visible = True
         Me![Img032412].Visible = False
         Me![img026009].Visible = False
         Me![img026001].Visible = False
         
      Case "032412"
         Me![img032401].Visible = False
         Me![Img032412].Visible = True
         Me![img026009].Visible = False
         Me![img026001].Visible = False
                 
      Case "026009"
         Me![img032401].Visible = False
         Me![Img032412].Visible = False
         Me![img026009].Visible = True
         Me![img026001].Visible = False
       
      Case "026011"
         Me![img032401].Visible = False
         Me![Img032412].Visible = False
         Me![img026009].Visible = True
         Me![img026001].Visible = False
       
      Case "026108"
         Me![img032401].Visible = False
         Me![Img032412].Visible = False
         Me![img026009].Visible = True
         Me![img026001].Visible = False
       
      Case "026110"
         Me![img032401].Visible = False
         Me![Img032412].Visible = False
         Me![img026009].Visible = True
         Me![img026001].Visible = False
       
      Case "026001"
         Me![img032401].Visible = False
         Me![Img032412].Visible = False
         Me![img026009].Visible = False
         Me![img026001].Visible = True
       
      Case "026028"
         Me![img032401].Visible = False
         Me![Img032412].Visible = False
         Me![img026009].Visible = False
         Me![img026001].Visible = True
     
      Case "026101"
         Me![img032401].Visible = False
         Me![Img032412].Visible = False
         Me![img026009].Visible = False
         Me![img026001].Visible = True
     
      Case "026103"
         Me![img032401].Visible = False
         Me![Img032412].Visible = False
         Me![img026009].Visible = False
         Me![img026001].Visible = True
           
      Case Else
         Me![img032401].Visible = False
         Me![Img032412].Visible = False
         Me![img026009].Visible = False
         Me![img026001].Visible = False
         
      End Select
   
ErrorHandlerExit:
   Exit Sub

ErrorHandler:
   MsgBox "Error No: " & Err.Number _
      & " in " & Me.Name & " Format procedure; " _
      & "Description: " & Err.Description
   Resume ErrorHandlerExit

End Sub


End Sub

*************************************************************************************
PageFooter.jpg
img032401.jpg
I have a VM with 32-bit Windows XP and Office 2003, so I will try testing the code there as well as in my 64-bit Windows 10/Office 2010 environment.
Since Helen is likely preoccupied, I've shifted my attention to the mdb posted by als15.

I recreated the object MyImage in the version of the report that resides in the live database and also imported the tblImages. When I run the report, the system asks for the field input for [ImagePath] so I've obviously failed to define something. It's likely related to the fact that I don't understand how the system finds any connection between the PNUM field and the MyImage object.
I fixed my problem by noticing that I had failed to add the tblImages to the qryCOAprintSum that provides data to the report. But the report is still not printing the image. I suspect that the new issue is that [CurrentProject].[Path] is providing the wrong file path (although I would have expected an error message).
Congrats to als15.

After changing [CurrentPath].[Path] to \\server\database\ it's now working.
It also prints a different image depending on the field [PNUM] in tblImages.

Thanks for your help.
S.
Most simple solution and it works.