By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,821 Members | 1,319 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,821 IT Pros & Developers. It's quick & easy.

Minor Error with Code for Attaching Images to a Form

P: 2

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()
  4.   On Error GoTo Err_cmdInsertPic_Click
  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
  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
  22. Err_cmdInsertPic_Click:
  23.   MsgBox Err.Description, vbExclamation
  24. End Sub
Aug 15 '08 #1
Share this Question
Share on Google+
1 Reply

Expert Mod 2.5K+
P: 2,545
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.

Aug 15 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.