ACC: How to Load OLE Objects from a Folder into a Table

ID: Q158941

The information in this article applies to:

SUMMARY

Moderate: Requires basic macro, coding, and interoperability skills.

This article shows you how to automatically append all files with a particular extension from a specified folder on the hard disk into a table. This routine is good for loading OLE objects, such as .gif, .jpg, .doc, .xls, or .bmp files that are associated with an OLE Server, into a Microsoft Access database.

This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to your version of the "Building Applications with Microsoft Access" manual.

NOTE: To associate a graphic file with an OLE Server, open it with an OLE Server package such as Microsoft Imager or Microsoft Paint, and save the file.

For information about working programmatically with an OLE object in a form in Microsoft Access version 2.0, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q114214
   TITLE     : ACC2: How to Programmatically Embed or Link an Object in a
               Form

MORE INFORMATION

Method to Import OLE Object Files

 1. Create the following new table in Design view. Save it as tblLoadOLE:

       Table: tblLoadOLE
       ------------------------
       Field Name: OLEID
          Data Type: AutoNumber
       Field Name: OLEPath
          Data Type: Text
          Field Size: 255
       Field Name: OLEFile
          Data Type: OLE Object

       Table Properties: tblLoadOLE
       ----------------------------
       PrimaryKey: OLEID

 2. Using the AutoForm: Columnar Wizard, create a new form based on the
    tblLoadOLE table. Save it as frmLoadOLE.

 3. Open the frmLoadOLE form in Design view.

 4. Create three unbound text box controls in the form header section of
    the form:

       Form: frmLoadOLE
       ------------------------
       Text Box:
          Name: SearchFolder
       Text Box:
          Name: SearchExtension
       Text Box:
          Name: OLEClass

 5. Create a command button on the form:

       Command Button
       --------------
       Name: cmdLoadOLE
       Caption: Load Files

 6. Type the following event procedure in the OnClick property of the
    cmdLoadOLE button:

       Private Sub cmdLoadOLE_Click()

       Dim MyFolder As String
       Dim MyExt As String
       Dim MyPath As String
       Dim MyFile As String
       Dim strCriteria As String

       MyFolder = SearchFolder
       ' Get the search path.
       MyPath = MyFolder & "\" & "*." & [SearchExtension]
       ' Get the first file in the path containing the file extension.
       MyFile = Dir(MyPath, vbNormal)
       Do While Len(MyFile) <> 0
          [OLEPath] = MyFolder & "\" & MyFile
          [OLEFile].Class = [OLEClass]
          [OLEFile].OLETypeAllowed = acOLEEmbedded
          [OLEFile].SourceDoc = [OLEPath]
          [OLEFile].Action = acOLECreateEmbed
          ' Check for next OLE file in the folder.
          MyFile = Dir
          ' Go to new record on form.
          ' For Access 95 only, use the following Line of code:
          DoCmd.DoMenuItem acFormBar, acEditMenu, 12, 4, acMenuVer70

          ' For Access 97 only, use the following line of code:
          'DoCmd.RunCommand acCmdRecordsGoToNew
       Loop

       End Sub

 7. Save the frmLoadOLE form and open it in Form view.

 8. Type the full path name of the folder you want to search in the
    SearchFolder text box.

 9. Type the file extension you want to load in the SearchExtension text
    box, such as bmp, jpg, doc, xls, tif, or gif. Do not type a period as
    part of the extension.

10. Type the Class name for the type of file you are loading, such as
    Paint.Picture for .bmp files.

    NOTE: To determine the Class name of an OLE object, see the
    documentation for the application supplying the object.

11. Click the Load Files button. Note that All files that match the
    SearchFolder and SearchExtension you entered are added to the
    tblLoadOLE table.

Additional query words: Directory Multiple
Keywords          : kbinterop kbprg IntpOle 
Version           : 7.0 97
Platform          : WINDOWS
Hardware          : x86
Issue type        : kbhowto

Last Reviewed: January 8, 1999