Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with code to display images from a folder in a form. 2

Status
Not open for further replies.

LD1010

Technical User
Dec 6, 2001
78
US
I have tried using the method outlined in the MS Support Article ID: 285820 for displaying images from a folder in a form. They provide code for a Public Function called DisplayImage. I copied and pasted it into a new Module and it complied without an error. Then in the form that displays the image they instruct me to insert the following code:

Private Sub Form_AfterUpdate()
CallDisplayImage
End Sub

When I complied the code I get the error: Sub or Function not defined. How do I "define" the function?

VBA is still new ground for me. Thanks in advance for any help on this, and a MERRY CHRISTMAS to ALL!
 
Insert a space after the keyword CALL
As
Code:
Private Sub Form_AfterUpdate()
    Call DisplayImage
End Sub
 
Thanks RobertT687, I inserted the space after Call but now get a new error: Argument not optional

Here is all the code I got from the article.

Module1 Code:

Public Function DisplayImage(ctlImageControl As Control, strImagePath As Variant) As String
On Error GoTo Err_DisplayImage

Dim strResult As String
Dim strDatabasePath As String
Dim intSlashLocation As Integer

With ctlImageControl
If IsNull(strImagePath) Then
.Visible = False
strResult = "No image name specified."
Else
If InStr(1, strImagePath, "\") = 0 Then
' Path is relative
strDatabasePath = CurrentProject.FullName
intSlashLocation = InStrRev(strDatabasePath, "\", Len(strDatabasePath))
strDatabasePath = Left(strDatabasePath, intSlashLocation)
strImagePath = strDatabasePath & strImagePath
End If
.Visible = True
.Picture = strImagePath
strResult = "Image found and displayed."
End If
End With

Exit_DisplayImage:
DisplayImage = strResult
Exit Function

Err_DisplayImage:
Select Case Err.Number
Case 2220 ' Can't find the picture.
ctlImageControl.Visible = False
strResult = "No employee photo available."
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

frmActiveEmployees Code:
--------------
Private Sub Form_Afterupdate()
Call DisplayImage
End Sub
--------------
Private Sub Form_Current()
Call DisplayImage
End Sub

--------------
Private Sub txtEmployeePhoto_AfterUpdate()
Call DisplayImage
End Sub

-------------- (This was the last piece of code in the atricle but I don't know where to place it.)

Private Sub CallDisplayImage()
Me!txtPhotoNote = DisplayImage(Me!ImageFrame, Me!txtEmployeePhoto)
End Sub
 
lets say your image control is called
imgCtlOne
and your path to the image is displayed in
txtBxPath

Call DisplayImage(imgCtlOne , me.txtBxPath.value)

you have to pass in the image control and the path to the image.
 
Thanks RobertT687 and MajP! That got it working.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top