Draneer Code Library

Common Dialog - Path and File Browsing in local file system

Typical Usage:
 - Save the Module Code into a module called something like modCommonDialog.
 - See typical usage code below module code.

Module Code
=========================================================================================================
'---- From Access 97 Developer's Handbook
'---- by Litwin, Getz, and Gilbert (Sybex)
'---- Copyright 1997. All rights reserved.

Option Compare Database
Option Explicit

'---- General Errors
Public Const adhcAccErrSuccess = 0
Public Const adhcAccErrUnknown = -1

'---- Common Dialogs
'---- GetFileName errors
Public Const adhcAccErrGFNCantOpenDialog = -301
Public Const adhcAccErrGFNUserCancelledDialog = -302


'---- GetFileNameInfo flags
Public Const adhcGfniConfirmReplace = &H1 ' Prompt if overwriting a file?
Public Const adhcGfniNoChangeDir = &H2
' Disable the read-only option
Public Const adhcGfniAllowReadOnly = &H4
' Don't change to the directory the user selected?
Public Const adhcGfniAllowMultiSelect = &H8
' Allow multiple-selection?
Public Const adhcGfniDirectoryOnly = &H20
' Open as directory picker?
Public Const adhcGfniInitializeView = &H40
' Initialize the view to the lView member or use last selected view?

'---- Views in the Office Find File dialog
Public Const adhcGfniViewDetails = 0 ' Details
Public Const adhcGfniViewPreview = 1
' Preview
Public Const adhcGfniViewProperties = 2
' Properties
Public Const adhcGfniViewList = 3
' List (typical)

Type adh_accOfficeGetFileNameInfo
  hwndOwner As Long
  strAppName As String * 255
  strDlgTitle As String * 255
  strOpenTitle As String * 255
  strFile As String * 4096
  strInitialDir As String * 255
  strFilter As String * 255
  lngFilterIndex As Long
  lngView As Long
  lngFlags As Long
End Type

Declare Function adh_accOfficeGetFileName Lib "msaccess.exe" _
  Alias "#56" (gfni As adh_accOfficeGetFileNameInfo, ByVal fOpen As Integer) As Long

Function adhOfficeGetFileName(gfni As adh_accOfficeGetFileNameInfo, _
                              ByVal fOpen As Integer) As Long

 
'---- Use the Office file selector common dialog
  '---- exposed by Access.

  '---- From Access 97 Developer's Handbook
  '---- by Litwin, Getz, and Gilbert (Sybex)
  '---- Copyright 1997. All rights reserved.

  Dim lng As Long
  With gfni
    .strAppName = RTrim$(.strAppName) & vbNullChar
    .strDlgTitle = RTrim$(.strDlgTitle) & vbNullChar
    .strOpenTitle = RTrim$(.strOpenTitle) & vbNullChar
    .strFile = RTrim$(.strFile) & vbNullChar
    .strInitialDir = RTrim$(.strInitialDir) & vbNullChar
    .strFilter = RTrim$(.strFilter) & vbNullChar
    SysCmd acSysCmdClearHelpTopic
    lng = adh_accOfficeGetFileName(gfni, fOpen)
    .strAppName = RTrim$(adhTrimNull(.strAppName))
    .strDlgTitle = RTrim$(adhTrimNull(.strDlgTitle))
    .strOpenTitle = RTrim$(adhTrimNull(.strOpenTitle))
    .strFile = RTrim$(adhTrimNull(.strFile))
    .strInitialDir = RTrim$(adhTrimNull(.strInitialDir))
    .strFilter = RTrim$(adhTrimNull(.strFilter))
  End With
  adhOfficeGetFileName = lng
End Function


Function adhTrimNull(strVal As String) As String
 
'---- Trim the end of a string, stopping at the first
  '---- null character.

  '---- From Access 97 Developer's Handbook
  '---- by Litwin, Getz, and Gilbert (Sybex)
  '---- Copyright 1997. All rights reserved.

  Dim intPos As Integer
  intPos = InStr(strVal, vbNullChar)
  If intPos > 0 Then
    adhTrimNull = Left$(strVal, intPos - 1)
  Else
    adhTrimNull = strVal
  End If
End Function

=========================================================================================================

Typical usage procedures
=========================================================================================================

Private Sub cmdSelectFile_Click()
'---- Get the import estimate file path and name
  Dim gfni As adh_accOfficeGetFileNameInfo
  With gfni
    .strInitialDir = "."
    .strDlgTitle = "Select Estimate File to Import"
    .strFile = ""
    .strFilter = "USF Imports (*.CSV)|*.CSV|Text Files (*.TXT)|*.TXT|All Files (*.*)|*.*|"
  End With
  If adhOfficeGetFileName(gfni, True) = adhcAccErrSuccess Then
    [txtFileName] = Trim(gfni.strFile)
    [txtFileName].SetFocus
  End If
End Sub


Private Sub cmdSelectPath_Click()
  '---- Get the export estimate file path
  Dim gfni As adh_accOfficeGetFileNameInfo
  With gfni
    .lngFlags = adhcGfniDirectoryOnly
    .strOpenTitle = "Use"
    .strInitialDir = "."
    .strDlgTitle = "Select Export File Path"
    .strFile = ""
  End With
  If adhOfficeGetFileName(gfni, True) = adhcAccErrSuccess Then
    [txtPath] = Trim(gfni.strFile)
    [txtPath].SetFocus
  End If
End Sub

=========================================================================================================

Back to ...
Code Library Menu