Close form?

T

Tony Williams

If the user clicks No in this message I want the form to close, but I get a
message that says This action can't be carried out while processing a form
or report.
Here is my code
Private Sub txtMontha_LostFocus()
On Error GoTo Err_txtMontha_LostFocus
Dim MsgStr As String
Dim TitleStr As String
MsgStr = "You can not enter a record without " _
& "completing the Qtr End Date" & vbCrLf & vbCrLf _
& "Do You Wish To Go Back " _
& "And Enter The Qtr End Date?"
TitleStr = "Qtr End Date Must Be Entered"
If IsNull(Me!txtMontha) Or Me!txtMontha = "" Then
If MsgBox(MsgStr, vbYesNo, TitleStr) = vbYes Then
Cancel = True
Me.txtMonthlabela.SetFocus
Me.txtMontha.SetFocus

Else
Me.Undo
DoCmd.Close acForm, Me.Name
End If
End If
Exit_txtMontha_LostFocus:
Exit Sub

Err_txtMontha_LostFocus:
MsgBox Err.Description
Resume Exit_txtMontha_LostFocus
End Sub

Can anyone help?
Thanks
Tony
 
K

Ken Snell [MVP]

Why are you using the LostFocus event? That event will not let you reset the
focus as you wish. If your intent is to capture that the user is trying to
leave the control without having entered a value, then use the Exit event,
as it can be cancelled and thus keep the focus on the same textbox.

Private Sub txtMontha_Exit(Cancel As Integer)
On Error GoTo Err_txtMontha_LostFocus
Dim MsgStr As String
Dim TitleStr As String
MsgStr = "You can not enter a record without " _
& "completing the Qtr End Date" & vbCrLf & vbCrLf _
& "Do You Wish To Go Back " _
& "And Enter The Qtr End Date?"
TitleStr = "Qtr End Date Must Be Entered"
If IsNull(Me!txtMontha) Or Me!txtMontha = "" Then
If MsgBox(MsgStr, vbYesNo, TitleStr) = vbYes Then
Cancel = True
Else
Me.Undo
DoCmd.Close acForm, Me.Name
End If
End If
Exit_txtMontha_LostFocus:
Exit Sub

Err_txtMontha_LostFocus:
MsgBox Err.Description
Resume Exit_txtMontha_LostFocus
End Sub
 
T

Tony Williams

Thanks Ken
Tony
Ken Snell said:
Why are you using the LostFocus event? That event will not let you reset the
focus as you wish. If your intent is to capture that the user is trying to
leave the control without having entered a value, then use the Exit event,
as it can be cancelled and thus keep the focus on the same textbox.

Private Sub txtMontha_Exit(Cancel As Integer)
On Error GoTo Err_txtMontha_LostFocus
Dim MsgStr As String
Dim TitleStr As String
MsgStr = "You can not enter a record without " _
& "completing the Qtr End Date" & vbCrLf & vbCrLf _
& "Do You Wish To Go Back " _
& "And Enter The Qtr End Date?"
TitleStr = "Qtr End Date Must Be Entered"
If IsNull(Me!txtMontha) Or Me!txtMontha = "" Then
If MsgBox(MsgStr, vbYesNo, TitleStr) = vbYes Then
Cancel = True
Else
Me.Undo
DoCmd.Close acForm, Me.Name
End If
End If
Exit_txtMontha_LostFocus:
Exit Sub

Err_txtMontha_LostFocus:
MsgBox Err.Description
Resume Exit_txtMontha_LostFocus
End Sub
 
T

Tony Williams

Thanks Ken tried that but form still wont close still get the error message?
Tony
 
A

Andreas

1) Have had problems with Undo at various times not working properly.

2) Have you got code in the Form_Close event (or other events that may
interfere with closing the form)?

3) I used to use the various control events. Found it a pain in the ....
I now generally only use the Form_BeforeUpdate event and do everything
there. This way the user can do whatever they want in any order without
any unnecessary interference (after all, they are supposed to know what
they are doing) and validation kicks in only when it is actually
necessary (before actually saving anything). This way everything is done
in one place and generally there are no problems with things not working.

Regards,
Andreas
 
T

Tony Williams

Thanks Andreas. No code in Form_Close event. Tried in before Update still
had problems??????
Tony
 
A

Andreas

Does the form close?
Is the record undone or is it still sitting there in editing mode?
What does the error message say?
What action did you take (move to next record or trying to close the
form) to initiate the update and do they behave the same?
Can you post ALL the code behind this form?
 
T

Tony Williams

Thanks guys I've solved my problem. In the docmd.close line I just added the
name of the form instead of acForm, Me.Name and it worked. I
Thanks again
Tony
 
A

Andreas

Hmmm,
Didn't even think of this.
Try: DoCmd.Close acForm, Me.Form.Name

Regards,
Andreas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top