[AccessD] Cannot close a form with Docmd.Close

Heenan, Lambert Lambert.Heenan at chartisinsurance.com
Tue May 25 09:28:06 CDT 2010


You can always pass a form object to your sub...

SomeSub Me,... other params

Them in SomeSub

Sub SomeSub(f as Form,... Any other parameters)
...
	Docmd.Close acForm, f.Name, acSaveNo
End Sub

-----Original Message-----
From: accessd-bounces at databaseadvisors.com [mailto:accessd-bounces at databaseadvisors.com] On Behalf Of Bill Benson (vbacreations)
Sent: Tuesday, May 25, 2010 9:37 AM
To: 'Access Developers discussion and problem solving'
Subject: Re: [AccessD] Cannot close a form with Docmd.Close

Thanks for these clarifications. I cannot use Me.Name because this is a subroutine in a standard module. 

I ended up rebuilding the database and got it to work without significant code changes, although I did remove all variable dumping (not the problem).
What may have helped was changing how I save and close. Rather than combine into one operation I broke it up into two. That way I could force the database to save *only* my form, and then close *it*. Sometimes with DoCmd.Close acForm, frmName, acSaveYes I would get asked about other objects being edited. That plus a combination of Application.Echo off might have been the problem. More likely, it was database corruption.

	'This is working now ... for now ... for whatever reasons
	DoCmd.Save acForm, FrmName
	DoCmd.Close acForm, FrmName, acSaveNo

Thank you gentlemen.
-----Original Message-----
From: accessd-bounces at databaseadvisors.com
[mailto:accessd-bounces at databaseadvisors.com] On Behalf Of Heenan, Lambert
Sent: Tuesday, May 25, 2010 9:09 AM
To: Access Developers discussion and problem solving
Subject: Re: [AccessD] Cannot close a form with Docmd.Close

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

--
--
AccessD mailing list
AccessD at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/accessd
Website: http://www.databaseadvisors.com No virus found in this incoming message.
Checked by AVG - www.avg.com
Version: 9.0.819 / Virus Database: 271.1.1/2891 - Release Date: 05/24/10 14:26:00


--
AccessD mailing list
AccessD at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/accessd
Website: http://www.databaseadvisors.com




More information about the AccessD mailing list