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

Form closes before errors or notifications can be posted

Belimisimus
P: 18
On close command I have code which ask me if I want to save changes before closing (yes, no, cancel).

Expand|Select|Wrap|Line Numbers
  1. Private Sub Close_Click()
  2.     bSaving = True
  3.     If Me.Dirty Then
  4.         answer = MsgBox("Do you want to save changes before closing?", vbYesNoCancel + vbQuestion, "Confirm Save")
  5.         If answer = vbNo Then
  6.             Me.Undo
  7.             DoCmd.Close acForm, Me.Name
  8.  
  9.         ElseIf answer = vbYes Then
  10.             On Error Resume Next
  11.             DoCmd.RunCommand acCmdSaveRecord
  12.             DoCmd.Close acForm, Me.Name
  13.         End If
  14.     Else
  15.         DoCmd.Close acForm, Me.Name
  16.     End If
  17.     bSaving = False
  18. End Sub

If I say yes then in before update there is a code which checks if all required fields are filled.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_BeforeUpdate(Cancel As Integer)
  2. If IsNull(Me![cbo_AssignedFrom]) Then
  3. MsgBox "You must enter who is Assign from before this Record can be saved"
  4. Cancel = True: Me![cbo_AssignedFrom].SetFocus
In case they aren't it notify me but still form is close. How to stop closing in that case?
Mar 26 '10 #1
Share this Question
Share on Google+
6 Replies


NeoPa
Expert Mod 15k+
P: 31,489
The Cancel parameter in a Form_BeforeUpdate() event procedure simply specifies whether the Update should proceed. It has no effect on any related closing of the form. There wouldn't necessarily even be a related close if the save were called from elsewhere for instance.

Furthermore, there is no BeforeClose event to handle. The facility to cancel such a close is not supported. You need to ensure your code doesn't call for a close when it is not ready for one. You could do this by trying to save the record explicitly on dirty before the close. If the save failed (Check the Err object) then clearly you wouldn't want the close to proceed.
Mar 26 '10 #2

NeoPa
Expert Mod 15k+
P: 31,489
By the way, your Close_Click() procedure closes even when the operator chooses Cancel (vbCancel). This makes little sense. If you don't want to offer the facility to cancel then use the vbYesNo option instead.
Mar 26 '10 #3

Belimisimus
P: 18
@NeoPa
The cancel option is working. It stops closing and there is no questions from before update code!?


You need to ensure your code doesn't call for a close when it is not ready for one. You could do this by trying to save the record explicitly on dirty before the close. If the save failed (Check the Err object) then clearly you wouldn't want the close to proceed.
I'm newbie about vba, and most of codes I find (learn) surfing the net... could you give me an example of code for this solution?
Mar 29 '10 #4

NeoPa
Expert Mod 15k+
P: 31,489
@Belimisimus
You are quite right. My mistake. I misread your code.
@Belimisimus
What are you talking about? This is after a quote which doesn't mention this. I'm confused about what you're trying to say.
@Belimisimus
I'm happy to work with you, but I won't simply produce code for you. That said, if you make a try and tell me where you get confused or stuck I can certainly help.
Mar 29 '10 #5

Belimisimus
P: 18
I really appreciate your help and I didn't want from you to write a code instead of me. All I didn't know what the "Check the Err object" means so I ask for example (some times better way of explanation).

One way or another your solution was helpfull. Code below is working for me...hope that this is what you sugesst!

Expand|Select|Wrap|Line Numbers
  1. "On Error GoTo Close_Click_Err" 
  2. ...code...
  3. Close_Click_Exit:
  4.     Exit Sub
  5. Close_Click_Err:
  6.     'MsgBox Error$
  7.     Resume Close_Click_Exit
Thx again!
Mar 29 '10 #6

NeoPa
Expert Mod 15k+
P: 31,489
That makes better sense. I will explain the Err object further (but for a better understanding simply type in "Err" on a new line in your code and press F1 for context-sensitive help).

Err is set after any executed piece of code that can fail. There are many possible failures, and Err will give a Number and Description for whatever caused the problem.

The following code gives a very brief illustration :
Expand|Select|Wrap|Line Numbers
  1. On Error Resume Next
  2. Call DoCmd.OpenForm(Forms(-1))
  3. If Err.Number = 2456 Then
  4.     Call MsgBox(Err.Description)
  5. End If
Mar 30 '10 #7

Post your reply

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