Connecting Tech Pros Worldwide Help | Site Map

Minor Error with Code for Attaching Images to a Form

Newbie
 
Join Date: Aug 2008
Location: Caribbean
Posts: 2
#1: Aug 15 '08
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.

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdInsertPic_Click()
  2.  
  3.   Dim OFN As OPENFILENAME
  4.   On Error GoTo Err_cmdInsertPic_Click
  5.  
  6.   ' Set options for dialog box.
  7.   With OFN
  8.     .lpstrTitle = "Images"
  9.     If Not IsNull([ImagePath]) Then .lpstrFile = [ImagePath]
  10.     .flags = &H1804 ' OFN_FileMustExist + OFN_PathMustExist + OFN_HideReadOnly
  11.     .lpstrFilter = MakeFilterString("Image files (*.bmp;*.gif;*.jpg;*.wmf)", "*.bmp;*.gif;*.jpg;*.wmf", _
  12.       "All files (*.*)", "*.*")
  13.   End With
  14.  
  15.   If OpenDialog(OFN) Then
  16.     [ImagePath] = OFN.lpstrFile
  17.     Forms!Employee Profile Form![ImageFrame].Picture = [ImagePath]
  18.     SysCmd acSysCmdSetStatus, "Afbeelding: '" & [ImagePath] & "'."
  19.   End If
  20.   Exit Sub
  21.  
  22. Err_cmdInsertPic_Click:
  23.   MsgBox Err.Description, vbExclamation
  24. End Sub
Thanks
Forsi
Moderator
 
Join Date: Feb 2008
Location: Beauly, near Inverness, Scotland
Posts: 1,576
#2: Aug 15 '08

re: Minor Error with Code for Attaching Images to a Form


Hi. I added code tags round your code to aid readability. As you may see above, it makes it much easier to differentiate one line from another.

The problem you are experiencing (at what is now line 17 above) results from the spaces in the form name, as you surmised. All such names - of fields, forms, reports, controls and so on - must be enclosed in rectangular brackets '[' and ']' if they contain spaces. A corrected version is shown below.

Expand|Select|Wrap|Line Numbers
  1.     Forms![Employee Profile Form]![ImageFrame].Picture = [ImagePath]
  2.     SysCmd acSysCmdSetStatus, "Afbeelding: '" & [ImagePath] & "'."
The error message generated by the compiler arose because without the brackets it considered that the name of the object or variable concerned ended at the space after the word Employee - and it expected an operator such as "=" to follow.

-Stewart
Reply