Getting Current Directory (1 Viewer)

robben

Registered User.
Local time
Today, 12:22
Joined
Jan 27, 2008
Messages
52
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

The Mailman - AWF VIP
Local time
Today, 21:22
Joined
Aug 11, 2003
Messages
11,695
currentdb.name
returns the full path + name of the currentdatabase

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

Good luck
 

robben

Registered User.
Local time
Today, 12:22
Joined
Jan 27, 2008
Messages
52
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

The Mailman - AWF VIP
Local time
Today, 21:22
Joined
Aug 11, 2003
Messages
11,695
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

Registered User.
Local time
Today, 12:22
Joined
Jan 27, 2008
Messages
52
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

The Mailman - AWF VIP
Local time
Today, 21:22
Joined
Aug 11, 2003
Messages
11,695
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

Registered User.
Local time
Today, 12:22
Joined
Jan 27, 2008
Messages
52
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
 

Users who are viewing this thread

Top Bottom