View Full Version : Getting Current Directory


robben
07-30-2008, 12:45 AM
I have a simple question what I want to do is get the current directory path of where the Access database resides, how would I do this using VB?

Thank you.

namliam
07-30-2008, 01:02 AM
currentdb.name
returns the full path + name of the currentdatabase

left(currentdb.Name, instrrev(currentdb.name, "\"))
Will return only the path.

Good luck

robben
07-30-2008, 02:03 AM
Many thanks namliam, excellent the code worked.
However, I'm now trying to copy a file (selected by the user) to the database directory.

I have a directory named "testDFirectory" in the root directory where the database is located which I want to copy the file over to.

However, when I run the code (see below) i get the following error message "path/File access error", I'm not sure what I'm doing wrong.

Also, when I copy the file to the destination directory I would like to keep the name of the file the same although appended with the date and time.
Sorry, I have only basic knowledge of VB.

Dim currDir As String
currDir = Left(CurrentDb.Name, InStrRev(CurrentDb.Name, "\"))
...
FileCopy myFileToCopyPath, currDir & "\testDirectory"

namliam
07-30-2008, 02:13 AM
Hit CTRL+G on your keyboard while in access... This opens the immediate window
Now
If you type this, followed by <enter>:
?Left(CurrentDb.Name, InStrRev(CurrentDb.Name, "\"))
You will see that it returns:
C:\Documents and Settings\

This allready contains the backslash in your currDir causing your full path to be
C:\Documents and Settings\\testDirectory
Which offcourse is not going to work :(

robben
07-30-2008, 02:27 AM
If I have full path for a filename i.e. C:\Documents and Settings\test\file.doc
How do I just extract the filname and extension i.e. file.doc in this case using Left?
I tried but can't work it out.
Thanks

namliam
07-30-2008, 02:39 AM
Use right instead of left. Or use Dir, this only returns the file name.

Right:
right(currentdb.Name,len(currentdb.name) - instrrev(currentdb.Name,"\"))

Dir:
Dir(Currentdb.name)

You can replace currentdb.name by any full path + filename combination (i.e. variable).

robben
07-30-2008, 02:50 AM
Got it working,

Public Function GetFilenameFromPath(FullPath As String) As String
'Returns the filename and extension
GetFilenameFromPath = Right(FullPath, Len(FullPath) - InStrRev(FullPath, "\"))
End Function