422,946 Members | 1,129 Online
Bytes IT Community
Submit an Article
Got Smarts?
Share your bits of IT knowledge by writing an article on Bytes.

Display images in your DB without using Attachment Fields

Expert Mod 100+
P: 2,440
The later versions of MS Access have a field type called Attachment. This allows the user to "upload" documents and images into the database, so that all things are stored neatly in one place. However, as one advances in one's Access prowess, one finds that this new field types causes a few problems. One problem is that the size of your DB can grow significantly if you store a lot of documents in your Attachment fields. The second problem is that using an Attachment field isn't quite as straightforward as working with a regular text or number field. However, one thing that many users use Attachment field for is to display images on their forms.

This article provides an alternate method for displaying images associated with a record.

Let's say you have a Form frmCoolCars. Each record in your main Table tblCars has an image of the car associated with that record. Rather than uploading your image to the DB, you can save that image to a common location. For example, C:\Users\twinnyfo\Documents\DB\Cars\Images\. Create an additional text field in your table called CarImage. In that field, eneter the pull path and filename of your image: C:\Users\twinnyfo\Documents\DB\Cars\Images\Ferarri .jpg.

Now, just add an image control to your form (imgCar). Set the .PictureType Property to Linked, but make sure you do not include anything for the .Picture property. This comes later.

Now, in the OnCurrent Event of your Form, add the following code;

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Current()
  2. On Error GoTo EH
  4.     Me.imgCar.Picture = Me.CarImage
  6.     Exit Sub
  7. EH:
  8.     MsgBox "There was an error going to the current record!" & vbCrLf & vbCrLf & _
  9.         "Error: " & Err.Number & vbCrLf & _
  10.         "Description: " & Err.Description & vbCrLf & vbCrLf & _
  11.         "Please contact your Database Administrator.", vbCritical, "WARNING!"
  12.     Exit Sub
  13. End Sub
Nothing "rocket sciency" about this one, but just very useful.

Keep in mind that you're probably gonna wanna double-check a few things, like making sure the image exists, before you set the image file. You can also add code in your form to browse for image files and save that location/filename, and there would even be possibilities for adding multiple images for one record. BUT, this article is just a quickie on the basics--just in case you ever wanted to do something like this, so all the bells and whistles have been removed.
2 Weeks Ago #1
Share this Article
Share on Google+