Getting current folder in VBA

  • Thread starter Thread starter SirPoonga
  • Start date Start date
S

SirPoonga

I thought CurDir was suppose to return the current folder. When I use
it in Access VBA it returns a location to My Documents. How can I find
out what folder the DB is in through code?
 
SirPoonga said:
I thought CurDir was suppose to return the current folder. When I use
it in Access VBA it returns a location to My Documents. How can I
find out what folder the DB is in through code?

The "current" directory is the last one accessed, not the one where the MDB
you are running resides. For that you can use...

Left(CurrentDB.Name, Len(CurrentDB.Name) - Len(Dir(CurrentDB.Name)))
 
Thanks.

It's still odd behavior for CurDir though since the last folder I
accessed was the folder to open the DB. Unless you mean last folder
accessed through code.
 
In 2000 and later you can use CurrentProject.Path and CurrentProject.Name
for folder and filename, respectively.
In 97 and earlier CurrentDB.Name will give you the path and filename
combined, which can then parsed on the last path separator.
 
What do you mean by "the last folder I accessed was the folder to open the
DB."?

If you were within Access and navigated to the folder through the Windows
File Open dialog (using File | Open from the menu bar), then yes, CurDir
should be the same folder the database is in.

If, on the other hand, you found the mdb file through File Explorer and
double-clicked on it, that has no effect on CurDir.
 
SirPoonga said:
Thanks.

It's still odd behavior for CurDir though since the last folder I
accessed was the folder to open the DB. Unless you mean last folder
accessed through code.

CurDir() returns the Windows "current directory", which may be set by a
variety of user activities. Just accessing a folder doesn't necessarily
make that the current directory, in this sense.
 
When calling the GetOpenFileName API function in code, there's an option we
can set to determine whether the call to the API should change the current
directory or not. This API call is the same method that many Windows
programs use, so presumably the same option is available to the developers
of those programs. So it is probably safest to avoid any assumptions about
whether selecting a file name changes the current directory or not.

--
Brendan Reynolds (MVP)
http://brenreyn.blogspot.com

The spammers and script-kiddies have succeeded in making it impossible for
me to use a real e-mail address in public newsgroups. E-mail replies to
this post will be deleted without being read. Any e-mail claiming to be
from brenreyn at indigo dot ie that is not digitally signed by me with a
GlobalSign digital certificate is a forgery and should be deleted without
being read. Follow-up questions should in general be posted to the
newsgroup, but if you have a good reason to send me e-mail, you'll find
a useable e-mail address at the URL above.
 

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

Back
Top