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

button attach file to Object OLE field

P: 8
(Using Access 2003)

I have a an Object OLE field in a table where users can insert Excel sheets, Word Docs, Pdfs etc.

I need to create a button on a formulaire that allows the user to browse for, select and attach the file. The file must appear as an icon that can be double clicked on to be opened. The file should be stocked in the DB.

I can make a dialog box appear and browse for the file and copy the path into the field using this code:
Expand|Select|Wrap|Line Numbers
  1. Private Sub AttachButton_Click()
  2.   Dim varFile As Variant
  4.   With Application.FileDialog(msoFileDialogFilePicker)
  5.     With .Filters
  6.       .Clear
  7.     End With
  8.     .AllowMultiSelect = False
  9.     .InitialFileName = vbNullString
  10.     .InitialView = msoFileDialogViewDetails
  12.     If .Show = True Then
  13.       For Each varFile In .SelectedItems
  14.         Me![File] = varFile
  15.       Next
  16.     Else
  17.       MsgBox "No file attached ! "
  18.     End If
  19.   End With
  20. End Sub
Thanks to: ADezii

But it needs to be an object sitting there, not just the path to the file.
Any help or alternative suggestions would be appreciated.

Aug 5 '09 #1
Share this Question
Share on Google+
10 Replies

Expert 5K+
P: 8,679
Rather than go into a detailed explanation, I've created a simple Database and made it available to you as an Attachment. Clicking on the Insert OLE Object Command Button will:
  1. Open the Insert Object Dialog Box, from which you will:
    1. Select Create From File
    2. Browse ==> Select File to Insert ==> OK
    3. Select Display as Icon ==> OK
  2. The OLE Object will now be inserted into the OLE Object Field and displayed as an Icon.
  3. Double-Clicking on the Icon while in Form View will Open the File represented as an Icon in the Registered OLE Server for that File Type.
  4. No need for the File Dialog since the Insert Object Dialog will simply replace it.
  5. Good Luck, and Good Night!
Attached Files
File Type: zip OLE (11.9 KB, 2279 views)
Aug 5 '09 #2

P: 8
Thank you that works very well
Aug 21 '09 #3

Expert 5K+
P: 8,679
You are quite welcome.
Aug 21 '09 #4

P: 3
Can you pls provide the source code for the attachment, as I am new to VBA
Jun 15 '10 #5

Expert 5K+
P: 8,679
  1. Download the Attachment
  2. Set the Display Type Property of the Bound Object Frame to Icon
Jun 15 '10 #6

P: 3
Thanks for reply, How can I get the file path name of file selected by using bound control ?
I need to show the complet file location in some text box.
Jun 16 '10 #7

Expert 5K+
P: 8,679
Download the Attachment which should say it all.
Attached Files
File Type: zip OLE (15.8 KB, 1035 views)
Jun 16 '10 #8

P: 1
Thank you so much! This was extremely helpful!
Feb 10 '12 #9

P: 14
Thanks a lot. Useful for me too
May 12 '16 #10

Expert Mod 5K+
P: 5,397
It should be pointed out for those that come across this thread that it's not normally advisable to store attachments within an access database, doing so consumes a lot of the limited space within the database file and if the database is corrupted you will lose your documents.

A better option is to store links to the appropriate file and then Shell() out the file or use the appropriate Office-Automation to instance the correct program for the Office document.
May 13 '16 #11

Post your reply

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