CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2001
    Posts
    17

    Browse files and select in Access 97

    In my module, rather than asking the user to type in the file
    path and directory, I want the user to be able to browse their computer and be
    able to select the file that they want. Similar to in most programs where when
    you click on "open" one can browse through their entire computer with the
    treeview system. For example, say a file is located in C:\My Documents\abc.txt,
    When I click on a button, i should be able to navigate through C:\, select My
    documents, which would open up that folder and display all of its contents, and
    then finally select the file abc.txt which is the file that I ultimately
    wanted. Can anyone help me out with this?..I hope I was clear enough


  2. #2
    Join Date
    Apr 2001
    Posts
    23

    Re: Browse files and select in Access 97

    You need to study/research the MS Common Dialog
    component, the following shows how to open MS Access Databases in the C:\My Documents directory.
    You need a form with a command button, cmdOpenDB,
    and a Common Dialog Control, dlgCommon, you will have to manually load the control from the components tool bar in the VB IDE.


    private Sub cmdOpenDB_Click()


    Dim strCheckForDatabase as string

    on error GoTo HandleErrors

    dlgCommon.DialogTitle = "Pick A Database"
    'Give the file selection window a title.

    dlgCommon.InitDir = "C:\My Documents" 'App. Path
    'The file selection window will start in the
    'applications directory.

    'Allow the user to view only Access files.
    dlgCommon.Filter = "Access Databases (*.mdb)|*.mdb|" & _
    "All Files (*.*)|*.*"
    dlgCommon.ShowOpen
    'Open the file selection window.

    strCheckForDatabase = Right(dlgCommon.FileName, 4)
    'Select the last four letters of the file selected.

    Select Case strCheckForDatabase
    Case vbNullString
    'Do not allow empty strings.
    Exit Sub

    Case ".mdb"
    'Assign the chosen file to the path string.
    mstrDatabasePath = dlgCommon.FileName
    'Do not allow the user to select another DB until
    'clear is clicked.
    cmdOpenDB.Enabled = false

    End Select






  3. #3
    johnpc7 Guest

    Re: Browse files and select in Access 97

    You need to study/research the MS Common Dialog
    component, the following shows how to open MS Access Databases in the C:\My Documents directory.
    You need a form with a command button, cmdOpenDB,
    and a Common Dialog Control, dlgCommon, you will have to manually load the control from the components tool bar in the VB IDE.


    private Sub cmdOpenDB_Click()


    Dim strCheckForDatabase as string

    on error GoTo HandleErrors

    dlgCommon.DialogTitle = "Pick A Database"
    'Give the file selection window a title.

    dlgCommon.InitDir = "C:\My Documents" 'App. Path
    'The file selection window will start in the
    'applications directory.

    'Allow the user to view only Access files.
    dlgCommon.Filter = "Access Databases (*.mdb)|*.mdb|" & _
    "All Files (*.*)|*.*"
    dlgCommon.ShowOpen
    'Open the file selection window.

    strCheckForDatabase = Right(dlgCommon.FileName, 4)
    'Select the last four letters of the file selected.

    Select Case strCheckForDatabase
    Case vbNullString
    'Do not allow empty strings.
    Exit Sub

    Case ".mdb"
    'Assign the chosen file to the path string.
    mstrDatabasePath = dlgCommon.FileName
    'Do not allow the user to select another DB until
    'clear is clicked.
    cmdOpenDB.Enabled = false

    End Select






  4. #4
    Join Date
    Jul 2001
    Posts
    17

    Re: Browse files and select in Access 97

    FOR FUTURE REFERENCE:
    if anyone needs code for this type of problem go to this site:
    http://www.calvinsmithsoftware.com/SelectDiskFile.htm

    It saved my life...well not really but it's exactly what i was looking for.


  5. #5
    Join Date
    Oct 2000
    Posts
    58

    Re: Browse files and select in Access 97

    This is a nice one

    Private Type BrowseInfo
    hWndOwner As Long
    pIDLRoot As Long
    pszDisplayName As Long
    lpszTitle As Long
    ulFlags As Long
    lpfnCallback As Long
    lParam As Long
    iImage As Long
    End Type

    Const BIF_RETURNONLYFSDIRS = 1
    Const MAX_PATH = 260
    Private Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long)
    Private Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" (ByVal lpString1 As String, ByVal lpString2 As String) As Long
    Private Declare Function SHBrowseForFolder Lib "shell32" (lpbi As BrowseInfo) As Long
    Private Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList As Long, ByVal lpBuffer As String) As Long



    Private Sub GetFolderPath()
    'KPD-Team 1998
    'URL: http://www.allapi.net/
    '[email protected]
    Dim iNull As Integer, lpIDList As Long, lResult As Long
    Dim sPath As String, udtBI As BrowseInfo

    With udtBI
    'set the owner window
    '.hWndOwner = Me.hWnd
    'lstrcat appends the two strings and returns the memory address
    .lpszTitle = lstrcat("Default folder: " & GetSetting("TAC", "User Info", "Library Location"), "")
    'Return only if the user selected a directory
    .ulFlags = BIF_RETURNONLYFSDIRS
    End With

    'Show the 'Browse for folder' dialog
    lpIDList = SHBrowseForFolder(udtBI)
    If lpIDList Then
    sPath = String$(MAX_PATH, 0)
    'get the path from the IDList
    SHGetPathFromIDList lpIDList, sPath
    'free the block of memory
    CoTaskMemFree lpIDList
    iNull = InStr(sPath, vbNullChar)
    If iNull Then
    sPath = Left$(sPath, iNull - 1)
    End If
    End If

    MsgBox sPath

    End Sub


  6. #6
    Join Date
    Nov 2000
    Location
    Toronto, Canada
    Posts
    12
    In the code (please, see previuos message) I would like to specify/code some action in case user presses a "Cancel" button on the 'Browse for folder' dialog. How (where in the code) can I do that?

    Thanks a lot!
    Eugene.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured