Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create windows folders by looping through records 1

Status
Not open for further replies.

Newgrammer

Programmer
Jul 21, 2008
11
US
Hey guys,
What i'm trying to do is automate creating a large number of file folders by using Access. I already have created all of the "Parent" folders. What I need my code to then do is cycle through each record and create child folder within the parent folders that will be unique. So I will have a parent folder named 1 AA 1234 XX, and within that folder will be 1 AA 1234 01, 1 AA 1234 02 etc.
So each record in my tables have both a unique number, and a parent top level number.

The code I currently have is:
Code:
Function Folders()

Dim rs As DAO.Recordset
Dim fso, fldr
Dim strSQL, ParentLink, ChildLink As String
Set fso = CreateObject("Scripting.FileSystemObject")

   strSQL = "SELECT tblCombinedItemList.strFourthshift, tblCombinedItemList.strFourthshiftTopLevel, tblCombinedItemList.strEcometry, tblCombinedItemList.strEcometryTopLevel, tblCombinedItemList.strDesc, tblCombinedItemList.strDescTopLevel FROM tblCombinedItemList WHERE (((tblCombinedItemList.strEcometry) Is Not Null)) ORDER BY tblCombinedItemList.strEcometry;"

   Set rs = CurrentDb.OpenRecordset(strSQL, dbOpenSnapshot)
   ParentLink = ("S:\Home\Wes\Von Morris\Part Database\Files\" & rs!strEcometryTopLevel & " " & rs!strDescTopLevel)
   ChildLink = ("\" & rs!strEcometry & " " & rs!strDesc)
      
      
rs.MoveFirst
      
Do Until rs.EOF
    If fso.FolderExists(ParentLink & ChildLink) Then
        rs.MoveNext
        Else
        Set fldr = fso.CreateFolder(ParentLink & ChildLink)
    End If
        rs.MoveNext
Loop
    
rs.Close
Set rs = Nothing

End Function

Where I am stuck is that although the code runs fine and without errors, it does not cycle past one record. It creates one folder correctly, and that's it.

I'd love any advice anyone might have as I continue to tinker with it. I'm trying to get this code functional so down the line when i will have 20,000+ parts each with their own folders containing any relevant data.
 
My only ideas at this point is that the two variables ParentLink and ChildLink aren't updating as the recordset is changing to the next record. In which case it would make sense that it is only creating one folder.
 
Try:

Code:
<...>   
rs.MoveFirst
      
Do Until rs.EOF

[red]ParentLink = ("S:\Home\Wes\Von Morris\Part Database\Files\" & rs!strEcometryTopLevel & " " & rs!strDescTopLevel)
   ChildLink = ("\" & rs!strEcometry & " " & rs!strDesc)[/red]

    If fso.FolderExists(ParentLink & ChildLink) Then
        rs.MoveNext
        Else
        Set fldr = fso.CreateFolder(ParentLink & ChildLink)
    End If
        rs.MoveNext
Loop
    
rs.Close
Set rs = Nothing
 
That did the trick, thank you very much Remou! Love the easy fixes despite the "Duhhh!" feeling.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top