Shell to app in the same folder

B

Bob Richardson

When a user clicks a button on my form, I want to Shell to MyApp, which is
in the same folder as my MDB.

It works if I include the entire path; e.g.

stAppName = "C:\Documents and Settings\Bob\My Documents\BW\MyApp.exe"
Call Shell(stAppName, 3)

but this is cumbersome, since I want to be able to move my MDB (and of
course MyApp.exe) to other folders on other computers.

Since my MDB is in the C:\Documents and Settings\Bob\My Documents\BW\
folder, I would have thought that the CurDir would be that. However, when I
run

MsbBox CurDir

it shows "C:\Documents and Settings\Bob\My Documents\"

in otherwords, it doesn't show the "BW" at the end.

I'd like to Shell to an app in the same folder by just using:

Call Shell("MyApp",3)

Anyone know what's going on, and why the current directory is not where my
MDB is? How would I change the directory back to the same directory as my
MDB?
 
V

Van T. Dinh

If you use A2000 or later:

CurrentProject.Path

will give you the dir. the MDB (Front-End in a split system) resides.
 
D

Douglas J. Steele

And if you're still using Access 97 or earlier, you can use

Left$(CurrentDb.Name, Len(CurrentDb.Name) - Len(Dir$(CurrentDb.Name)))

Note that this includes the terminating slash, while CurrentProject.Path
doesn't.
 
B

Bob Richardson

I have Office 2003.

The directory shown in CurDIr of CurrenteProject.Path DEPENDS on how you
opened your MDB. If you click on the MDB file you get one result, if you
open the mdb file from Access, you get another!!!! This has to be the most
obtuse bug I've encountered.

If you use Application.CurrentProject.Path, it works consistently. This has
worked.

ChDir Application.CurrentProject.Path
stAppName = "AssignClasses.exe"
Call Shell(stAppName, 3)

You can't use ChDir CurrentProject.Path because you'll gewt a different
result, depending on how your MDB was opened. Go figure.
 
D

Douglas J. Steele

It's not a bug. It's a case of you not realizing that CurDir wasn't designed
to return the directory that the application is in: it's supposed to return
the Current Directory.

When you create a shortcut, you have the option of specifying a Start In
folder. If you open Access using that shortcut, the Current Directory will
be the Start In folder. (This is true whether the shortcut simplies open
Access, or opens a particular file in Access)

If you open Access then select File | Open, the current directory gets
changed to wherever you navigated in the File Open dialog.

If you open Access and select one of the databases from the MRU (Most
Recently Used) list, or if you open a database by clicking on the MDB name
in Windows Explorer, the current directory gets changed to whatever you've
set the Default Database Folder to on the General tab under Tools | Options.

You also have the ability to change the value of CurDir using the ChDir
and/or ChDrive statements.

Your comment about ChDir CurrentProject.Path not always working implies that
CurrentProject.Path points to a different drive than CurDir: the ChDir
function only changes directory, not drive. See whether the following works
any more consistently for you:

ChDrive CurrentProject.Path
ChDir CurrentProject.Path
 
B

Bob Richardson

Thank you for the good explanation of this issue. It seems that there is an
important distinction between "current directory" and "current path" while I
thought they were pretty synonymous. Perhaps they are often one and the
same, but not always. So the "current path" is normally where the program
is, and the "current directory" is "initially" the Start In directory,
although it can be changed with the ChDir command?

I haven't tried to change drives...I'm challenged enough just getting around
the directories on one drive :)
 
D

Douglas J. Steele

Sort of. I think you've got the idea for Current Directory, but Current Path
can be a bit more complicated, since in theory there can be many different
"Current Paths". The example you have below is the CurrentPath for the
CurrentProject object. To simplify, though, yes,
Application.CurrentProject.Path always points to the path where the MDB
where the code's running exists.
 

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