Public Type BROWSEINFO
hOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type
'32-bit API declarations
Declare Function SHGetPathFromIDList Lib "shell32.dll" _
Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long
Declare Function SHBrowseForFolder Lib "shell32.dll" _
Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long
Public Function ReturnAllFiles(Optional ByVal selDir As String) As Boolean
Dim DirName As String
Dim TempName As String, FileNum As Integer
Dim dbs As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL, strTBL As String
On Error GoTo HandleErr
strTBL = "tblFiles"
Set dbs = CurrentDb
If ObjectExists("Table", strTBL) Then
strTBL = "tblFiles"
strSQL = "DELETE * FROM " & strTBL
DoCmd.RunSQL strSQL
Else
' Create the Table
End If
'C:\DirectoryLocation
strSQL = "SELECT * FROM tblFiles"
Set rs = dbs.OpenRecordset(strSQL, dbOpenDynaset)
FileNum = FreeFile
If selDir <> "C:\" Then
DirName = selDir
Else
DirName = GetDirectory2() & "\"
If Len(DirName) = 0 Then
ReturnAllFiles = False
Exit Function
End If
End If
TempName = Dir$(DirName, vbDirectory)
While Len(TempName)
If (TempName <> ".") And (TempName <> "..") Then 'get rid of "." and ".."
TempName = DirName & TempName
'GetAttr is a built-in function
If GetAttr(TempName) <> vbDirectory Then
'Debug.Print TempName
rs.AddNew
rs.Fields(0).Value = TempName
rs.Update
End If
End If
TempName = Dir$
Wend
Close #FileNum
ReturnAllFiles = True
Set rs = Nothing
Set dbs = Nothing
End Function
Function ObjectExists(strObjectType As String, strObjectName As String) As Boolean
Dim db As Database
Dim tbl As TableDef
Dim qry As QueryDef
Dim i As Integer
Set db = CurrentDb()
ObjectExists = False
If strObjectType = "Table" Then
For Each tbl In db.TableDefs
If tbl.Name = strObjectName Then
ObjectExists = True
Exit Function
End If
Next tbl
ElseIf strObjectType = "Query" Then
For Each qry In db.QueryDefs
If qry.Name = strObjectName Then
ObjectExists = True
Exit Function
End If
Next qry
ElseIf strObjectType = "Form" Or strObjectType = "Report" Or strObjectType = "Module" Then
For i = 0 To db.Containers(strObjectType & "s").Documents.Count - 1
If db.Containers(strObjectType & "s").Documents(i).Name = strObjectName Then
ObjectExists = True
Exit Function
End If
Next i
ElseIf strObjectType = "Macro" Then
For i = 0 To db.Containers("Scripts").Documents.Count - 1
If db.Containers("Scripts").Documents(i).Name = strObjectName Then
ObjectExists = True
Exit Function
End If
Next i
Else
MsgBox "Invalid Object Type passed, must be Table, Query, Form, Report, Macro, or Module"
End If
End Function
Public Function GetDirectory2(Optional Msg) As String
Dim bInfo As BROWSEINFO
Dim path As String
Dim R As Long, x As Long
' Root folder = Desktop
bInfo.pidlRoot = 0&
' Title in the dialog
If IsMissing(Msg) Then
bInfo.lpszTitle = "Select a folder."
Else
bInfo.lpszTitle = Msg
End If
' Type of directory to return
bInfo.ulFlags = &H1
' Display the dialog
x = SHBrowseForFolder(bInfo)
' Parse the result
path = Space$(512)
R = SHGetPathFromIDList(ByVal x, ByVal path)
If R Then
x = InStr(path, Chr$(0))
GetDirectory2 = Left(path, x - 1)
Else
GetDirectory2 = ""
End If
End Function