Checking multiple locations for folder using IF statement

R

Ron

Hi all. The system that we have uses mounted directories. This means
that a client/matter number in the format of 12345/123 could be mounted
on any drive letter between L and Z. The other variable is that the
client/matter number could be mounted including the folder "BOOKS" or
not, ie 12345/123/BOOKS.

So, what I'm trying to do is open a folder, if it exists. First I want
to check whether the directory has been mounted on Drive L with the
whole 12345/123/BOOKS. If so, open the folder, if not, I check whether
only 12345/123 was mounted on drive L. If so, open the folder. I need
to do this for all the drive letters to see where the client/matter
directory is mounted.

There are already issues with my coding below as the folder will open
twice if the folder is mounted on Drive L. [PATH] is the value of the
full pathline as stored in a form's text box. I'm missing something
here and would appreciate any assistance.

Thanks,

-Ron



Private Sub openpath_Click()
Dim stpath As String
Dim fixedpath As String
Dim smallpath As String

fixedpath = Replace([Path], "/", "\")
smallpath = Mid([fixedpath], 17)
stpath = "L:\" & smallpath

If Not Dir(stpath, vbDirectory) = "" Then
Retval = Shell("explorer /e,/root, " & stpath, vbMaximizedFocus)
Else
smallpath = Mid([fixedpath], 11)
stpath = "L:\BOOKS" & smallpath
End If

If Not Dir(stpath, vbDirectory) = "" Then
Retval = Shell("explorer /e,/root, " & stpath, vbMaximizedFocus)
Else
MsgBox "Directory Does Not Exist or Is Not Mounted", vbOKOnly,
"Warning..."
End If

End Sub
 
G

Guest

You are opening it twice if it is on Drive L: because the second call to Dir
happend regardless of what your If statement does. Here is a rewrite of you
code that may help. It will loop through all drives from L: to Z: looking
for the path name with and without BOOKS.
Notice the use of th Chr function and the For Next. 76 is the Character
equivalent of L and 90 is Z. So, if you do a For Next starting at 76 and
ending at 90, convert the loop counter to a character, you have the next
drive letter.

Private Sub openpath_Click()
Dim stpath As String
Dim fixedpath As String
Dim intDrive as Integer
Dim blnFoundPath

fixedpath = Replace([Path], "/", "\")
fixedpath = Mid([fixedpath], 17)
For intDrive = 76 to 90
stpath = Chr(intDrive) & ":\" & fixedpath

If Dir(stpath, vbDirectory) <> "" Then
blnFoundPath = True
Else
stpath = Replace(stpath, ":\", ":\BOOKS)
If Dir(stpath, vbDirectory) <> "" Then
blnFoundPath = True
End If

if blnFoundPath then
Exit For
End If
Next intDrive

If blnFoundPath Then
Retval = Shell("explorer /e,/root, " & stpath, vbMaximizedFocus)
Else
MsgBox "Directory Does Not Exist or Is Not Mounted", vbOKOnly, _
"Warning..."
End If

End Sub
 
R

Ron

This is very helpful Klatuu. I understand the logic now.

However, every time I run the code I get a "Next without For" error.
Not sure exactly where the problem lies, even after reading the help
section about it.

Thanks,

-Ron



You are opening it twice if it is on Drive L: because the second call to Dir
happend regardless of what your If statement does. Here is a rewrite of you
code that may help. It will loop through all drives from L: to Z: looking
for the path name with and without BOOKS.
Notice the use of th Chr function and the For Next. 76 is the Character
equivalent of L and 90 is Z. So, if you do a For Next starting at 76 and
ending at 90, convert the loop counter to a character, you have the next
drive letter.

Private Sub openpath_Click()
Dim stpath As String
Dim fixedpath As String
Dim intDrive as Integer
Dim blnFoundPath

fixedpath = Replace([Path], "/", "\")
fixedpath = Mid([fixedpath], 17)
For intDrive = 76 to 90
stpath = Chr(intDrive) & ":\" & fixedpath

If Dir(stpath, vbDirectory) <> "" Then
blnFoundPath = True
Else
stpath = Replace(stpath, ":\", ":\BOOKS)
If Dir(stpath, vbDirectory) <> "" Then
blnFoundPath = True
End If

if blnFoundPath then
Exit For
End If
Next intDrive

If blnFoundPath Then
Retval = Shell("explorer /e,/root, " & stpath, vbMaximizedFocus)
Else
MsgBox "Directory Does Not Exist or Is Not Mounted", vbOKOnly, _
"Warning..."
End If

End Sub



Ron said:
Hi all. The system that we have uses mounted directories. This means
that a client/matter number in the format of 12345/123 could be mounted
on any drive letter between L and Z. The other variable is that the
client/matter number could be mounted including the folder "BOOKS" or
not, ie 12345/123/BOOKS.

So, what I'm trying to do is open a folder, if it exists. First I want
to check whether the directory has been mounted on Drive L with the
whole 12345/123/BOOKS. If so, open the folder, if not, I check whether
only 12345/123 was mounted on drive L. If so, open the folder. I need
to do this for all the drive letters to see where the client/matter
directory is mounted.

There are already issues with my coding below as the folder will open
twice if the folder is mounted on Drive L. [PATH] is the value of the
full pathline as stored in a form's text box. I'm missing something
here and would appreciate any assistance.

Thanks,

-Ron



Private Sub openpath_Click()
Dim stpath As String
Dim fixedpath As String
Dim smallpath As String

fixedpath = Replace([Path], "/", "\")
smallpath = Mid([fixedpath], 17)
stpath = "L:\" & smallpath

If Not Dir(stpath, vbDirectory) = "" Then
Retval = Shell("explorer /e,/root, " & stpath, vbMaximizedFocus)
Else
smallpath = Mid([fixedpath], 11)
stpath = "L:\BOOKS" & smallpath
End If

If Not Dir(stpath, vbDirectory) = "" Then
Retval = Shell("explorer /e,/root, " & stpath, vbMaximizedFocus)
Else
MsgBox "Directory Does Not Exist or Is Not Mounted", vbOKOnly,
"Warning..."
End If

End Sub
 
R

Ron

Hi, could someone please look at Klatuu's code in this thread and tell
me why I'm getting a "Next Without For" error?

Thanks,

-Ron


This is very helpful Klatuu. I understand the logic now.

However, every time I run the code I get a "Next without For" error.
Not sure exactly where the problem lies, even after reading the help
section about it.

Thanks,

-Ron



You are opening it twice if it is on Drive L: because the second call to Dir
happend regardless of what your If statement does. Here is a rewrite of you
code that may help. It will loop through all drives from L: to Z: looking
for the path name with and without BOOKS.
Notice the use of th Chr function and the For Next. 76 is the Character
equivalent of L and 90 is Z. So, if you do a For Next starting at 76 and
ending at 90, convert the loop counter to a character, you have the next
drive letter.

Private Sub openpath_Click()
Dim stpath As String
Dim fixedpath As String
Dim intDrive as Integer
Dim blnFoundPath

fixedpath = Replace([Path], "/", "\")
fixedpath = Mid([fixedpath], 17)
For intDrive = 76 to 90
stpath = Chr(intDrive) & ":\" & fixedpath

If Dir(stpath, vbDirectory) <> "" Then
blnFoundPath = True
Else
stpath = Replace(stpath, ":\", ":\BOOKS)
If Dir(stpath, vbDirectory) <> "" Then
blnFoundPath = True
End If

if blnFoundPath then
Exit For
End If
Next intDrive

If blnFoundPath Then
Retval = Shell("explorer /e,/root, " & stpath, vbMaximizedFocus)
Else
MsgBox "Directory Does Not Exist or Is Not Mounted", vbOKOnly, _
"Warning..."
End If

End Sub



Ron said:
Hi all. The system that we have uses mounted directories. This means
that a client/matter number in the format of 12345/123 could be mounted
on any drive letter between L and Z. The other variable is that the
client/matter number could be mounted including the folder "BOOKS" or
not, ie 12345/123/BOOKS.

So, what I'm trying to do is open a folder, if it exists. First I want
to check whether the directory has been mounted on Drive L with the
whole 12345/123/BOOKS. If so, open the folder, if not, I check whether
only 12345/123 was mounted on drive L. If so, open the folder. I need
to do this for all the drive letters to see where the client/matter
directory is mounted.

There are already issues with my coding below as the folder will open
twice if the folder is mounted on Drive L. [PATH] is the value of the
full pathline as stored in a form's text box. I'm missing something
here and would appreciate any assistance.

Thanks,

-Ron



Private Sub openpath_Click()
Dim stpath As String
Dim fixedpath As String
Dim smallpath As String

fixedpath = Replace([Path], "/", "\")
smallpath = Mid([fixedpath], 17)
stpath = "L:\" & smallpath

If Not Dir(stpath, vbDirectory) = "" Then
Retval = Shell("explorer /e,/root, " & stpath, vbMaximizedFocus)
Else
smallpath = Mid([fixedpath], 11)
stpath = "L:\BOOKS" & smallpath
End If

If Not Dir(stpath, vbDirectory) = "" Then
Retval = Shell("explorer /e,/root, " & stpath, vbMaximizedFocus)
Else
MsgBox "Directory Does Not Exist or Is Not Mounted", vbOKOnly,
"Warning..."
End If

End Sub
 
S

Stefan Hoffmann

hi Ron,
Hi, could someone please look at Klatuu's code in this thread and tell
me why I'm getting a "Next Without For" error?
Come on Ron, count the Ends.
This is very helpful Klatuu. I understand the logic now.

However, every time I run the code I get a "Next without For" error.
Not sure exactly where the problem lies, even after reading the help
section about it.

Thanks,

-Ron



You are opening it twice if it is on Drive L: because the second call to Dir
happend regardless of what your If statement does. Here is a rewrite of you
code that may help. It will loop through all drives from L: to Z: looking
for the path name with and without BOOKS.
Notice the use of th Chr function and the For Next. 76 is the Character
equivalent of L and 90 is Z. So, if you do a For Next starting at 76 and
ending at 90, convert the loop counter to a character, you have the next
drive letter.

Private Sub openpath_Click()
Dim stpath As String
Dim fixedpath As String
Dim intDrive as Integer
Dim blnFoundPath

fixedpath = Replace([Path], "/", "\")
fixedpath = Mid([fixedpath], 17)
For intDrive = 76 to 90
stpath = Chr(intDrive) & ":\" & fixedpath

If Dir(stpath, vbDirectory) <> "" Then
blnFoundPath = True
Else
stpath = Replace(stpath, ":\", ":\BOOKS)
If Dir(stpath, vbDirectory) <> "" Then
blnFoundPath = True Here is an End If missing, or an underscore after the Then.
End If

if blnFoundPath then
Exit For
End If
Next intDrive

If blnFoundPath Then
Retval = Shell("explorer /e,/root, " & stpath, vbMaximizedFocus)
Else
MsgBox "Directory Does Not Exist or Is Not Mounted", vbOKOnly, _
"Warning..."
End If

End Sub



:

Hi all. The system that we have uses mounted directories. This means
that a client/matter number in the format of 12345/123 could be mounted
on any drive letter between L and Z. The other variable is that the
client/matter number could be mounted including the folder "BOOKS" or
not, ie 12345/123/BOOKS.

So, what I'm trying to do is open a folder, if it exists. First I want
to check whether the directory has been mounted on Drive L with the
whole 12345/123/BOOKS. If so, open the folder, if not, I check whether
only 12345/123 was mounted on drive L. If so, open the folder. I need
to do this for all the drive letters to see where the client/matter
directory is mounted.

There are already issues with my coding below as the folder will open
twice if the folder is mounted on Drive L. [PATH] is the value of the
full pathline as stored in a form's text box. I'm missing something
here and would appreciate any assistance.

Thanks,

-Ron



Private Sub openpath_Click()
Dim stpath As String
Dim fixedpath As String
Dim smallpath As String

fixedpath = Replace([Path], "/", "\")
smallpath = Mid([fixedpath], 17)
stpath = "L:\" & smallpath

If Not Dir(stpath, vbDirectory) = "" Then
Retval = Shell("explorer /e,/root, " & stpath, vbMaximizedFocus)
Else
smallpath = Mid([fixedpath], 11)
stpath = "L:\BOOKS" & smallpath
End If

If Not Dir(stpath, vbDirectory) = "" Then
Retval = Shell("explorer /e,/root, " & stpath, vbMaximizedFocus)
Else
MsgBox "Directory Does Not Exist or Is Not Mounted", vbOKOnly,
"Warning..."
End If

End Sub



mfG
--> stefan <--
 
G

Guest

I am not seeing the problem, Ron. Have you figured it out yet? I do see one
error. In fact, I thought about it last night. This line
stpath = Replace(stpath, ":\", ":\BOOKS)
Should be
stpath = Replace(stpath, ":\", ":\BOOKS\")

I have reviewd the code and I don't see why it is getting that error. It
often happens if you have unbalanced If End If pairs. In the code I sent,
they are all equal and appear to be correct. I wonder if the problem could be
caused by the error above?

Let me know if you are still having problems with it.


Ron said:
Hi, could someone please look at Klatuu's code in this thread and tell
me why I'm getting a "Next Without For" error?

Thanks,

-Ron


This is very helpful Klatuu. I understand the logic now.

However, every time I run the code I get a "Next without For" error.
Not sure exactly where the problem lies, even after reading the help
section about it.

Thanks,

-Ron



You are opening it twice if it is on Drive L: because the second call to Dir
happend regardless of what your If statement does. Here is a rewrite of you
code that may help. It will loop through all drives from L: to Z: looking
for the path name with and without BOOKS.
Notice the use of th Chr function and the For Next. 76 is the Character
equivalent of L and 90 is Z. So, if you do a For Next starting at 76 and
ending at 90, convert the loop counter to a character, you have the next
drive letter.

Private Sub openpath_Click()
Dim stpath As String
Dim fixedpath As String
Dim intDrive as Integer
Dim blnFoundPath

fixedpath = Replace([Path], "/", "\")
fixedpath = Mid([fixedpath], 17)
For intDrive = 76 to 90
stpath = Chr(intDrive) & ":\" & fixedpath

If Dir(stpath, vbDirectory) <> "" Then
blnFoundPath = True
Else
stpath = Replace(stpath, ":\", ":\BOOKS)
If Dir(stpath, vbDirectory) <> "" Then
blnFoundPath = True
End If

if blnFoundPath then
Exit For
End If
Next intDrive

If blnFoundPath Then
Retval = Shell("explorer /e,/root, " & stpath, vbMaximizedFocus)
Else
MsgBox "Directory Does Not Exist or Is Not Mounted", vbOKOnly, _
"Warning..."
End If

End Sub



:


Hi all. The system that we have uses mounted directories. This means
that a client/matter number in the format of 12345/123 could be mounted
on any drive letter between L and Z. The other variable is that the
client/matter number could be mounted including the folder "BOOKS" or
not, ie 12345/123/BOOKS.

So, what I'm trying to do is open a folder, if it exists. First I want
to check whether the directory has been mounted on Drive L with the
whole 12345/123/BOOKS. If so, open the folder, if not, I check whether
only 12345/123 was mounted on drive L. If so, open the folder. I need
to do this for all the drive letters to see where the client/matter
directory is mounted.

There are already issues with my coding below as the folder will open
twice if the folder is mounted on Drive L. [PATH] is the value of the
full pathline as stored in a form's text box. I'm missing something
here and would appreciate any assistance.

Thanks,

-Ron



Private Sub openpath_Click()
Dim stpath As String
Dim fixedpath As String
Dim smallpath As String

fixedpath = Replace([Path], "/", "\")
smallpath = Mid([fixedpath], 17)
stpath = "L:\" & smallpath

If Not Dir(stpath, vbDirectory) = "" Then
Retval = Shell("explorer /e,/root, " & stpath, vbMaximizedFocus)
Else
smallpath = Mid([fixedpath], 11)
stpath = "L:\BOOKS" & smallpath
End If

If Not Dir(stpath, vbDirectory) = "" Then
Retval = Shell("explorer /e,/root, " & stpath, vbMaximizedFocus)
Else
MsgBox "Directory Does Not Exist or Is Not Mounted", vbOKOnly,
"Warning..."
End If

End Sub
 
D

Douglas J Steele

Stefan identified it: it's a missing End If in

If Dir(stpath, vbDirectory) <> "" Then
blnFoundPath = True
Else
stpath = Replace(stpath, ":\", ":\BOOKS)
If Dir(stpath, vbDirectory) <> "" Then
blnFoundPath = True
End If

Perhaps you intended

If Dir(stpath, vbDirectory) <> "" Then
blnFoundPath = True

to be 1 line:



--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


Klatuu said:
I am not seeing the problem, Ron. Have you figured it out yet? I do see one
error. In fact, I thought about it last night. This line
stpath = Replace(stpath, ":\", ":\BOOKS)
Should be
stpath = Replace(stpath, ":\", ":\BOOKS\")

I have reviewd the code and I don't see why it is getting that error. It
often happens if you have unbalanced If End If pairs. In the code I sent,
they are all equal and appear to be correct. I wonder if the problem could be
caused by the error above?

Let me know if you are still having problems with it.


Ron said:
Hi, could someone please look at Klatuu's code in this thread and tell
me why I'm getting a "Next Without For" error?

Thanks,

-Ron


This is very helpful Klatuu. I understand the logic now.

However, every time I run the code I get a "Next without For" error.
Not sure exactly where the problem lies, even after reading the help
section about it.

Thanks,

-Ron




Klatuu wrote:
You are opening it twice if it is on Drive L: because the second call to Dir
happend regardless of what your If statement does. Here is a rewrite of you
code that may help. It will loop through all drives from L: to Z: looking
for the path name with and without BOOKS.
Notice the use of th Chr function and the For Next. 76 is the Character
equivalent of L and 90 is Z. So, if you do a For Next starting at 76 and
ending at 90, convert the loop counter to a character, you have the next
drive letter.

Private Sub openpath_Click()
Dim stpath As String
Dim fixedpath As String
Dim intDrive as Integer
Dim blnFoundPath

fixedpath = Replace([Path], "/", "\")
fixedpath = Mid([fixedpath], 17)
For intDrive = 76 to 90
stpath = Chr(intDrive) & ":\" & fixedpath

If Dir(stpath, vbDirectory) <> "" Then
blnFoundPath = True
Else
stpath = Replace(stpath, ":\", ":\BOOKS)
If Dir(stpath, vbDirectory) <> "" Then
blnFoundPath = True
End If

if blnFoundPath then
Exit For
End If
Next intDrive

If blnFoundPath Then
Retval = Shell("explorer /e,/root, " & stpath, vbMaximizedFocus)
Else
MsgBox "Directory Does Not Exist or Is Not Mounted", vbOKOnly, _
"Warning..."
End If

End Sub



:


Hi all. The system that we have uses mounted directories. This means
that a client/matter number in the format of 12345/123 could be mounted
on any drive letter between L and Z. The other variable is that the
client/matter number could be mounted including the folder "BOOKS" or
not, ie 12345/123/BOOKS.

So, what I'm trying to do is open a folder, if it exists. First I want
to check whether the directory has been mounted on Drive L with the
whole 12345/123/BOOKS. If so, open the folder, if not, I check whether
only 12345/123 was mounted on drive L. If so, open the folder. I need
to do this for all the drive letters to see where the client/matter
directory is mounted.

There are already issues with my coding below as the folder will open
twice if the folder is mounted on Drive L. [PATH] is the value of the
full pathline as stored in a form's text box. I'm missing something
here and would appreciate any assistance.

Thanks,

-Ron



Private Sub openpath_Click()
Dim stpath As String
Dim fixedpath As String
Dim smallpath As String

fixedpath = Replace([Path], "/", "\")
smallpath = Mid([fixedpath], 17)
stpath = "L:\" & smallpath

If Not Dir(stpath, vbDirectory) = "" Then
Retval = Shell("explorer /e,/root, " & stpath, vbMaximizedFocus)
Else
smallpath = Mid([fixedpath], 11)
stpath = "L:\BOOKS" & smallpath
End If

If Not Dir(stpath, vbDirectory) = "" Then
Retval = Shell("explorer /e,/root, " & stpath, vbMaximizedFocus)
Else
MsgBox "Directory Does Not Exist or Is Not Mounted", vbOKOnly,
"Warning..."
End If

End Sub
 
G

Guest

Thanks, Douglas, I missed that.

Douglas J Steele said:
Stefan identified it: it's a missing End If in

If Dir(stpath, vbDirectory) <> "" Then
blnFoundPath = True
Else
stpath = Replace(stpath, ":\", ":\BOOKS)
If Dir(stpath, vbDirectory) <> "" Then
blnFoundPath = True
End If

Perhaps you intended

If Dir(stpath, vbDirectory) <> "" Then
blnFoundPath = True

to be 1 line:



--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


Klatuu said:
I am not seeing the problem, Ron. Have you figured it out yet? I do see one
error. In fact, I thought about it last night. This line
stpath = Replace(stpath, ":\", ":\BOOKS)
Should be
stpath = Replace(stpath, ":\", ":\BOOKS\")

I have reviewd the code and I don't see why it is getting that error. It
often happens if you have unbalanced If End If pairs. In the code I sent,
they are all equal and appear to be correct. I wonder if the problem could be
caused by the error above?

Let me know if you are still having problems with it.


Ron said:
Hi, could someone please look at Klatuu's code in this thread and tell
me why I'm getting a "Next Without For" error?

Thanks,

-Ron



Ron wrote:
This is very helpful Klatuu. I understand the logic now.

However, every time I run the code I get a "Next without For" error.
Not sure exactly where the problem lies, even after reading the help
section about it.

Thanks,

-Ron




Klatuu wrote:
You are opening it twice if it is on Drive L: because the second call to Dir
happend regardless of what your If statement does. Here is a rewrite of you
code that may help. It will loop through all drives from L: to Z: looking
for the path name with and without BOOKS.
Notice the use of th Chr function and the For Next. 76 is the Character
equivalent of L and 90 is Z. So, if you do a For Next starting at 76 and
ending at 90, convert the loop counter to a character, you have the next
drive letter.

Private Sub openpath_Click()
Dim stpath As String
Dim fixedpath As String
Dim intDrive as Integer
Dim blnFoundPath

fixedpath = Replace([Path], "/", "\")
fixedpath = Mid([fixedpath], 17)
For intDrive = 76 to 90
stpath = Chr(intDrive) & ":\" & fixedpath

If Dir(stpath, vbDirectory) <> "" Then
blnFoundPath = True
Else
stpath = Replace(stpath, ":\", ":\BOOKS)
If Dir(stpath, vbDirectory) <> "" Then
blnFoundPath = True
End If

if blnFoundPath then
Exit For
End If
Next intDrive

If blnFoundPath Then
Retval = Shell("explorer /e,/root, " & stpath, vbMaximizedFocus)
Else
MsgBox "Directory Does Not Exist or Is Not Mounted", vbOKOnly, _
"Warning..."
End If

End Sub



:


Hi all. The system that we have uses mounted directories. This means
that a client/matter number in the format of 12345/123 could be mounted
on any drive letter between L and Z. The other variable is that the
client/matter number could be mounted including the folder "BOOKS" or
not, ie 12345/123/BOOKS.

So, what I'm trying to do is open a folder, if it exists. First I want
to check whether the directory has been mounted on Drive L with the
whole 12345/123/BOOKS. If so, open the folder, if not, I check whether
only 12345/123 was mounted on drive L. If so, open the folder. I need
to do this for all the drive letters to see where the client/matter
directory is mounted.

There are already issues with my coding below as the folder will open
twice if the folder is mounted on Drive L. [PATH] is the value of the
full pathline as stored in a form's text box. I'm missing something
here and would appreciate any assistance.

Thanks,

-Ron



Private Sub openpath_Click()
Dim stpath As String
Dim fixedpath As String
Dim smallpath As String

fixedpath = Replace([Path], "/", "\")
smallpath = Mid([fixedpath], 17)
stpath = "L:\" & smallpath

If Not Dir(stpath, vbDirectory) = "" Then
Retval = Shell("explorer /e,/root, " & stpath, vbMaximizedFocus)
Else
smallpath = Mid([fixedpath], 11)
stpath = "L:\BOOKS" & smallpath
End If

If Not Dir(stpath, vbDirectory) = "" Then
Retval = Shell("explorer /e,/root, " & stpath, vbMaximizedFocus)
Else
MsgBox "Directory Does Not Exist or Is Not Mounted", vbOKOnly,
"Warning..."
End If

End Sub
 

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