Minor Error with Code for Attaching Images to Forms

Forsi

New member
Local time
Yesterday, 18:28
Joined
Aug 15, 2008
Messages
7
Hello

I don't know a thing about writing VBA Codes but this forum I was able thas helped me to finally figure out how to link the pictures to a path etc on a form. Everything works fine on the form - the pictures change with each employee etc, etc. However, when I select the command button "change picture" or "Remove", it opens the dialog box or the folder on the c:\drive etc and I can select a new picture or the one for deletion; BUT I also get a message which says compile error, syntax error and it highlights in yellow the name Private Sub cmdInsertPic_Click(). The name of the form in which the employee data is stored is Employee Profile Form (from the table Employee Profile). The computer highlights the word "Profile" and gives the error (Expected =). It is possible that it is the spacing between the three words in the file name is the problem because if I were to rename the form to a single word, it works. The section with the error is emboldened. The code is listed below. Any assistance you can provide would be appreciated.

Private Sub cmdInsertPic_Click()

Dim OFN As OPENFILENAME
On Error GoTo Err_cmdInsertPic_Click

' Set options for dialog box.
With OFN
.lpstrTitle = "Images"
If Not IsNull([ImagePath]) Then .lpstrFile = [ImagePath]
.flags = &H1804 ' OFN_FileMustExist + OFN_PathMustExist + OFN_HideReadOnly
.lpstrFilter = MakeFilterString("Image files (*.bmp;*.gif;*.jpg;*.wmf)", "*.bmp;*.gif;*.jpg;*.wmf", _
"All files (*.*)", "*.*")
End With

If OpenDialog(OFN) Then
[ImagePath] = OFN.lpstrFile
Forms!Employee Profile Form![ImageFrame].Picture = [ImagePath]
SysCmd acSysCmdSetStatus, "Afbeelding: '" & [ImagePath] & "'."
End If
Exit Sub

Err_cmdInsertPic_Click:
MsgBox Err.Description, vbExclamation
End Sub

Thank you
Forsi
 
Last edited:
You need square brackets around 'Employee Profile Form' as it contains whitespace.
 
Simple Software Solutions

Another simpler and more easily readable way of referring to form fields is as follows.

Forms("Name Of Form")("Name Of Control").Whatever

CodeMaster::cool:
 
Both ways work. My sincerest appreciation to both of you.
One more question. Using this same code, how can I insert a text box message to say something to the effect: "No Image is Displayed for this Employee. Select Add/Change Picture to insert a photo"? Currently, if no image is selected, it will display the last image chosen for a previous employee.
 
Hey. For the remove picture command button try setting the field that stores the image path as null. Also, add a label underneath the image control with its visible property set to No. For the remove picture command button, add code such as:
Me.LabelName.Caption = "No Image is Displayed for this Employee. Select Add/Change Picture to insert a photo"
Me.LabelName.Visible = True

Also, you'll want to add Me.LabelName.Visible = False for when a picture is added, and also, perhaps code on the current event such as:
If IsNull(Me.Photo) Then
Me.LabelName.Visible = True
Me.LabelName.Caption = "No Image is Displayed for this Employee. Select Add/Change Picture to insert a photo"
Else
Me.LabelName.Visible = False
End If

Just a thought, hope it helps.
 
Hi Zanaeira
Thank you for responding. I tried what you indicated (with my ltd knowledge of codes). It worked for Remove Picture. However, as I move through each record, I see the message "Click Add/Change Picture to Add Photo" on each picture (I named the label errormsg). Also, when I get to the last record, I still see the last picture that was inserted. It is possible that I may need to remove a section of the code so that this will not happen any more but I don't know which line to delete.

This is the code. If it isn't much trouble, can you edit it accordingly please? You said "Also, you'll want to add Me.LabelName.Visible = False for when a picture is added, and also, perhaps code on the current event " so I did:

ON CURRENT PROPERTY:

Private Sub Form_Current()
On Error Resume Next
Me![ImageFrame].Picture = Me![ImagePath]
If IsNull(Me.Photo) Then
Me.errormsg.Visible = True
Me.errormsg.Caption = "Click Add/Change Picture to Add Employee Picture"
Else
Me.errormsg.Visible = False
End If
End Sub

IMAGE AFTER UPDATE:

Private Sub ImagePath_AfterUpdate()
On Error Resume Next
Me![ImageFrame].Picture = Me![ImagePath]
If IsNull(Me.Photo) Then
Me.errormsg.Visible = True
Me.errormsg.Caption = "Click Add/Change Picture to Add Employee Picture"
Else
Me.errormsg.Visible = False
End If
End Sub


REMOVE PIC

Private Sub cmdErasePic_Click()

If Not IsNull([ImagePath]) Then
If MsgBox("The image will be removed from this record. Are you sure?", vbYesNo + vbQuestion) = vbYes Then
Forms("Employee Profile Form")("ImageFrame").Picture = ""
[ImagePath] = Null
SysCmd acSysCmdClearStatus
Call Form_Current
End If
End If
Me.errormsg.Caption = "Click Add/Change Picture to Add Employee Picture"
Me.errormsg.Visible = True
End Sub

INSERT PIC - I did not edit this but it is included in case you need it

