Results 1 to 4 of 4
  1. #1

    Browsing w/ Common Dialog Window (Access 2000)

    I know there is a way to do this - but I can't figure out the examples I've seen... I'm looking for a way to call a Common Dialog window so a user can call a file... When a user selects a file I'd like the path/filename entered into a field box on my form... Does anyone have a good example of this or a link to a good example of this?

  2. #2
    5 Star Lounger
    Join Date
    Jan 2001
    Vancouver, Br. Columbia, Canada
    Thanked 0 Times in 0 Posts

    Re: Browsing w/ Common Dialog Window (Access 2000)

    You can register the common dialog control with Access, but I've run into problems with different computers having different versions of the common controls. As an alternative, you can create a Class Module that is completely "internal" to Access, and does not have the registration problems. Here is the code that I've been using lately instead of using the Common Control -- it's not fully developed, but you may find it useful. Sorry to the original author -- I don't have the name anywhere, so I can't properly attribute it to the original writer.

    *********** example usage

    Sub test()
    Dim dlg As New cDialog

    dlg.ModeOpen = True
    dlg.Filter = "Text files|*.txt|Old files|*.old|All files|*.*"
    MsgBox dlg.FileName & vbCrLf & dlg.FileDir & vbCrLf & dlg.FileTitle & vbCrLf & dlg.FileRoot & vbCrLf & dlg.FileExt

    End Sub

    ****** end of example

    **** create new class module named cDialog, and paste the following code into it.

    Option Compare Database
    Option Explicit

    Private Type OPENFILENAME
    lStructSize As Long
    hwnd 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

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

    Private Const OFN_FILEMUSTEXIST = &H1000
    Private Const OFN_HIDEREADONLY = &H4
    Private Const OFN_OVERWRITEPROMPT = &H2
    Private Const OFN_PATHMUSTEXIST = &H800
    Private Const OFN_SAVE = 0
    Private Const OFN_OPEN = 1

    Private lngHwnd As Long
    Private wMode As Integer
    Private szDialogTitle As String
    Private szFileName As String
    Private szFilter As String
    Private szDefDir As String
    Private szDefExt As String
    Private szFileTitle As String
    Private szFileDir As String
    Private intFilterIndex As Integer
    Public Function Action() As String
    Dim x As Long, OFN As OPENFILENAME
    Call SetDefs
    With OFN
    .lStructSize = Len(OFN)
    .hwnd = lngHwnd
    .lpstrTitle = szDialogTitle
    .lpstrFile = szFileName & VBA.String$(250 - Len(szFileName), 0)
    .nMaxFile = 255
    .lpstrFileTitle = VBA.String$(255, 0)
    .nMaxFileTitle = 255
    .lpstrFilter = szFilter
    .nFilterIndex = intFilterIndex
    .lpstrInitialDir = szDefDir
    .lpstrDefExt = szDefExt
    If wMode = 1 Then
    x = GetOpenFileName(OFN)
    x = GetSaveFileName(OFN)
    End If
    If x <> 0 Then
    If InStr(.lpstrFile, VBA.Chr$(0)) > 0 Then
    szFileName = VBA.Left$(.lpstrFile, InStr(.lpstrFile, VBA.Chr$(0)) - 1)
    szFileTitle = VBA.Left$(.lpstrFileTitle, InStr(.lpstrFileTitle, VBA.Chr$(0)) - 1)
    Call getFile_Dir
    End If
    szFileName = ""
    End If
    End With
    Action = szFileName
    End Function
    'Pass a bar separated string and returns a Null separated string
    Private Function NullSepString(ByVal BarString As String) As String
    Dim intInstr As Integer
    Const vbBar = "|"
    intInstr = InStr(BarString, vbBar)
    If intInstr > 0 Then Mid(BarString, intInstr, 1) = vbNullChar
    Loop While intInstr > 0
    NullSepString = BarString
    End Function
    Private Sub getFile_Dir()
    Dim intInstr As Integer
    intInstr = InStr(szFileName, szFileTitle) - 1
    szFileDir = VBA.Left(szFileName, intInstr)
    End Sub

    Property Let hwnd(SourceHwnd As Long)
    lngHwnd = SourceHwnd
    End Property

    Property Let ModeOpen(DialogMode As Boolean)
    wMode = DialogMode * -1
    End Property
    Property Let Title(DialogTitle As String)
    szDialogTitle = DialogTitle
    End Property
    Property Let FileName(DefaultFile As String)
    szFileName = DefaultFile
    End Property
    Property Get FileName() As String
    FileName = szFileName
    End Property
    Property Let Filter(FilterList As String)
    szFilter = NullSepString(FilterList)
    End Property
    Property Let StartDir(InitialDir As String)
    szDefDir = InitialDir
    End Property
    Property Let DefaultExtension(DefExt As String)
    szDefExt = DefExt
    End Property
    Property Get FileTitle()
    FileTitle = szFileTitle
    End Property
    Property Get FileDir() As String
    FileDir = szFileDir
    End Property
    Private Sub SetDefs()
    If lngHwnd = 0 Then lngHwnd = 0
    If szFilter = "" Then szFilter = NullSepString("All Files|*.*")
    If szDefDir = "" Then szDefDir = "C:"
    If intFilterIndex = 0 Then intFilterIndex = 1
    End Sub

    Property Get FileExt() As String
    FileExt = Mid(szFileTitle, LastBackslash(szFileTitle, ".") + 1)
    End Property

    Property Get FileRoot() As String
    FileRoot = Left(szFileTitle, LastBackslash(szFileTitle, ".") - 1)
    End Property

    Function LastBackslash(sSearch As String, Optional psSearchFor As String = "") As Integer
    Dim iCurrent As Integer
    Dim iLast As Integer
    iCurrent = 0
    iLast = 0
    iCurrent = InStr(iCurrent + 1, sSearch, psSearchFor)
    Do While iCurrent <> 0
    iLast = iCurrent
    iCurrent = InStr(iCurrent + 1, sSearch, psSearchFor)
    LastBackslash = iLast
    End Function
    Jack MacDonald
    Vancouver, Canada

  3. #3
    5 Star Lounger
    Join Date
    Nov 2001
    Jerusalem, Israel
    Thanked 1 Time in 1 Post

    Re: Browsing w/ Common Dialog Window (Access 2000)

    you can find the CommonDLg code on the internet.

    It is written by From Access 2000 Developer's Handbook, Volume I ' by Getz, Litwin, and Gilbert. (Sybex)
    ' Copyright 1999. All rights reserved.
    ' Provide access to the File Open/Save,
    ' Color and Font common dialogs.
    ' Works similarly to the CommonDlg
    ' ActiveX control, but adds more features,
    ' and doesn't implement Printer or Help
    ' support.

  4. #4
    Plutonium Lounger
    Join Date
    Dec 2000
    Sacramento, California, USA
    Thanked 1 Time in 1 Post

    Re: Browsing w/ Common Dialog Window (Access 2000)

    Among other places, you can find code for this here, on The Access Web. The code was written for 97 but will work just fine with later versions of Access.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts