Maintain current directory when canceling Form

S

singeredel

Hi,

Is there a way to write code to include in the "Cancel" click command (as
follows) so that the directory which is currently active stays the active
directory? Every time I call up the form but then decide to cancel and click
the "Cancel" button on the form, the directory changes to the default
directory rather than staying in the last active directory before the cancel
command. This is what I currently have for the cancel command:

Private Sub cmdCancel_Click()
End
End Sub

Thanks...
 
A

Anand.V.V.N

Hi,

What you can do is have a global variable or a static variable that would
store the current directory value, and set the value in your cancle button
click,
and eveytime you have disply your dialog box, you can use api to set the
current directory path to the value stored in the variable if it has some
value stored in it, otherwise you can let it take the default directory.

Hope that was helpful.

Anand
 
S

singeredel

Since I am just a layperson that has written some code and not a programmer,
I could use some help in how to go about this.

Thanks...
 
A

Anand.V.V.N

Hi,

Here is wat you do

In general section declare a global variable

Dim curdirstring as string
dim rtnval
In the cmdCancel_Click() event

curdirstring=GetCurrentDirectory()

And when your form loads check if the curdirstring in empty

if curdirstring<>"" then
rtnval=SetCurrentDirectory(curdirstring)
else
endif

I'll paste the whole code in just a while.

Hope this was helpful
Anand
 
A

Anand.V.V.N

Hi,

Here is wat you do

In general section declare a global variable

Dim curdirstring as string
dim rtnval
In the cmdCancel_Click() event

curdirstring=GetCurrentDirectory()

And when your form loads check if the curdirstring in empty

if curdirstring<>"" then
rtnval=SetCurrentDirectory(curdirstring)
else
endif
 
A

Anand.V.V.N

Hi,

Here is wat you do

In general section declare a global variable

Dim curdirstring as string
dim rtnval
In the cmdCancel_Click() event

curdirstring=GetCurrentDirectory()

And when your form loads check if the curdirstring in empty

if curdirstring<>"" then
rtnval=SetCurrentDirectory(curdirstring)
else
endif

Anand
 
A

Anand.V.V.N

Sorry about that, too many posts, there was some error in some setting,
didn't know if it was posted properly so I posted so many times.
Any way I'll post the full code in some time

Anand
 
S

singeredel

Thanks for your help! Perhaps I am not putting these statements in the proper
places, but I get a compile error: "Sub or Function Not Defined" and
highlights the "GetCurrentDirectory."
 
A

Anand.V.V.N

Hi,
Public Declare Function GetCurrentDirectory Lib "kernel32" Alias
"GetCurrentDirectory" (ByVal nBufferLength As Long, ByVal lpBuffer As String)
As Long

Public Declare Function SetCurrentDirectory Lib "kernel32" Alias
"SetCurrentDirectoryA" (ByVal lpPathName As String) As Long

You these two API refrences and try. Paste them in thegeneral section of
your form.

I hope you find this helpful.

Anand
 
S

singeredel

Thanks so much for your help. I placed the "Public Declare Function..."
statements in the general section that appears before the Form code, as well
as the "Dim curdirstring as string" and "Dim rtnval" statements. However, I
get a Compile Error: "Constants, Fixed Length Strings, Arrays, user-defined
types and Declare statements not allowed as Public members of object modules"
and it highlights the "Public Declare Function..." lines of code.
 
A

Anand.V.V.N

Hi,

I am very sorry about that.

You can try this. From the insert menu choose module. Declare those API in
the module and also you can delcare your variables in that. Then I guess your
problem should be solved.

I hope this helps you and solve the problem you are facing.

Anand.
 
S

singeredel

I declared the API and the variables in the general section (declarations) of
a module, as follows:

Public Declare Function GetCurrentDirectory Lib "kernel32" (ByVal
nBufferLength As Long, ByVal IpBuffer As String) As Long
Public Declare Function SetCurrentDirectory Lib "kernel32" Alias
"SetCurrentDirectoryA" (ByVal IpPathName As String) As Long

Dim curdirstring As String
Dim rtnval

However, in the following Cancel event, I get a Compile error: Argument not
optional:

Private Sub cmdCancel_Click()
curdirstring = GetCurrentDirectory()
End
End Sub

Thanks...
 
A

Anand.V.V.N

Hi I am sorry again. Try this code this is bound to work, I tried calling APi
from the cancle button but it didn't work so I came up with this code and it
works fine.

Paste the code in the form load or otehr event button

curdirstring = CurDir$()
MsgBox curdirstring

In the module declare the string

global curdirstring as string

In the cancle button this code

ChDir (curdirstring)

Hope this code works

Anand
 

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