Unable to Close Form

Iceolate

Registered User.
Local time
Today, 22:57
Joined
Oct 21, 2011
Messages
17
Hi All,
I am trying to close a form with a subform.
I enter data in the main form and then the subform.
The main form has a save btn with an on click event which includes at the end of code
If Me.Dirty then
Me.Dirty = false
DoCmd.Close
DoCmd.OpenForm "FormName"

however the Form will only close and open the required form if I remove the two "Dirty" Code lines.
Anyone see where I am going wrong?
I am obviously trying to ensure that the data is saved on both the main and subforms using the If Me.Dirty......code

Thanks,
 
Code:
If Me.Dirty then Me.Dirty = false
DoCmd.Close 
DoCmd.OpenForm "FormName"
Or
Code:
If Me.Dirty then 
    Me.Dirty = false
[COLOR="Blue"]End If[/COLOR]
DoCmd.Close 
DoCmd.OpenForm "FormName"
 
Or to be even more specific,

PHP:
DoCmd.Close acForm, "YourFormNameHere"
 
Thank you vbaInet and burrina.
The End If after Me.Dirty = false
indeed fixed my problem.

I normally place my End Ifs at the end of my code but I get this feeling that as you suggested there are times when this is not correct. Unfortunately my VBA is not strong enough to make the distinction.

Thank you again, really appreciated.
 
Thanks vbaInet. The End If's don't usually cause me any problems but now I know I can try your suggestions if I run into similar problems.
Thanks again.
 
A good habit to adopt, to avoid these problems, is to always close off your statements before you begin writing the results.

So you first write this:
Code:
IF Criteria THEN

[COLOR="Blue"]END IF[/COLOR]
... close it off first.

Then write the result(s)
Code:
IF Criteria THEN
[COLOR="blue"]    result(s)[/COLOR]
END IF
If you adopt this habit (not just for IFs, but for everything else, Loops etc), you won't ever have these sort of problems. Unfortunately, in the VBA environment you have to close off pretty much everything except Functions and Subs.

Happy developing!
 
Thank you for taking the time to provide some follow up advice.
This will improve the "structure" of my code I am sure and as you say, hopefully reduce problems when I run the code.

Thanks again.
 

Users who are viewing this thread

Back
Top Bottom