ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   Error Message (https://www.excelbanter.com/excel-discussion-misc-queries/179141-error-message.html)

Jeff

Error Message
 
Hi,

I have code that checks if a folder exists

strFullPath = "D:\Release"

Sub FolderExistCheck(strFullPath As String, Exists As Boolean)
'if Exists = True then the path exists
If Not Dir(strFullPath, vbDirectory) = vbNullString Then Exists = True
If strFullPath = "" Then Exists = False
End Sub

"D:\Release" does not exist but instead of returning "False" it returns
Bad filename or number. But this only happens with the D: directory if the
directory starts with C: it returns false. Does anyone know why this is?

Thanks


FSt1

Error Message
 
hi.
why is strFullPath out side the sub?
Put it inside the sub and may vb will know what it is

Regards
FSt1

"Jeff" wrote:

Hi,

I have code that checks if a folder exists

strFullPath = "D:\Release"

Sub FolderExistCheck(strFullPath As String, Exists As Boolean)
'if Exists = True then the path exists
If Not Dir(strFullPath, vbDirectory) = vbNullString Then Exists = True
If strFullPath = "" Then Exists = False
End Sub

"D:\Release" does not exist but instead of returning "False" it returns
Bad filename or number. But this only happens with the D: directory if the
directory starts with C: it returns false. Does anyone know why this is?

Thanks


Dave Peterson

Error Message
 
The Dir() can cause a runtime error if the drive drive doesn't exist.

Using "On error resume next" to ignore the error that you know might occur seems
like a reasonable approach to me.

Option Explicit
Sub testme()
Dim ok As Boolean
Call FolderExistCheck("\\a\asfd", ok)
MsgBox ok
End Sub
Sub FolderExistCheck(strFullPath As String, Exists As Boolean)

Dim myStr As String

Exists = False

If strFullPath = "" Then
'do nothing
Else
myStr = ""
On Error Resume Next
myStr = Dir(strFullPath, vbDirectory)
On Error GoTo 0
If myStr < "" Then
Exists = True
End If
End If

End Sub





Jeff wrote:

Hi,

I have code that checks if a folder exists

strFullPath = "D:\Release"

Sub FolderExistCheck(strFullPath As String, Exists As Boolean)
'if Exists = True then the path exists
If Not Dir(strFullPath, vbDirectory) = vbNullString Then Exists = True
If strFullPath = "" Then Exists = False
End Sub

"D:\Release" does not exist but instead of returning "False" it returns
Bad filename or number. But this only happens with the D: directory if the
directory starts with C: it returns false. Does anyone know why this is?

Thanks


--

Dave Peterson


All times are GMT +1. The time now is 02:40 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com