Code:
Option Compare Database
Option Explicit
Public Function DisplayImage(rctlImage As Access.Control, rvarImagePath As Variant) As String
On Error GoTo Err_DisplayImage
Dim strDatabasePath As String
Dim strImagePath As String
Dim intSlashLocation As Integer
Dim strResult As String
'Check control type.
If Not (TypeOf rctlImage Is Access.Image) Then
rctlImage.Visible = False
strResult = "Invalid image control."
GoTo Exit_DisplayImage
End If
'Check general format of image path.
If Trim(Nz(rvarImagePath, "")) = "" Then
rctlImage.Visible = False
strResult = "No image path."
GoTo Exit_DisplayImage
Else
strImagePath = Trim(CStr(rvarImagePath)) 'Convert to string.
End If
'Add full path extension if necessary.
If strDatabasePath Like "[A-Z]:\*" Then 'Assume full path is supplied. May need to test for [a-z]
Else
strDatabasePath = CurrentProject.FullName
intSlashLocation = InStrRev(strDatabasePath, "\", Len(strDatabasePath))
strDatabasePath = Left(strDatabasePath, intSlashLocation)
If strImagePath Like "\*" Then 'Strip leading back slash.
strImagePath = Right(strImagePath, Len(strImagePath) - 1)
End If
strDatabasePath = strDatabasePath & strImagePath
End If
rctlImage.Picture = strDatabasePath
rctlImage.Visible = True
strResult = "Image found."
Exit_DisplayImage:
DisplayImage = strResult
Exit Function
'I have altered none of the error handling code.
Err_DisplayImage:
Select Case Err.Number
Case 2220 ' Can't find the picture.
rctlImage.Visible = False
strResult = "Can't find image in the specified name."
Resume Exit_DisplayImage
Case Else ' Some other error.
MsgBox Err.Number & " " & Err.Description
strResult = "An error occurred displaying image."
Resume Exit_DisplayImage
End Select
End Function
I believe that the procedure should check that the control type of the first argument is an image control. You could go one step further and be more explicit in defining the argument as rimgControl As Access.Image
I think
Code:
If Trim(Nz(rvarImagePath, "")) = "" Then
Is a better test than simply testing for null since it will catch values that are zero length string and values that are one or more spaces.
The comment that if there are no back-slashes then the path is ‘relative’ is misleading. What is meant is that the image resides in the ‘same directory’ as the Access mdb. Here I think a better test is as follows:- If the path begins with a letter followed by a colon, then it is safe to assume the complete explicit path is supplied.
- If there are no ‘\’s in the path, then assume the same directory.
- If there are ‘\’s in the path assume a sub directory. (Ensure that there is no double ‘\’)
The second and third actions above require identical code.
The original code relies heavily on Access’ automatic variable type conversions. I prefer to force the type, hence I convert the path to a string at the earliest opportunity.
There will still be run-time errors if the path is not properly formed or the picture does not exist so the error handling is essential.
(BTW I have prefixed the argument names with a lower case ‘r’ indicating that the arguments are passed by reference. This is non-standard and is a little quirk of mine. It corresponds to prefixing arguments with ‘v’ when they are passed by value. It’s easy to determine which when viewing the procedure definition but not always so easy when writing the calling code.)
You had some errors – mainly typos – in your procedure. Label names end in a colon but when referenced in the code the colon is omitted.
OK, how to get you up and running?
Delete the Bound Object Frame from your form and insert an Image control. Name this control imgFirework – or whatever.
You have a field on the table called, ‘Picture.’ I assume this is to contain the full or fragment path of the associated picture. (I suggest you make the default for this field, “\images\0000.jpg” rather than just “\images\.”
In the declaration section of the code behind your form type the following.
Code:
Dim strResult as String
Somewhere else type
Code:
Private Sub CallDisplayImage()
strResult = DisplayImage(Me!imgFirework, Me!Picture)
End Sub