Private Sub cmdInsertPic_Click()
Dim OFN As OPENFILENAME
On Error GoTo Err_cmdInsertPic_Click

' Set options for dialog box.
With OFN
.lpstrTitle = "Images"
If Not IsNull([ImagePath]) Then .lpstrFile = [ImagePath]
.flags = &H1804 ' OFN_FileMustExist + OFN_PathMustExist + OFN_HideReadOnly
.lpstrFilter = MakeFilterString("Image files (*.bmp;*.gif;*.jpg;*.wmf)", "*.bmp;*.gif;*.jpg;*.wmf", _
"All files (*.*)", "*.*")
End With

If OpenDialog(OFN) Then
[ImagePath] = OFN.lpstrFile
Forms![Employee Profile Form]![ImageFrame].Picture = [ImagePath]
SysCmd acSysCmdSetStatus, "Afbeelding: '" & [ImagePath] & "'."
End If
Exit Sub

Err_cmdInsertPic_Click:
MsgBox Err.Description, vbExclamation
End Sub

Thanks, Forsi
 
Hey, sorry to say I actually don't understand the code you have for your cmdInsertPic_Click. I've never seen this OPENFILENAME and when I tried it on my Access it didn't come up :S. I'm using Access 2007 by the way. You're loading an image using an image control with its control source set to a field that stores the file path right? Well, in one of my database I also do this using the following code:

Private Sub cmdAdd_Click()
Dim strPath As String
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
.Title = "Locate the employee Picture"
.ButtonName = "Select"
.Filters.Clear
.Filters.Add "JPEGs", "*.jpg"
.Filters.Add "Bitmaps", "*.bmp"
.FilterIndex = 1
.InitialFileName = "C:\Documents and Settings\Zanaeira\My Documents\My Pictures"
.InitialView = msoFileDialogViewThumbnail
If .Show = 0 Then
Exit Sub
End If
strPath = Trim(.SelectedItems(1))
On Error Resume Next
Me.Photo = strPath
Me.lblPhoto.Caption = "Failed to load the selected picture. Please try again."
End With
Me.Photo.Visible = False
Screen.PreviousControl.SetFocus
Me.cmdDelete.Enabled = True
Me.lblPhoto.Visible = False
End Sub

Private Sub cmdDelete_Click()
If vbYes = (msgbox("Are you sure you want to remove this employee's photo?", vbYesNo + vbQuestion _
+ vbDefaultButton2, "Confirm photo delete")) Then
Me.Photo = Null
Me.lblPhoto.Visible = True
Me.lblPhoto.Caption = "Click Add or double-click this box to add a photo for this employee."
Screen.PreviousControl.SetFocus
Me.cmdDelete.Enabled = False
Else
Exit Sub
End If
End Sub

Private Sub Form_Current()
If IsNull(Me.Photo) Then
Me.lblPhoto.Visible = True
Me.lblPhoto.Caption = "Click Add or double-click this box to add a photo for this employee."
Else
Me.lblPhoto.Visible = False
End If
If Me.NewRecord Then
Me.lblPhoto.Visible = True
Me.lblPhoto.Caption = "Click Add or double-click this box to add a photo for this employee."
Else
Me.lblPhoto.Visible = False
End If
End Sub

Hope this helps.
 
Just to touch on DCrake's reference of the "alternative" syntax - there's a bit more of a description on it for you here and similarly here.

Rather than confuse the issue by further discussing any more code options for selecting the file, i.e. OpenDialog (likely an API solution like this) vs Application.FileDialog, one thing I'd suggest if you're removing the image on records without a path specified (whereby it'll be Null) you'd use
Me![ImageFrame].Picture = Nz(Me![ImagePath], "")
or include it in your If statement

Code:
If IsNull(Me.Photo) Then
    Me![ImageFrame].Picture = ""

As you can't assign Null to such a property of a control.
(Your On Error Resume Next statement is masking that fact to you as your code runs).

Cheers!
 
Guys
I am royally confused (know absolutely nothing about codes or where to place what). Purvis - are you saying that if the code works then let it be, but just add Me![ImageFrame].Picture = Nz(Me![ImagePath], "") or
If IsNull(Me.Photo) Then
Me![ImageFrame].Picture = ""
Please show me exactly where I should include either one of these codes.

Private Sub cmdErasePic_Click()

If Not IsNull([ImagePath]) Then
If MsgBox("The image will be removed from this record. Are you sure?", vbYesNo + vbQuestion) = vbYes Then
Forms("Employee Profile")("ImageFrame").Picture = ""
[ImagePath] = Null
SysCmd acSysCmdClearStatus
Call Form_Current
End If
End If
Me.errormsg.Caption = "Please Click Add/Change Picture to Load Employee Photo"
Me.errormsg.Visible = True
End Sub


ZANAEIRA, I use Access 2003. I am interested in what you are saying as it is definitely something to explore but I have no clue how to edit some of your stuff. If you were to use what I have and fit it in that would be great. With your style I take it you're saying that I would not need an extra module to open the file information etc and everything would be done right away once the buttons are clicked on the form? That's easier...help...I sent a link to the sample database I received from this forum please look at it and you should understand how this whole thing came about.
Thanks and I'm looking forward to hearing from both of you and anyone else who can assist.

Forsi
 

Users who are viewing this thread

Back
Top Bottom