accmdclose vs docmd.close

A

AndyB

I have about 10 different forms. Each has a Cancel button linked to a
simple coding of Docmd.close. The code is in each form. I have found that
if I use Runcommand accmdclose in a Module as a function I can just call it
from the on click event and get rid of all the other repetitive code in the
other forms. Is this a good way to go? Or is it considered improper coding?


Andy B
 
N

Niklas Östergren

It´s a good way to go and it makes your application much easyer to maintain.
Let´s say that you would like to display a MsgBox for the user asking "Do
you realy want to close form " & HereYouGetTheFormName & "?"

This has to be done in 10 different places compared to one :)

My 5 cents of knowledge.

// Niklas
 
A

Allen Browne

My personal preference is to use DoCmd.Close because you can specify *what*
to close, and it does not have to be the form that has focus.

The code:
DoCmd.Close acForm, Me.Name
will work in any form, and it will work correctly regardless of where it is
called from.

You may also need to be aware of a glaring bug in Access that can cause you
to lose your entry. If the form is dirty and cannot be saved (required field
missing, validation rule not met, duplicate index, etc), Access just throws
away what you entered *without* any warning that the entry was not accepted.
To me, that's a glaring flaw, because the default behavior in Access is to
save and warn it if fails. To work around that, you must explicitly save the
record in the form before closing, and again you need to specify which form.

So:
If Me.Dirty Then
Me.Dirty = False
End If
DoCmd.Close acForm, Me.Name
and add error handling in case the reccord can't be saved.
 
A

AndyB

Hi Allen

I understand what you are saying however, Since the Cancel button I have
is the same in all forms I would not need to specify what to close.
Otherwise each time I wanted to close the form in the DoCmd CLose I would
have to name the form.

ANdy
 
D

Dale Fye

Call it personal preference, but I'm with Allen.

I prefer to have the code for closing a form (or canceling changes) in that
forms code module. That way, I don't have to go look it up in some other
module when I am not sure what it is doing. Besides, if it is really a
cancel button you are coding, and not a close button, then you will need to
have some code that undoes any changes (me.undo) before you actually close
the form.

It might save a little code to put it in a separate module, but not much,
and for maintainability, my money is on easy of use.

Dale
 
A

AndyB

Thank you both for your input. I will leave the code in the individual
forms.

AndyB
 
A

Allen Browne

Thanks fine, Andy.

Personally I would code a function that accepts name of the form to be
closed, and you can set the On Click of the button to:
=MyFunction2CloseForm([Form])
so the generic function can see if it's dirty and issue the close on the
specific form.

You can still just copy the button from form to form and it works.
 
D

Dirk Goldgar

Allen Browne said:
Thanks fine, Andy.

Personally I would code a function that accepts name of the form to be
closed, and you can set the On Click of the button to:
=MyFunction2CloseForm([Form])
so the generic function can see if it's dirty and issue the close on
the specific form.

You can still just copy the button from form to form and it works.

I might point out that the expression above will pass an object
reference to the form itself, not just the name of the form. So
presumably the function would have to be coded to receive an argument of
type Access.Form, not of type String.
 
A

Allen Browne

Thanks, Dirk.

Yes, the function needs to receive a Form type object.
It can then examine the Dirty property of the form, and read its Name for
the Close action.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Dirk Goldgar said:
Allen Browne said:
Thanks fine, Andy.

Personally I would code a function that accepts name of the form to be
closed, and you can set the On Click of the button to:
=MyFunction2CloseForm([Form])
so the generic function can see if it's dirty and issue the close on
the specific form.

You can still just copy the button from form to form and it works.

I might point out that the expression above will pass an object
reference to the form itself, not just the name of the form. So
presumably the function would have to be coded to receive an argument of
type Access.Form, not of type String.

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 

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