A simple way is to make use of the common dialog. Below is some code that
uses the Windows API to invoke the dialog which eliminates the need to
distribute the ActiveX control with your applications.
Here is the code to invoke the dialog -- place it in the click event of a
button. You can set the view options of the dialog to Icons to view
thumbnails of the documents.
Private Sub cmdBrowseEmployees_Click()
On Error GoTo Err_Proc
Me.txtFileNameEmployees = LaunchCD(Me) <-------------------- to
invoke the dialog
Exit_Proc:
Exit Sub
Err_Proc:
MsgBox Err.Number & "-" & Err.Description
Resume Exit_Proc
End Sub
In a standard module --------
Option Compare Database
Option Explicit
Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias _
"GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
Private Type OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type
Function LaunchCD(strform As Form) As String
Dim OpenFile As OPENFILENAME
Dim lReturn As Long
Dim sFilter As String
OpenFile.lStructSize = Len(OpenFile)
OpenFile.hwndOwner = strform.hwnd
If strform.Name = "frmImportSpreadsheet" Then
If Application.Version >= 12 Then
sFilter = "Excel Files (*.xls; *.xlsx)" & Chr(0)
Else
sFilter = "Excel Files (*.xls)" & Chr(0)
End If
Else
If Application.Version >= 12 Then
sFilter = "Access Files (*.mdb; *.accdb)" & Chr(0)
Else
sFilter = "Access Files (*.mdb)" & Chr(0)
End If
End If
OpenFile.lpstrFilter = sFilter
OpenFile.nFilterIndex = 1
OpenFile.lpstrFile = String(257, 0)
OpenFile.nMaxFile = Len(OpenFile.lpstrFile) - 1
OpenFile.lpstrFileTitle = OpenFile.lpstrFile
OpenFile.nMaxFileTitle = OpenFile.nMaxFile
OpenFile.lpstrInitialDir = "C:\"
OpenFile.lpstrTitle = "Select a file"
OpenFile.flags = 0
lReturn = GetOpenFileName(OpenFile)
If lReturn = 0 Then
MsgBox "A file was not selected!", vbInformation, _
"Select a Spreadsheet to import"
Else
LaunchCD = Trim(OpenFile.lpstrFile)
End If
End Function