Check, Create and Open a Folder From a Button

ddrew

seasoned user
Local time
Today, 09:01
Joined
Jan 26, 2003
Messages
911
I have been playing aroiund with some code to open a folder, first it has to check to see if the folder exists, if it dosent, it has to create it. It works fine up to the point of opening the folder. When it opens it always opens My Documents rather than the path that I want.

This is my code:
Code:
Private Sub btn_EventPhotos_Click()
Dim x As Variant

If Len(Dir("C:\Databases\Event Photos\" & Me.ConcatinatedName, vbDirectory)) = 0 Then
MkDir ("C:\Databases\Event Photos\" & Me.ConcatinatedName)
End If

x = Shell("EXPLORER.EXE c:\Databases\" & "Me.ConcatinatedName", vbNormalFocus)


End Sub
 
Hi,

It may be that the folder doesn't exist... are you positive you've validated that correctly? (do another folder check after the MkDir to make sure, you might have write permission problems - additionally, a DoEvents statement in there might give the system an extra second to finish creating the folder, possibly the VBA is moving too fast).

hth
 
Seems to be missing \Event Photos\ and Me.ConcatinatedName is not equal to "Me.ConcatinatedName".

Chris.
 
There doesn't appear to be anything wrong in particular with your method, except that you're not validating the folder creation.

Have you debugged for that?
 
Seems to be missing \Event Photos\ and Me.ConcatinatedName is not equal to "Me.ConcatinatedName".

Chris.
:o Doh! Thats what comes of working at it till 10 at night! :o Should read:

Code:
Private Sub btn_EventPhotos_Click()
    Dim x As Variant

    If Len(Dir("C:\Databases\Event Photos\" & Me.ConcatinatedName, vbDirectory)) = 0 Then
        MkDir ("C:\Databases\Event Photos\" & Me.ConcatinatedName)
    End If

    x = Shell("EXPLORER.EXE c:\Databases\Event Photos\" & Me.ConcatinatedName, vbNormalFocus)

End Sub
 
Well, it’s sorted now but:-

“C:\Databases\Event Photos\” & Me.ConcatinatedName

could have been created as a single variable and used in all three places.

The failure, therefore, was in defining the directory in more than one place.

Chris.
 
Well, it’s sorted now but:-

“C:\Databases\Event Photos\” & Me.ConcatinatedName

could have been created as a single variable and used in all three places.

The failure, therefore, was in defining the directory in more than one place.

Chris.

Thanks Chris, first time ive doneanything like this, co how would I define it as a vaiable (bear in mind I get most of my code from cources and then adapt it, so dont always know what the code means!)
 
Sorted, how does this look?
Code:
Private Sub btn_EventPhotos_Click()
    Dim x As Variant
    Dim EventPhotos As Variant

    EventPhotos = ("C:\Databases\Event Photos\" & Me.ConcatinatedName)

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

    x = Shell("EXPLORER.EXE c:\Databases\Event Photos\" & Me.ConcatinatedName, vbNormalFocus)

End Sub
 
Use a string instead of a variant. Avoid variants except with specific reason... they're expensive and tricky if you don't know what you're doing.

You don't need the x either, just call Shell without the parentheses

You're checking for a directory after you've applied the filename to the variable. Your DirCheck won't work correctly.

Code:
Dim EventPhotos As String

EventPhotos = "C:\Databases\Event Photos\"

If Dir(EventPhotos, vbDirectory) = "" Then
  MkDir EventPhotos
  DoEvents
End If

Shell EventPhotos & Me.ConcatinatedName

hth
 
Oh yea, I'd verify the directory creation as well:

Code:
Dim EventPhotos As String

EventPhotos = "C:\Databases\Event Photos\"

If Dir(EventPhotos, vbDirectory) = "" Then
  MkDir EventPhotos
  DoEvents
  If Dir(EventPhotos, vbDirectory) = "" Then Err.Raise vbObjectError, "Couldn't Create Directory!"
End If

Shell EventPhotos & Me.ConcatinatedName
 
Oh yea, I'd verify the directory creation as well:

Code:
Dim EventPhotos As String

EventPhotos = "C:\Databases\Event Photos\"

If Dir(EventPhotos, vbDirectory) = "" Then
  MkDir EventPhotos
  DoEvents
  If Dir(EventPhotos, vbDirectory) = "" Then Err.Raise vbObjectError, "Couldn't Create Directory!"
End If

Shell EventPhotos & Me.ConcatinatedName

Didn't like this bit:
Code:
Shell EventPhotos & Me.ConcatinatedName
Coming up with a file not found error
 
To keep it at its simplest:-

Code:
Private Sub btn_EventPhotos_Click()
    Dim strEventPhotos As String

    [color=green]' [color=red]Define[/color] in one place, [color=blue]use[/color] in three.[/color]
    [color=red]strEventPhotos[/color] = "C:\Databases\Event Photos\" & Me.ConcatinatedName

    If Len(Dir([color=blue]strEventPhotos[/color], vbDirectory)) = 0 Then
        MkDir [color=blue]strEventPhotos[/color]
    End If

    Shell "EXPLORER.EXE " & [color=blue]strEventPhotos[/color], vbNormalFocus

End Sub

But because it looks like Me.ConcatinatedName might be typed by a user the entire process could be a lot more complicated.

More to read here.

Chris.
 
Chris, thanks for this, it makes a whole lotta sense to me now, (havent got my head around the link yet!) The concatinated field is a join between a date field and an Event Name, so yees the user does put that info in, but ot only concatinates once the name is in. The only issue I see is if the user chooses to change the nae of the event after it has taken place (unlikely but possible I guess).
 

Users who are viewing this thread

Back
Top Bottom