If you have a column in a Microsoft Access table that has the name of an image that refers to a file on the disk, you can update an image control in either a form or report with the code below. Put this code into a standard VBA module that you create with Insert Module from the menu. This code assumes that all the images are in a subdirectory of the location of the database called "images" if there are relative paths to the image file.
Public Const strImageFolder = "images"
Public Sub DisplayImage(ctlImageControl As Control, strImagePath As Variant)
On Error GoTo ErrorHandler
If IsNull(strImagePath) Then
ctlImageControl.Visible = False
Else
If InStr(1, strImagePath, "\") = 0 Then
strImagePath = Application.CurrentProject.Path & "\" & strImageFolder & "\" & strImagePath
End If
ctlImageControl.Visible = True
ctlImageControl.Picture = strImagePath
End If
Exit Sub
ErrorHandler:
Select Case Err.Number
Case 2114 'Doesn't support the format of the file
ctlImageControl.Visible = False
Case 2220 ' Can't find the picture.
ctlImageControl.Visible = False
Case Else ' Some other error.
MsgBox "Unexpected Error #" & Err.Number & " " & Err.Description, vbExclamation, "Unexpected Error"
End Select
End Sub
To use this code in a form, if the control that contains the image name txtPicture and the image control is named imgPicture then add this code to the module for the form:
Private Sub Form_AfterUpdate()
Call DisplayImage(Me!imgPicture, Me!txtPicture)
End Sub
Private Sub Form_Current()
Call DisplayImage(Me!imgPicture, Me!txtPicture)
End Sub
Private Sub txtPicture_AfterUpdate()
Call DisplayImage(Me!imgPicture, Me!txtPicture)
End Sub
To use it in a report, assuming there is a column in the table or query the report is based on called strPicture and an image control named imgPicture, add this code to the report:
Private Sub Detail_Print(Cancel As Integer, PrintCount As Integer)
Call DisplayImage(Me!imgPicture, Me!strPicture)
End Sub
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.