Question Looping Records to Create Directories (1 Viewer)

Tim Bedborough

Registered User.
Local time
Today, 10:51
Joined
Nov 16, 2015
Messages
42
Hi all, usual apology in case this has been done before (I'm sure it has!).

I have a recordset (the table is called tblJobTitle) and I want to loop through all the records and create a windows folder (MkDir) in my P directory with the name of the field/control [JobTitle].

This is what I have:

Private Sub Test_Click()

Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("SELECT * FROM tblJobTitle")

Do Until rs.EOF
MkDir "P:" & Me.JobTitle
rs.MoveNext
Loop

End Sub

It will create the first record but then errors with a run time error 75 saying Path/File access error.

I have tried searching the blogs and the above looks OK to me but obviously not.

Help please, thanks.
 

Tim Bedborough

Registered User.
Local time
Today, 10:51
Joined
Nov 16, 2015
Messages
42
Oops sorry, MkDir "P:" & Me.JobTitle

I do have the slash in there just pasted it wrong
 

moke123

AWF VIP
Local time
Today, 06:51
Joined
Jan 11, 2013
Messages
3,920
Code:
Private Sub Test_Click()

Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("SELECT * FROM tblJobTitle")

Do Until rs.EOF
MkDir "P:" & Me.JobTitle
rs.MoveNext
Loop

End Sub
Where in your loop is the recordset field? Me.JobTitle is a form object.
I would also test for existing directory before making a new one.
 

vba_php

Forum Troll
Local time
Today, 05:51
Joined
Oct 6, 2019
Messages
2,880
how bout this?
Code:
Private Sub Test_Click()

    Dim rs As DAO.Recordset
    Set rs = CurrentDb.OpenRecordset("SELECT * FROM tblJobTitle")
             
    Do Until rs.EOF
          rs.movelast
          rs.movefirst
              if dir("P:\" & me.jobtitle.name) = "" then
                 MkDir "P:" & Me.JobTitle.name (".name" gives you the CONTROL name on the form.  without ".name", you get the value that's in that control at the time this code runs).
              end if
        rs.MoveNext
    Loop
        
End Sub
 

Gasman

Enthusiastic Amateur
Local time
Today, 11:51
Joined
Sep 21, 2011
Messages
14,311
Oops sorry, MkDir "P:" & Me.JobTitle

I do have the slash in there just pasted it wrong

This site removes "" unless it is in code or single quotes '\' :D

'P:\' & [YourfolderPath]
Code:
\
 

Tim Bedborough

Registered User.
Local time
Today, 10:51
Joined
Nov 16, 2015
Messages
42
Hi moke. I'm running the vba from a button on the form that houses the data. The form is a simple 2 field continuous form taken from the table tblJobTitle. Field 1 is the primary key and field 2 is called JobTitle. I tried using RecordsetClone and setting variables for directory name etc but everything ends up with not being able to move onto the next record in the table. I'm not sure this is what you are asking but it's about all I have unless you can make it a bit more simple please. Thanks.
 

moke123

AWF VIP
Local time
Today, 06:51
Joined
Jan 11, 2013
Messages
3,920
More along these lines
Code:
Private Sub Test_Click()

    Dim rs As DAO.Recordset
    Dim DPath As String

    Set rs = CurrentDb.OpenRecordset("SELECT * FROM tblJobTitle")

    Do Until rs.EOF

        DPath = "P:\" & rs!JobTitle

        Debug.Print DPath

        If Len(Dir(DPath, vbDirectory)) = 0 Then
            MkDir DPath
        End If

        rs.MoveNext
    Loop

End Sub

@Adam
wouldnt this be an endless loop? your moving first on each iteration.
Code:
    Do Until rs.EOF
          rs.movelast
          rs.movefirst
              if dir("P:\" & me.jobtitle.name) = "" then
                 MkDir "P:" & Me.JobTitle.name (".name" gives you the CONTROL name on the form.  without ".name", you get the value that's in that control at the time this code runs).
              end if
        rs.MoveNext
    Loop
 

Users who are viewing this thread

Top Bottom