Solved

Convert VB Code to use in Access

Posted on 2011-03-03
5
314 Views
Last Modified: 2012-05-11
Hi,

i am currently using the below code in excel to get a list of files which are on one of my drives.

In access i have created a table with all the headers that are getting created in the Sub List_Of_Files_In_Folder(). Can someone help me build this code to work in access?

Sub List_Of_Files_In_Folder()
    'add headers
    Sheets("Files on Drive").Select
    Columns("A:L").Delete
    Range("A1").Formula = "Current File Path:"
    Range("B1").Formula = "Destination File Path:"
    Range("C1").Formula = "Parent Folder:"
    Range("D1").Formula = "File Name:"
    Range("E1").Formula = "File Size:"
    Range("F1").Formula = "File Type:"
    Range("G1").Formula = "Date Created:"
    Range("H1").Formula = "Date Last Accessed:"
    Range("I1").Formula = "Date Last Modified:"
    Range("J1").Formula = "Attributes:"
    Range("K1").Formula = "Short File Name:"
    Range("L1").Formula = "Archiving Status (Yes/No):"
    Range("A1:L1").Font.Bold = True
    ListFilesInFolder "I:\", True
End Sub

Sub ListFilesInFolder(SourceFolderName As String, IncludeSubfolders As Boolean)
Dim FSO As Scripting.FileSystemObject
Dim SourceFolder As Scripting.Folder, SubFolder As Scripting.Folder
Dim FileItem As Scripting.File
Dim r As Long
    Set FSO = New Scripting.FileSystemObject
    Set SourceFolder = FSO.GetFolder(SourceFolderName)
    r = Range("A65536").End(xlUp).Row + 1
    For Each FileItem In SourceFolder.Files
        Cells(r, 1).Formula = FileItem.Path
        Cells(r, 2).Formula = "D:\Archiving " & Left(FileItem.Path, 1) & " Drive" & Mid(FileItem.Path, 3, (Len(FileItem.Path)))
        Cells(r, 3).Formula = Mid(FileItem.Path, 4, (InStr(4, FileItem.Path, "\") - 4))
        Cells(r, 4).Formula = FileItem.Name
        Cells(r, 5).Formula = FileItem.Size
        Cells(r, 6).Formula = FileItem.Type
        Cells(r, 7).Formula = FileItem.DateCreated
        Cells(r, 8).Formula = FileItem.DateLastAccessed
        Cells(r, 9).Formula = FileItem.DateLastModified
        Cells(r, 10).Formula = FileItem.Attributes
        Cells(r, 11).Formula = FileItem.ShortPath & FileItem.ShortName
        r = r + 1
    Next FileItem
    If IncludeSubfolders Then
        For Each SubFolder In SourceFolder.SubFolders
            ListFilesInFolder SubFolder.Path, True
        Next SubFolder
    End If
    Columns("A:L").AutoFit
    Set FileItem = Nothing
    Set SourceFolder = Nothing
    Set FSO = Nothing
    ActiveWorkbook.Saved = True
    Cells(1, 1).Select
End Sub
0
Comment
Question by:Saichand
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 35032347
Hi, maybe this will work.  You'll need to check your table and field names.

Regards,

Rob.
Sub List_Of_Files_In_Folder()
    ListFilesInFolder "I:\", True
End Sub

Sub ListFilesInFolder(SourceFolderName As String, IncludeSubfolders As Boolean)
Dim FSO As Scripting.FileSystemObject
Dim SourceFolder As Scripting.Folder, SubFolder As Scripting.Folder
Dim FileItem As Scripting.File
Dim r As Long
    Set FSO = New Scripting.FileSystemObject
    Set SourceFolder = FSO.GetFolder(SourceFolderName)
    For Each FileItem In SourceFolder.Files
    	CurrentDb.Execute "INSERT INTO MyTable (CurrentFilePath,DestinationFilePath,ParentFolder,FileName,FileSize,FileType,DateCreated,DateLastAccessed,DateLastModified,Attributes,ShortFileName) " & _
    		"VALUES (" & _
    		"'" & FileItem.Path & "'," & _
    		"'" & "D:\Archiving " & Left(FileItem.Path, 1) & " Drive" & Mid(FileItem.Path, 3, (Len(FileItem.Path))) & "'," & _
    		"'" & Mid(FileItem.Path, 4, (InStr(4, FileItem.Path, "\") - 4)) & "'," & _
    		"'" & FileItem.Name & "'," & _
    		"'" & FileItem.Size & "'," & _
    		"'" & FileItem.Type & "'," & _
    		"'" & FileItem.DateCreated & "'," & _
    		"'" & FileItem.DateLastAccessed & "'," & _
    		"'" & FileItem.DateLastModified & "'," & _
    		"'" & FileItem.Attributes & "'," & _
    		"'" & FileItem.ShortPath & FileItem.ShortName & "'" & _
    		")"
    Next FileItem
    If IncludeSubfolders Then
        For Each SubFolder In SourceFolder.SubFolders
            ListFilesInFolder SubFolder.Path, True
        Next SubFolder
    End If
    Set FileItem = Nothing
    Set SourceFolder = Nothing
    Set FSO = Nothing
End Sub

Open in new window

0
 

Author Comment

by:Saichand
ID: 35037506
it worked perfectly fine... thanks a lot...

i have another question... in the belew piece of code, how would i give referance of a text box on a form in place of I:\

Sub List_Of_Files_In_Folder()
    ListFilesInFolder "I:\", True
End Sub

0
 

Author Comment

by:Saichand
ID: 35037983
nevermind i got it.. thanks..
0
 

Author Closing Comment

by:Saichand
ID: 35037992
I get the perfect solution from RobSampson
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 35045097
Thanks for the grade.

Regards,

Rob.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question