By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,306 Members | 1,657 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,306 IT Pros & Developers. It's quick & easy.

Display Pictures in Access Report through VBA code

P: 7
I have a form that is coded to open a File Dialog boc to pick a picture to display on that particular item on a subform. My form is not based on query, but rather two separate tables (one primary, one sub). That code is working properly. How do I get that to translate to my report? I use VBA code because I use formats other than BMP for the pictures. Would basing the form on the qury that the report is based on solve the issue without further coding? Below is the code from the form.
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3. Dim path As String
  4.  
  5. Private Sub AddPicture_Click()
  6.     ' Use the Office File Open dialog to get a file name to use
  7.     ' as an employee picture.
  8.     getFileName
  9. End Sub
  10.  
  11. Private Sub Form_RecordExit(Cancel As Integer)
  12.     ' Hide the errormsg label to reduce flashing when navigating
  13.     ' between records.
  14.     errormsg.Visible = False
  15. End Sub
  16.  
  17. Private Sub RemovePicture_Click()
  18.     ' Clear the file name for the employee record and display the
  19.     ' errormsg label.
  20.     Me![ImagePath] = ""
  21.     hideImageFrame
  22.     errormsg.Visible = True
  23. End Sub
  24. Private Sub ImagePath_AfterUpdate()
  25.     ' After selecting an image for the employee, display it.
  26.     On Error Resume Next
  27.         'showErrorMessage
  28.         showImageFrame
  29.         If (IsRelative(Me!ImagePath) = True) Then
  30.             Me![ImageFrame].Picture = path & Me![ImagePath]
  31.         Else
  32.             Me![ImageFrame].Picture = Me![ImagePath]
  33.         End If
  34. End Sub
  35. Private Sub Form_Current()
  36.     ' Display the picture for the current employee record if the image
  37.     ' exists.  If the file name no longer exists or the file name was blank
  38.     ' for the current employee, set the errormsg label caption to the
  39.     ' appropriate message.
  40.     Dim res As Boolean
  41.     Dim fName As String
  42.  
  43.     path = CurrentProject.path
  44.     On Error Resume Next
  45.         errormsg.Visible = False
  46.         If Not IsNull(Me!Photo) Then
  47.             res = IsRelative(Me!Photo)
  48.             fName = Me![ImagePath]
  49.             If (res = True) Then
  50.                 fName = path & "\" & fName
  51.             End If
  52.  
  53.             Me![ImageFrame].Picture = fName
  54.             showImageFrame
  55.             Me.PaintPalette = Me![ImageFrame].ObjectPalette
  56.             If (Me![ImageFrame].Picture <> fName) Then
  57.                 hideImageFrame
  58.                 errormsg.Caption = "Picture not found"
  59.                 errormsg.Visible = True
  60.             End If
  61.         Else
  62.             hideImageFrame
  63.             errormsg.Caption = "Click Add/Change to add picture"
  64.             errormsg.Visible = True
  65.         End If
  66.  
  67. End Sub
  68.  
  69. Sub getFileName()
  70.     ' Displays the Office File Open dialog to choose a file name
  71.     ' for the current employee record.  If the user selects a file
  72.     ' display it in the image control.
  73.     Dim fileName As String
  74.     Dim result As Integer
  75.     Dim fd As FileDialog
  76.     Set fd = Application.FileDialog(msoFileDialogFilePicker)
  77.     With fd
  78.         .Title = "Select Screenshot"
  79.         .Filters.Add "All Files", "*.*"
  80.         .Filters.Add "JPEGs", "*.jpg"
  81.         .Filters.Add "Bitmaps", "*.bmp"
  82.         .Filters.Add "Portable Network Graphics", "*.png"
  83.         .FilterIndex = 4
  84.         .AllowMultiSelect = False
  85.         .InitialFileName = CurrentProject.path
  86.         result = .Show
  87.         If (result <> 0) Then
  88.             fileName = Trim(.SelectedItems.Item(1))
  89.             Me![ImagePath].Visible = True
  90.             Me![ImagePath].SetFocus
  91.             Me![ImagePath].Text = fileName
  92.             Me![Issue Reference].SetFocus
  93.             Me![ImagePath].Visible = False
  94.         End If
  95.     End With
  96. End Sub
  97.  
  98. 'Sub showErrorMessage()
  99.     ' Display the errormsg label if the image file is not available.
  100.     'If Not IsNull(Me!Photo) Then
  101.      '   errormsg.Visible = False
  102.     'Else
  103.      '   errormsg.Visible = True
  104.     'End If
  105. 'End Sub
  106.  
  107. Function IsRelative(fName As String) As Boolean
  108.     ' Return false if the file name contains a drive or UNC path
  109.     IsRelative = (InStr(1, fName, ":") = 0) And (InStr(1, fName, "\\") = 0)
  110. End Function
  111.  
  112. Sub hideImageFrame()
  113.     ' Hide the image control
  114.     Me![ImageFrame].Visible = False
  115. End Sub
  116.  
  117. Sub showImageFrame()
  118.     ' Display the image control
  119.     Me![ImageFrame].Visible = True
  120. End Sub
  121.  
  122. Private Sub CloseForm_Click()
  123. On Error GoTo Err_CloseForm_Click
  124.  
  125.  
  126.     DoCmd.Close
  127.  
  128. Exit_CloseForm_Click:
  129.     Exit Sub
  130.  
  131. Err_CloseForm_Click:
  132.     MsgBox Err.Description
  133.     Resume Exit_CloseForm_Click
  134.  
  135. End Sub
  136. Private Sub Rpt_Click()
  137. On Error GoTo Err_Rpt_Click
  138.  
  139.     Dim stDocName As String
  140.  
  141.     stDocName = "Print Test Report"
  142.     DoCmd.OpenReport stDocName, acPreview
  143.  
  144. Exit_Rpt_Click:
  145.     Exit Sub
  146.  
  147. Err_Rpt_Click:
  148.     MsgBox Err.Description
  149.     Resume Exit_Rpt_Click
  150.  
  151. End Sub
Dec 5 '07 #1
Share this Question
Share on Google+
2 Replies


Denburt
Expert 100+
P: 1,356
What kind of control is Me![ImagePath] and is it bound to a field in the table? If it is bound to a field in the table you should be able to include that in your query that is behind the report then bind it to your control in the report.

What kind of control are you trying to bind to in the report and what have you tried so far in that respect?
Dec 5 '07 #2

P: 7
What kind of control is Me![ImagePath] and is it bound to a field in the table? If it is bound to a field in the table you should be able to include that in your query that is behind the report then bind it to your control in the report.

What kind of control are you trying to bind to in the report and what have you tried so far in that respect?
I am not binding it because as I understand it bound can only support .bmp files and I'm using multiple formats.

The file name is being stored in the Steps table which is part of the query on which the report is based. I just want to display in an image control the picture associated with the step, if there is one, for that particular step in a process.

I've tried using pieces of the code from above and adapting it to the report but I'm not having any luck.
Dec 6 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.