Solved

Convert VB Code to use in Access

Posted on 2011-03-03
5
322 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Suggested Courses

624 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