close form: command button

sarahb845

Registered User.
Local time
Today, 08:08
Joined
Jun 19, 2002
Messages
43
I have a command button (cmdClose) that closes my form OnClick.

I also have a Form_BeforeUpdate that checks to make sure a particular required field has been filled in. If it hasn't, it pops a message box to either set-focus to the field (click YES) or cancel the entire record (click NO).

My problem is with my cmdClose button in conjunction w/ the Form_BeforeUpdate. When cmdClose is clicked, the message box pops up correctly. However, when the YES button is clicked it closes the form anyway, even though it should set focus to the required field.

What's going on? Any help would be greatly appreciated!
sarah


Here's the code:

Private Sub Form_BeforeUpdate(Cancel As Integer)
On Error GoTo Err_Form_BeforeUpdate

Dim stdResponse As Integer

' If field not filled out then give the user this message
If IsNull(Me.txtCallerName) Then
stdResponse = MsgBox("You must enter a Caller Name before continuing." _
& vbCr & vbCr & "Do you want to continue?" & vbCr & vbCr & _
"Click YES to finish completing the record, or NO to delete the record.", _
vbYesNo, "Incomplete Record")

' if the user says yes, continue then return them to the field
If stdResponse = vbYes Then
Cancel = True
Me.txtCallerName.SetFocus
Exit Sub
'if they say no, stop the before update and undo the record
Else
Cancel = True
Me.Undo
End If
End If

Exit_Form_BeforeUpdate:
Exit Sub

Err_Form_BeforeUpdate:
MsgBox Err.Description
Resume Exit_Form_BeforeUpdate

End Sub

------

Private Sub cmdClose_Click()
On Error GoTo Err_cmdClose_Click

DoCmd.Close acForm, "frmPhoneLog", acSaveNo

Exit_cmdClose_Click:
Exit Sub

Err_cmdClose_Click:
MsgBox Err.Description
Resume Exit_cmdClose_Click

End Sub
 
I use something similar and I can't see anything different. I have my code on the OnClick event of the button, rather than the before update event.

You could try stepping through the code to see where it's going wrong, that might throw some light on the problem. Put the word Stop just under the line On Error GoTo Err_Form_BeforeUpdate and run the code. As the code runs it will highlight each line. Hit F8 and it will step you through each line separately. You can follow the progression and make sure it's doing what it should.
 
I need to keep the code in the BeforeUpdate b/c the user might go into the form and decide to close it w/o creating a new record. I only want the field required if they've actually created a new record. Keeping the code on the OnClick of the close button forces them to enter something in the field even if they don't want to create a new record.

So, I've kept the code as is, and added the Stop line. It steps through the BeforeUpdate code like it should. Except, after executing the BeforeUpdate, it executes the OnClick of the close button.

For ex:
1) User creates new record. Leaves the required field blank.
2) User clicks the Close cmd button.
3) Incomplete Record Msg box appears. User selects YES to return to required record. (Code correctly steps through the if statement.)
4) Form closes anyway and record is not saved.


I have a feeling it's some little thing, like a missing "." or something. argggg.
 
I would use the forms OnCose event. Since you only care if they created a new record, test if the record is "Dirty".

HTH
 
What I use is:

If IsNull (Me.Autonumberfieldname) or Me.autonumberfieldname = 0 then
DoCmd.Close
Exit Sub
End If

Put that before the other code so that if it's a new record (the autonumberfield has not incrimented) then it just closes the form, otherwise the other code will kick in.
 
Use a hidden boolean field called CanClose, add the following
Private Sub Form_Unload(Cancel As Integer)
If CanClose = False Then
Cancel = True

End If
End Sub
change the CanClose status dependant on your before update event
 
Thanks everyone for your help.

I went with DBL's response. I added that code to the cmdClose_Click. It seems to work now!
 

Users who are viewing this thread

Back
Top Bottom