[AccessD] Cannot close a form with Docmd.Close

Heenan, Lambert Lambert.Heenan at chartisinsurance.com
Tue May 25 08:09:29 CDT 2010


I simply cannot agree with your comment about the variable names. "Prp", "lbl", "Ctl",and "Frm" are simply *not* reserved words. Consequently VBA will not get "confused" by them. True, a programmer reading the code might get confused, but not the compiler. If there was any truth in that notion we would never be able to compile a line of code without confusing the compiler.

That said, it is certainly not clear why the form will not close. Setting object variables to Nothing is certainly not the *cause*, nor is it bad practice. In fact in earlier versions of Access not setting your objects to nothing could prevent Access itself from terminating.

Without seeing more of the code it's difficult to see what is the culprit, but a sure fire way to ensure you are closing the current form is to use Me.Name instead of a variable with the form name. i.e. 

DoCmd.Close acForm, Me.Name, acSaveYes

Lambert

-----Original Message-----
From: accessd-bounces at databaseadvisors.com [mailto:accessd-bounces at databaseadvisors.com] On Behalf Of Jim Lawrence
Sent: Monday, May 24, 2010 9:41 PM
To: 'Access Developers discussion and problem solving'
Subject: Re: [AccessD] Cannot close a form with Docmd.Close

Hi Bill:

There are a few comments about the code I will make.

1. Why are you setting all the objects to 'nothing'. If these are variables especially and if you didn't set them to Public, exiting from a form will close them automatically.

2. Avoid such names as Prp, lbl, Ctl, Frm as the system can get confused with them; try PrpValues, lblNameGeorge, CtlNamePopdown, FrmGeorge and considering non descriptive naming would confuse you when going through the code a year later... Recordsets are one of the exception when closing and setting to nothing.

3. If you are closing the current form you might check to see if the form you are closing is the form you want to close:

strFrmName = Me.Form.Name
DoCmd.Close acForm, strFrmName, acSaveYes

4. If that does not work check to see if all your references are correct...
through one of your Modules. If you do not already have one, create a dummy one and go into design mode then you will have access to the above mentioned feature.

5. While in a Module the run 'Debug' and 'save'. Debug will catch a lot of programmer errors.

6. If that does not work make a new form and drag and drop the code and fields into the new form... sometimes a form just gets corrupted and can not be fixed.

HTH
Jim


-----Original Message-----
From: accessd-bounces at databaseadvisors.com
[mailto:accessd-bounces at databaseadvisors.com] On Behalf Of Bill Benson
(vbacreations)
Sent: Monday, May 24, 2010 2:06 PM
To: 'Access Developers discussion and problem solving'
Subject: [AccessD] Cannot close a form with Docmd.Close

I am trying to close a form. A lot was done on this form before I get to the line to close it, but I am being told "The close action was canceled".

I later added a whole bunch of cleanup lines, and it went through everything and DID close immediately after I added these lines. Then I thought, well why not test them one by one to see which helped. So I turned them all off one at a time and found none helped. Thinking I was going insane, I added all the lines back and the form wouldn't close. Can someone tell me what might be going on without looking at all the code? Nothing I know of could be clinging to the form. Debug.Print Forms.count = 1. The form was opened with this line:

DoCmd.OpenForm MyTemplate, acDesign, , , acFormEdit, acHidden


TIA!

Set T = Nothing
Set D = Nothing
Set R = Nothing
Set Prp = Nothing
Set Lbl = Nothing
Set LastControl = Nothing
Set Txt = Nothing
Set Ctl = Nothing
Set Frm = Nothing
Set F = Nothing
ReDim VarOriginalControls(0)
ReDim ArSkip(0)

DoCmd.Close acForm, FrmName, acSaveYes

--



More information about the AccessD mailing list