Automatically create folder

B

Blakey300

Hi

I am using access 2007 and i have the following code:

Private Sub Command160_Click()
Dim strDesktop As String
Dim strSubfolder As String
strDesktop = "C:\Users\Dave Blake\Documents\Keibudo Karate\Student Documents\"
strSubfolder = Me![LastName] & " " & Me![FirstName]
If Len(Dir(strDesktop & strSubfolder, vbDirectory)) > 0 Then
MsgBox "ERROR: THE FOLDER ALREADY EXISTS"
Else
MkDir strDesktop & strSubfolder
End If
End Sub

This code is attached to a button on my form, once i click the button it
checks to see if there is a folder for that record, and if not it creates
one, which works great however is it possible to change this so that it will
do this for all records on the form and not just the current one.

Sorry I am a bit of a thicko when it comes to VBA coding so any help would
be appricated.

Regards

Dave
 
S

Stefan Hoffmann

hi Dave,
This code is attached to a button on my form, once i click the button it
checks to see if there is a folder for that record, and if not it creates
one, which works great however is it possible to change this so that it will
do this for all records on the form and not just the current one.
Create a method from your piece of code, it's quite easy to do. E.g.

Private Sub cmdMakeAllFolders_Click()

Dim rs As DAO.Recordset

Set rs = Me.RecordsetClone
If Not rs.Bof And Not rs.Eof Then
Do While Not rs.Eof
CreateFolder rs![LastName] & " " & rs![FirstName]
rs.MoveNext
Loop
End If
rs.Close
Set rs = Nothing

End Sub

Private Sub CreateFolder(ASubFolder As String)

Dim strDesktop As String
Dim strSubfolder As String

strDesktop = "C:\Users\Dave Blake\Documents\" & _
"Keibudo Karate\Student Documents\"

If Len(Dir(strDesktop & ASubfolder, vbDirectory)) = 0 Then
MkDir strDesktop & ASubfolder
End If

End Sub


But beware of the fact that your name fields may contain characters that
are not allowed in a folder name. So you need still to do some error
handling.


mfG
--> stefan <--
 
B

Blakey300

thanks stefan

Must confess hadn't thought about that, think I will use my primary key as
the folder name instead to eliminate any future issues.

Dave

Stefan Hoffmann said:
hi Dave,
This code is attached to a button on my form, once i click the button it
checks to see if there is a folder for that record, and if not it creates
one, which works great however is it possible to change this so that it will
do this for all records on the form and not just the current one.
Create a method from your piece of code, it's quite easy to do. E.g.

Private Sub cmdMakeAllFolders_Click()

Dim rs As DAO.Recordset

Set rs = Me.RecordsetClone
If Not rs.Bof And Not rs.Eof Then
Do While Not rs.Eof
CreateFolder rs![LastName] & " " & rs![FirstName]
rs.MoveNext
Loop
End If
rs.Close
Set rs = Nothing

End Sub

Private Sub CreateFolder(ASubFolder As String)

Dim strDesktop As String
Dim strSubfolder As String

strDesktop = "C:\Users\Dave Blake\Documents\" & _
"Keibudo Karate\Student Documents\"

If Len(Dir(strDesktop & ASubfolder, vbDirectory)) = 0 Then
MkDir strDesktop & ASubfolder
End If

End Sub


But beware of the fact that your name fields may contain characters that
are not allowed in a folder name. So you need still to do some error
handling.


mfG
--> stefan <--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top