Question : File or Folder Selection

I am using the following code to allow a user to select a file. The file is then amongst other things copied to another server. This code allows the user to select only files to be processed.

I would like to the able to give the user the ability to select a file or a folder from the same dialogue box. If the user selects a file then the file will be processed. If the user selects a folder then the entire folder will be looped through and processed.

Currently the file open dialogue box has an 'Open' button. When the user selects a folder and clicks the open button, that folder is opened within the dialogue box rather than passing the string back to access (as it does when you select a file and click open).

And ideas on how to achieve this would be greatly appreciated.

James.

    Dim strFilter As String
    Dim lngFlags As Long
    Dim varFileName As Variant

    strFilter = "All Files (*.*)" & vbNullChar & "*.*"

    lngFlags = tscFNPathMustExist Or tscFNFileMustExist Or tscFNHideReadOnly

    varFileName = tsGetFileFromUser( _
    fOpenFile:=True, _
    strFilter:=strFilter, _
    rlngflags:=lngFlags, _
    strDialogTitle:="Find File (Select The File And Click The Open Button)")

    If IsNull(varFileName) Or varFileName = "" Then
        Debug.Print "User pressed 'Cancel'."
        Beep
        MsgBox "File selection was canceled.", vbInformation
        Exit Sub
    Else
        selectedFile = varFileName
    End If



Global Functions
-------------------
Private Declare Function ts_apiGetOpenFileName Lib "comdlg32.dll" _
 Alias "GetOpenFileNameA" (tsFN As tsFileName) As Boolean

Private Declare Function ts_apiGetSaveFileName Lib "comdlg32.dll" _
 Alias "GetSaveFileNameA" (tsFN As tsFileName) As Boolean

Private Declare Function CommDlgExtendedError Lib "comdlg32.dll" () As Long

Private Type tsFileName
   lStructSize As Long
   hwndOwner As Long
   hInstance As Long
   strFilter As String
   strCustomFilter As String
   nMaxCustFilter As Long
   nFilterIndex As Long
   strFile As String
   nMaxFile As Long
   strFileTitle As String
   nMaxFileTitle As Long
   strInitialDir As String
   strTitle As String
   Flags As Long
   nFileOffset As Integer
   nFileExtension As Integer
   strDefExt As String
   lCustData As Long
   lpfnHook As Long
   lpTemplateName As String
End Type

' Flag Constants
Public Const tscFNAllowMultiSelect = &H200
Public Const tscFNCreatePrompt = &H2000
Public Const tscFNExplorer = &H80000
Public Const tscFNExtensionDifferent = &H400
Public Const tscFNFileMustExist = &H1000
Public Const tscFNPathMustExist = &H800
Public Const tscFNNoValidate = &H100
Public Const tscFNHelpButton = &H10
Public Const tscFNHideReadOnly = &H4
Public Const tscFNLongNames = &H200000
Public Const tscFNNoLongNames = &H40000
Public Const tscFNNoChangeDir = &H8
Public Const tscFNReadOnly = &H1
Public Const tscFNOverwritePrompt = &H2
Public Const tscFNShareAware = &H4000
Public Const tscFNNoReadOnlyReturn = &H8000
Public Const tscFNNoDereferenceLinks = &H100000

Public Function tsGetFileFromUser( _
 Optional ByRef rlngflags As Long = 0&, _
 Optional ByVal strInitialDir As String = "", _
 Optional ByVal strFilter As String = "All Files (*.*)" & vbNullChar & "*.*", _
 Optional ByVal lngFilterIndex As Long = 1, _
 Optional ByVal strDefaultExt As String = "", _
 Optional ByVal strFileName As String = "", _
 Optional ByVal strDialogTitle As String = "", _
 Optional ByVal fOpenFile As Boolean = True) As Variant
   
   On Error GoTo tsGetFileFromUser_Err
   Dim tsFN As tsFileName
   Dim strFileTitle As String
   Dim fResult As Boolean

   strFileName = Left(strFileName & String(256, 0), 256)
   strFileTitle = String(256, 0)


   With tsFN
      .lStructSize = Len(tsFN)
      .hwndOwner = Application.hWndAccessApp
      .strFilter = strFilter
      .nFilterIndex = lngFilterIndex
      .strFile = strFileName
      .nMaxFile = Len(strFileName)
      .strFileTitle = strFileTitle
      .nMaxFileTitle = Len(strFileTitle)
      .strTitle = strDialogTitle
      .Flags = rlngflags
      .strDefExt = strDefaultExt
      .strInitialDir = strInitialDir
      .hInstance = 0
      .strCustomFilter = String(255, 0)
      .nMaxCustFilter = 255
      .lpfnHook = 0
   End With
   

   If fOpenFile Then
      fResult = ts_apiGetOpenFileName(tsFN)
   Else
      fResult = ts_apiGetSaveFileName(tsFN)
   End If


   If fResult Then
      rlngflags = tsFN.Flags
      tsGetFileFromUser = tsTrimNull(tsFN.strFile)
   Else
      tsGetFileFromUser = Null
   End If

tsGetFileFromUser_End:
   On Error GoTo 0
   Exit Function

tsGetFileFromUser_Err:
   Beep
   MsgBox Err.description, , "Error: " & Err.number _
    & " in function basBrowseFiles.tsGetFileFromUser"
   Resume tsGetFileFromUser_End

End Function

Answer : File or Folder Selection

i guess you have to ask the user first whether a single file or a whole dir should be processed, then---in the latter case---present the dir-selection box (http://www.mvps.org/access/api/api0002.htm).  the point is, the file selection box is a windows function, and i'd assume it's pretty complex to modify this for your needs...


--bluelizard
Random Solutions  
 
programming4us programming4us