How to use the Common Dialog API in an Access database

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


This article describes how to use the Common Dialog API to replace the Common Dialog Box functionality that is included only with Microsoft Office 97 Developer Edition, Office 2000 Developer Edition, and Office XP Developer Edition.

More Information

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.
  1. Open the sample database Northwind.mdb.
  2. Create a new form. Name the form Form1. Open the new form in Design view.
  3. Add a command button to Form1. Set both the Name property and the Caption property to "Command1."
  4. Add a text box to Form1. Set the Name property to "Text1."
  5. Right-click Command1, click Properties, and then click the Event tab.
  6. In the On Click event procedure, click [Event Procedure] from the drop-down list, and then click the ellipsis to start the Visual Basic Editor.
  7. Modify the Command1_Click procedure to the following:
    Private Sub Command1_Click()
    Me!Text1 = LaunchCD(Me)
    End Sub
  8. On the Insert menu, click Module.
  9. Copy and paste the following code sample in the new module.
    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
    sFilter = "All Files (*.*)" & Chr(0) & "*.*" & Chr(0) & _
    "JPEG Files (*.JPG)" & Chr(0) & "*.JPG" & Chr(0)
    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 using the Common Dialog DLL"
    OpenFile.flags = 0
    lReturn = GetOpenFileName(OpenFile)
    If lReturn = 0 Then
    MsgBox "A file was not selected!", vbInformation, _
    "Select a file using the Common Dialog DLL"
    LaunchCD = Trim(Left(OpenFile.lpstrFile, InStr(1, OpenFile.lpstrFile, vbNullChar) - 1))
    End If
    End Function
  10. On the Debug menu, click Compile and Save All Modulesif you are using Access 97. If you are using Access 2000 or Access 2002, click Compile Northwind, and then close the Code window.
  11. On the View menu, click Form View.
  12. Click Command1. Click a file from the browse window. Put the path of that file in the Text1 box.