Validate existance of a directory

D

Daniel

Hello,

I want to set a startdir variable = "//public/common/%username%" if the
computer has access to the folder otherwise set it equal to "c:\". How can
this be done?

Thank you,

Daniel
 
A

Allen Browne

Dir() can search for folders. The function below provides an example
wrapper.

You will need to pass in a string. To get the username, you might be able to
use:
Environ("username")
or you may prefer to use an API call:
http://www.mvps.org/access/api/api0008.htm

Public Function FolderExists(varPath As Variant) As Boolean
On Error Resume Next
If Len(varPath) > 0& Then
FolderExists = (Len(Dir$(varPath, vbDirectory)) > 0&)
End If
End Function
 
D

Daniel

I've tried to use an if statement to verify if the directory exist because
the help file explained that the dir function returned "" if the folder did
not exist

if dir("c:\temp")="" then
msgbox "this directory does not exist"
end if

but I can't seem to get it to work.Could you point out my mistake.

Thankyou

Daniel
 
D

Douglas J. Steele

You're missing the second parameter, vbDirectory:

if dir("c:\temp", vbDirectory)="" then
msgbox "this directory does not exist"
end if
 

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