USING THE OPEN FILE DIALOG API CALL

The Office Developer Edition Tools include an ActiveX control called Common Dialogs. But if you're like me, you would rather not use ActiveX controls in your application and pull in the overhead. All the dialogs available in the ActiveX Common Dialog methods have a comparable API call. The API call covered here is the Open File dialog. Some of the other API calls are in Listing 15.13.

Listing 15.13. Win32API.txt: API Declarations for Equivalent Common Dialog ActiveX Methods
Declare Function GetOpenFileName Lib "comdlg32.dll" Alias _
                "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
Declare Function GetSaveFileName Lib "comdlg32.dll" Alias _
                "GetSaveFileNameA" (pOpenfilename As OPENFILENAME) As Long
Declare Function ChooseColor Lib "comdlg32.dll" Alias _
                "ChooseColorA" (pChoosecolor As CHOOSECOLOR) As Long
Declare Function ChooseFont Lib "comdlg32.dll" Alias _
                "ChooseFontA" (pChoosefont As CHOOSEFONT) As Long
Declare Function PrintDlg Lib "comdlg32.dll" Alias _
                "PrintDlgA" (pPrintdlg As PRINTDLG) As Long
Declare Function PageSetupDlg Lib "comdlg32.dll" Alias _
                "PageSetupDlgA" (pPagesetupdlg As PAGESETUPDLG) As Long

You can use the Open File dialog in just about every app written when front- and back-end scenarios are used. Primarily it will be used for letting users locate the back end if the links are broken to the tables. If you want to look at this case, check out Chapter 26, “Startup Checking System Routines Using DAO,” and Chapter 27, “Startup Checking System Routines Using ADO.” You can see the dialog in action in Figure 15.16. This figure also shows the calling form UseOpenFileDialog, which is located on the CD-ROM in ExamplesChap15Chap15.mdb.

Figure 15.16. This API call is necessary for every application you write.


The event procedure call to the API wrapper is very few lines of code, attached to the command button cmdOpenFile:

Private Sub cmdOpenFile_Click()
    If IsNull(Me!txtFileToOpen) Then
        Me!txtFileToOpen = ap_OpenFile()
    Else
        Me!txtFileToOpen = ap_OpenFile(Me!txtFileToOpen)
    End If
End Sub

Listing 15.14 shows the code for declaring the API, along with creating a custom type structure to pass to the API call. This code is located in the modFileOpenRoutine module, also in ExamplesChap15Chap15.mdb. Next are some constants used for the common dialog routines. Lastly is the wrapper function itself, ap_OpenFile().

Listing 15.14. Chap15.mdb: The Code Necessary for Getting a Filename by Using the Open File Dialog
Option Compare Text
Option Explicit
Private Declare Function ap_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
    FilterIndex 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

Const cdlOFNAllowMultiselect = &H200
Const cdlOFNCreatePrompt = &H2000
Const cdlOFNExplorer = &H80000
Const cdlOFNExtensionDifferent = &H400
Const cdlOFNFileMustExist = &H1000
Const cdlOFNHelpButton = &H10
Const cdlOFNHideReadOnly = &H4
Const cdlOFNLongNames = &H200000
Const cdlOFNNoChangeDir = &H8
Const CdlOFNNoDereferenceLinks = &H100000
Const cdlOFNNoLongNames = &H40000
Const CdlOFNNoReadOnlyReturn = &H8000
Const cdlOFNNoValidate = &H100
Const cdlOFNOverwritePrompt = &H2
Const cdlOFNPathMustExist = &H800
Const cdlOFNReadOnly = &H1
Const CdlOFNShareAware = &H4000

Public Function ap_OpenFile(Optional ByVal strFileNameIn _
                     As String = "", Optional strDialogTitle _
                     As String = "Name of File to Open")
    Dim lngReturn As Long
    Dim intLocNull As Integer
    Dim strTemp As String
    Dim ofnFileInfo As OPENFILENAME
    Dim strInitialDir As String
    Dim strFileName As String

    '-- if a file path passed in with the name,
    '-- parse it and split it off.

    If InStr(strFileNameIn, "") <> 0 Then

        strInitialDir = Left(strFileNameIn, InStrRev(strFileNameIn, ""))
        strFileName = Left(Mid$(strFileNameIn, _
                            InStrRev(strFileNameIn, "") + 1) & _
                            String(256, 0), 256)

    Else
        strInitialDir = Left(CurrentDb.Name, _
                            InStrRev(CurrentDb.Name, "") - 1)
        strFileName = Left(strFileNameIn & String(256, 0), 256)

    End If

    With ofnFileInfo
        .lStructSize = Len(ofnFileInfo)
        .lpstrFile = strFileName
        .lpstrFileTitle = String(256, 0)
        .lpstrInitialDir = strInitialDir
        .hwndOwner = Application.hWndAccessApp
        .lpstrFilter = "All Files (*.*)" & Chr(0) & "*.*" & Chr(0)
        .nFilterIndex = 1
        .nMaxFile = Len(strFileName)
        .nMaxFileTitle = ofnFileInfo.nMaxFile
        .lpstrTitle = strDialogTitle
        .flags = cdlOFNFileMustExist Or cdlOFNHideReadOnly Or _
                            cdlOFNNoChangeDir
        .hInstance = 0
        .lpstrCustomFilter = String(255, 0)
        .nMaxCustFilter = 255
        .lpfnHook = 0
    End With

    lngReturn = ap_GetOpenFileName(ofnFileInfo)

    If lngReturn = 0 Then
         strTemp = ""
    Else

         '--Trim off any null string
         strTemp = Trim(ofnFileInfo.lpstrFile)
         intLocNull = InStr(strTemp, Chr(0))

         If intLocNull Then
             strTemp = Left(strTemp, intLocNull - 1)
         End If

    End If

    ap_OpenFile = strTemp

End Function

You can either supply a file path to start with, or leave the text box blank. Another enhancement you can use is copying the ap_OpenFile routine, and then replacing the ap_GetOpenFileName call with one for ap_GetSaveFileName. Don't forget to declare the function as shown here:

Private Declare Function ap_GetSaveFileName Lib "comdlg32.dll" Alias _
   "GetSaveFileNameA" (pOpenfilename As OPENFILENAME) As Long

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.27.45