Results 1 to 8 of 8
  1. #1
    Join Date
    Mar 2003
    Location
    UK
    Posts
    85

    Question Unanswered: How can I select File Folders using this code?

    Dear all

    I am using following code to get path of file from a specific folder.

    This code only allows me to select files not folders. Can you please tell me what changes I should make in this code so that it would allow me to select File folders as well?

    Thanks


    Option Compare Database

    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:\DataFolder\Import Files\"
    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"
    Else
    LaunchCD = Trim(OpenFile.lpstrFile)
    End If
    End Function

  2. #2
    Join Date
    Sep 2004
    Posts
    161
    I think it's not possible with your code.
    You can find the solution here:
    http://www.mvps.org/access/api/api0002.htm
    Last edited by jepi; 09-09-04 at 08:33.

  3. #3
    Join Date
    Mar 2003
    Location
    UK
    Posts
    85
    Thank you for your reply.

    But this would only let you to browse a folder but what if you want to link to a file like .jpg, or .xls ect in the same parent folder.

    I want to be able to link to a folder (open in the browser) and to a file within the same parent main folder.

    Thanks

  4. #4
    Join Date
    Sep 2004
    Posts
    161
    sorry, I does not understand what you want to do. My
    English is too poor

  5. #5
    Join Date
    Mar 2003
    Location
    UK
    Posts
    85
    I explain my question here a bit more.

    I want to save the file paths in my database table
    I am running the above code by OnClick event of a command button
    On the click of this command button, a window opens with the location specified in code.
    I select a photo file. The path of that photo file is then stored in a text box on the same form.

    The above code works fine if I just do the above. But the problem is that there are situations when there are 3-4 photos for same record, I put those photos in a folder and save the folder in the same main folder where there are other photo files.

    Now by using above code, I click on command button and then I select just the folder - I do not want to get the path of the photo files inside this folder but only the path of folder.

    But at the same time I need to be able to get the file path of singel photo files as well.

    Have I been able to explain myself or not? I am not sure.

  6. #6
    Join Date
    Sep 2004
    Posts
    161
    I understands better your problem.
    I dont know if that can be useful to you, but there is a API allowing the multiple selection of files.

  7. #7
    Join Date
    Mar 2003
    Location
    UK
    Posts
    85
    Quote Originally Posted by jepi
    I understands better your problem.
    I dont know if that can be useful to you, but there is a API allowing the multiple selection of files.
    API ???? No clue

    Please explain

  8. #8
    Join Date
    Sep 2004
    Posts
    161
    Is not my code :
    ' Use the Office file selector common dialog exposed by Access.
    ' From Access 97 Developer's Handbook' by Litwin, Getz, and Gilbert (Sybex)'
    Module DirListBox
    Code:
    Option Compare Database
    Option Explicit
    
    'General Errors
    Public Const adhcAccErrSuccess = 0
    Public Const adhcAccErrUnknown = -1
    ' FileExists return values
    Public Const adhcFileExistsYes = 1
    Public Const adhcFileExistsNo = 0
    ' Common Dialogs'
    ' GetFileName errors
    Public Const adhcAccErrGFNCantOpenDialog = -301
    Public Const adhcAccErrGFNUserCancelledDialog = -302
    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
    ' API declaration
    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
    Function adhHandleAccErrors(intErr As Integer) As Boolean
    
    ' A generic error handler for Access function errors.
    ' From Access 97 Developer's Handbook
    ' by Litwin, Getz, and Gilbert (Sybex)
    ' Copyright 1997. All rights reserved.
    ' In:
    ' intErr: the error number
    ' Out:
    ' Return value: True if this function handled the error,
    ' False if it couldn't.
    Dim strMsg As String
    Dim fOK As Boolean
    fOK = True
    Select Case intErr
    Case adhcAccErrUnknown
    strMsg = "Unknown error"
    ' GetFileName errors
    Case adhcAccErrGFNCantOpenDialog
    strMsg = "Can't open common dialog"
    Case adhcAccErrGFNUserCancelledDialog
    strMsg = "User cancelled dialog"
    Case Else
    fOK = False
    End Select
    If fOK Then
    MsgBox strMsg, vbExclamation, "Error in Acc7032.DLL"
    End If
    adhHandleAccErrors = fOK
    End Function
    Module SelectFile
    Code:
    Function Recherche_fic(strRowSrc)
    
    '***********************************************************************************************************
    ' PURPOSE: Browse files to create link
    '***********************************************************************************************************
    On Error GoTo Err_Recherche_fic
    Dim lngFlags As Long
    Dim gfni As adh_accOfficeGetFileNameInfo
    'Me!txtFileList = ""
    'Initiate call to Windoze browser to get filenames:
    With gfni
        .lngView = 0
        'show details
        .lngFlags = &H40 Or &H8
        'initialize view and allow multiple file selection
        .strFilter = "Fichier Texte  (*.csv)"
        .lngFilterIndex = 0
        .strFile = ""
        .strDlgTitle = "Selectionner le fichier  importer"
        .StrOpenTitle = "Selectionner"
        .strInitialDir = "D:\"
    End With
    
    If adhOfficeGetFileName(gfni, 1) <> adhcAccErrSuccess Then
        Exit Function
    End If
    
    'Set up RowSource for list box:
    strRowSrc = Trim(gfni.strFile)
    
    If Len(strRowSrc) > 2048 Then GoTo cmdBrowse_TooLong
    TabLoop:
    'if any tabs in RowSource, replace with ';' (required to separate elements in RowSource)
    If InStr(1, strRowSrc, Chr(9)) <> 0 Then
        strRowSrc = Left(strRowSrc, InStr(1, strRowSrc, Chr(9)) - 1) _
            & Chr(59) _
            & Right(strRowSrc, Len(strRowSrc) - InStr(1, strRowSrc, Chr(9)))
        GoTo TabLoop
    End If
    'Me!txtFileList.RowSource = strRowSrc
    Exit_Recherche_fic:
        Exit Function
    Err_Recherche_fic:
    ' Call adhHandleAccErrors(Application.CurrentObjectName, "Recherche_fic", Err.Number, Err.Description)
    Resume Exit_Recherche_fic
    cmdBrowse_TooLong:
    Beep
    MsgBox "Too many files selected!" _
    & vbCr & vbCr & "You have exceeded Microsoft Access" & vbCr _
    & "limitations for handling file selections." & vbCr & vbCr _
    & "Please select fewer filenames." _
    & vbCr & vbCr & "Click OK to acknowledge." & vbCr & vbCr & "", 48
    Exit Function
    End Function
    In a form call the function Recherche_fic(strRowSrc)

Posting Permissions

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