This article was previously published under Q198466
Moderate: Requires basic macro, coding, and interoperability skills.
↑ Back to the top
This article shows you how to automatically append all files with a
particular extension from a specified folder on the hard disk to 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.
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.
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.
↑ Back to the top
Method to Import OLE Object Files
- 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
- Use the AutoForm: Columnar Wizard to create a new form based on the
tblLoadOLE table. Save it as frmLoadOLE.
- Open the frmLoadOLE form in Design view.
- Create the following 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
- Create the following command button on the form:
Command Button
--------------
Name: cmdLoadOLE
Caption: Load Files
- 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 = Me!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.
DoCmd.RunCommand acCmdRecordsGoToNew
Loop
End Sub
- Save the frmLoadOLE form and open it in Form view.
- In the SearchFolder text box, type the full path name of the folder
that you want to search.
- In the SearchExtension text box, type the file extension that you want
to load, such as bmp, jpg, doc, xls, tif, or gif. Do not type a period
as part of the extension. For example, do not type .bmp.
- Type the Class name for the type of file that 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.
- Click the Load Files button. Note that All files that match the
SearchFolder and SearchExtension that you entered are added to the
tblLoadOLE table.
↑ Back to the top