By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,154 Members | 1,050 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,154 IT Pros & Developers. It's quick & easy.

Strange Form_Close() Problem

P: 7
I have a strange problem while working on a MS Access db. The code is in VBA..

I have a main form A, and a button to call a subform B.
now the form B has some lists which inserts data. That part is working fine.
B also has "Submit" and "Close" button. But whenever i code for the "X" button on right hand top corner, this results in strange behavior.

Expand|Select|Wrap|Line Numbers
  1. Private sub Form_Close()
  2. cmdFormBClose()
  3. End Sub
  6. Private sub cmdFormBsubmit()
  7. ...
  8. <some code>
  9. ...
  10. Docmd.Close
  11. End Sub
  13. Private sub cmdFormBClose()
  14. <prompt to save>
  15. End Sub
Whenever it encounters Docmd.Close(then Form_Close is called,which is not wanted) in the 'cmdFormBClose' function, the main form A closes down.and the application give errors if anything is done on form B and closes down.
Why is Docmd.Close calling Form_close????
And Why does Form A close???
i want the submit to be independent of Form_Close.
I have also tried to add FormB.SetFocus before Docmd.Close but still Form A is closed.
Pls help uif u know something about this.
Thank you
Jan 2 '08 #1
Share this Question
Share on Google+
4 Replies

Expert 2.5K+
P: 2,653
Hi, there.

You will not prevent Form_Close event firing but you can toggle flag before you close the form from the code. And you should explicitely pass form name to DoCmd.Close, to ensure that "Form B" will be closed.
Expand|Select|Wrap|Line Numbers
  1. Dim blnDontPrompt as Boolean 'flag toggled before fom closing
  3. Private sub Form_Close()
  4. If Not blnDontPrompt Then cmdFormBClose()
  5. End Sub
  8. Private sub cmdFormBsubmit()
  9. ...
  10. <some code>
  11. ...
  12. blnDontPrompt = True  'toggle flag
  13. Docmd.Close acForm, "Form B"
  14. End Sub
  16. Private sub cmdFormBClose()
  17. <prompt to save>
  18. End Sub
Jan 2 '08 #2

Expert Mod 15k+
P: 31,494
Form_Close() will always fire when the form's closed. That's what it's designed to do.
A better approach might be to include in there (if you have any) the code which should be run whenever the form closes (however that is) then put the code you want to run in the procedure for the command button. This can finish with a DoCmd.Close() if you will.
I'm afraid that putting that code in the Form_Close() procedure is simply not the logical place for it.
Jan 2 '08 #3

P: 7
Thanks a lot, now I am almost done..
one round about i had used, and i dont know why...

for the Close button
Docmd.Close acForm , "FormB" , acSaveno

for Submit button

while for Form_Close()
Docmd.Close , "FormB" , acSaveno

I had to remove acForm, It took time finding this round-about, but eventually it worked.
Although i had to use flags to prevent the submit event call Form_Close()

Thanks again...
Jan 3 '08 #4

Expert Mod 15k+
P: 31,494
I'm not sure exactly what you're trying to say here, but I did notice that you are closing the form, ..., in the procedure which HANDLES the form closing (Form_Close()). Clearly this will cause you some problems as it is essentially calling itself interminably. I suggest you remove that line of code from your procedure.
Jan 3 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.