Problem with closing from from Close Button

D

Dennis

Hi,

I'm running Access via Office XP Pro on Windows 7.

I've already read the other forum discussion on this subject & I'm still
having a problem.

I have a form called frmWarranty_Search. It can be run from either a main
menu program (in which case the OpenArgs is set to ""). Or it can be run
from other programs (in which case the calling program's name is set in
OpenArgs).

In the form, I have a close button. Here is the code:
-------- S t a r t C o d e ------------
Private Sub cbClose_Click()

If pstrCalledBy = "" Then
DoCmd.Close acForm, Me.Name ' Close
the form
Else
Me.Visible = False ' Hide
the form & let calling form close
End If

End Sub
----------- E n d C o d e -------------------

pstrCalledBy is set by the OnLoad event. If OpenArgs = "", then this
variable is set to "" otherwise it has the name of the calling program.

When I run the form directly the the Access forms menu or calling it from
one of the programs, I receive the following error when I hit the close
button:

Microsoft Visual Basic
Run-time error '2501'

The Close action was canceled.

I the have a End, Debug, or Help button.

I hit the debug button and it takes me to the line:

DoCmd.Close acForm, Me.Name ' Close
the form

I lookup up the value of me.name and it is the form name.
I also changed Me.Name to cpstrFormName, which is a constant with the form'a
name.

I still received the same error. How do I fix this?

Thanks,

Dennis
 
D

Dennis

Marshall,

Your comment: That error indicates that the form can not be closed at that
point. Common causes are:

1. code in the form's Close or Unload
I have no code in the Unload event.

Here is the code in the cbClose and On Close Event:

Private Sub cbClose_Click()
Call Form_Close
End Sub


Here is code in On Close event:

Private Sub Form_Close()
If pstrCalledBy = "" Then
DoCmd.Close acForm, Me.Name ' Close
the form
Else
Me.Visible = False ' Hide
the form & let calling form close
End If
End Sub


2. if the form is bound, the current record can not be saved because it
fails a validation check.

This is supposed to be a display only screen. There is no way to alter the
data when it is displayed. Currently, this is a continuous form. There is
an invisible button that is layed over the data control fields so there is no
way to alter the data fields.

How do I make sure it is a display only form (other than the unbound fields
at the top which I used for selecting the customer and invoice).?


Any way to determine what is cancelling the update?

Dennis
 
B

Bob Quintal

Marshall,

Your comment: That error indicates that the form can not be
closed at that point. Common causes are:

1. code in the form's Close or Unload
I have no code in the Unload event.

Here is the code in the cbClose and On Close Event:

Private Sub cbClose_Click()
Call Form_Close
End Sub


Here is code in On Close event:

Private Sub Form_Close()
If pstrCalledBy = "" Then
DoCmd.Close acForm, Me.Name
' Close
the form
Else
Me.Visible = False
' Hide
the form & let calling form close
End If
End Sub


2. if the form is bound, the current record can not be saved
because it fails a validation check.

This is supposed to be a display only screen. There is no way to
alter the data when it is displayed. Currently, this is a
continuous form. There is an invisible button that is layed over
the data control fields so there is no way to alter the data
fields.

How do I make sure it is a display only form (other than the
unbound fields at the top which I used for selecting the customer
and invoice).?


Any way to determine what is cancelling the update?

Dennis
You are calling the form_close event
when calling the form_close event
when calling the form_close event
when calling the form_close event
when calling the form_close event
.....ad infinitum

Move the code to the cbClose_Click event
 
D

Dennis

Bob,

I originally had the code in the cbClose_Click event and it caused the same
issue. However, maybe I know why.

Originally, I had the following code in the On Close Event:

Call cbClose_Click

Which would result in the same thing.


Ok, now that we know what is happening, here is the next question.

My objective is:
If the form is called by an external form, then the form should be make
invisible by Me.Visible = False. If the form was NOT called by external
form, then it should close.

This worked fine with the Close button, but did not work with the Close X
button. Therefore, I modified the On Close event. Obviously, this did not
work.

So how do I do the above?

Dennis
 
J

John W. Vinson

My objective is:
If the form is called by an external form, then the form should be make
invisible by Me.Visible = False. If the form was NOT called by external
form, then it should close.

I'd suggest passing some flag in the OpenArgs argument of the OpenForm method
to signify that it's being called from the external form. If you open the form
directly there'll be nothing in OpenArgs.
 
D

Dennis

John,

The issue I am having is not with passing arguments in the OpenArgs field.
I'm already doing that and it is working just fine.

The problem I am having is with the close routine. When I close my form, I
receive the following error message:

Microsoft Visual Basic
Run-time error '2501'

The Close action was canceled.

I the have a End, Debug, or Help button.

I hit the debug button and it takes me to the line:

DoCmd.Close acForm, Me.Name


Here is the code in the cbClose and On Close Event:

Private Sub cbClose_Click()
Call Form_Close
End Sub


Here is code in On Close event:

Private Sub Form_Close()
If pstrCalledBy = "" Then
DoCmd.Close acForm, Me.Name ' Close
the form
Else
Me.Visible = False ' Hide
the form & let calling form close
End If
End Sub


My question is how do I get the get my close routine to run when the user
clicks on the red x close button?

Dennis
 
J

Jeanette Cunningham

Rewrite the code like this

Here is the code in the cbClose and On Close Event:

Private Sub cbClose_Click()
If pstrCalledBy = "" Then
DoCmd.Close acForm, Me.Name ' Close
the form
Else
Me.Visible = False ' Hide
the form & let calling form close
End If
End Sub


You don't want that code in the on close event.
That is why you were getting that error.
the cbClose_Click was closing the form, then in the On Close event, you were
also closing the form.
However the form was already closed so couldn't be closed again.


Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia
 
J

Jon Lewis

Try the following

Put error handling in your close button code:

Private Sub cmdClose_Click()
On Error GoTo Err_cmdClose_Click
DoCmd.Close acForm, Me.Name
Exit_cmdClose_Click:
Exit Sub
Err_cmdClose_Click:
Dim Response As Integer
If Err.Number = 2501 Then
Response = acDataErrContinue
Else
Response = MsgBox("Error " & Err.Number & ": " & Err.Description)
End If
Resume Exit_cmdClose_Click
End Sub

Put your on close code in the Unload event:

Private Sub cmdClose_Click()
On Error GoTo Err_cmdClose_Click
DoCmd.Close acForm, Me.Name
Exit_cmdClose_Click:
Exit Sub
Err_cmdClose_Click:
Dim Response As Integer
If Err.Number = 2501 Then
Response = acDataErrContinue
Else
Response = MsgBox("Error " & Err.Number & ": " & Err.Description)
End If
Resume Exit_cmdClose_Click
End Sub

Private Sub Form_Unload(Cancel As Integer)
If Len(pstrCalledBy) > 0 Then
Cancel = True
Me.Visible = False
End If
End Sub
 
J

Jon Lewis

Sorry, I double pasted the Private Sub cmdClose_Click() code by mistake, so
it should have read as below. The OP specifically wanted the pstrCalledBy
logic to apply when the X button is used as well as the Command Button.

Private Sub cmdClose_Click()
On Error GoTo Err_cmdClose_Click
DoCmd.Close acForm, Me.Name
Exit_cmdClose_Click:
Exit Sub
Err_cmdClose_Click:
Dim Response As Integer
If Err.Number = 2501 Then
Response = acDataErrContinue
Else
Response = MsgBox("Error " & Err.Number & ": " & Err.Description)
End If
Resume Exit_cmdClose_Click
End Sub

and

Private Sub Form_Unload(Cancel As Integer)
If Len(pstrCalledBy) > 0 Then
Cancel = True
Me.Visible = False
End If
End Sub
 
D

Dennis

Joh, Marshall,

Ah, I think I see my issue. I thought the On Close event was the starting
event when someone clicked on the red X close button. I take it that the On
Unload event is called when someone clicks on the red X close button.

I also lookup up access event order on the web and found some articles that
will help me understand the sequence better.

Ok. Let me play with that.

The bottom line is I want the form to behave the same regardless of whether
the close button of the red X button is clicked. Will the above code do
that? If it does, I will be putting that code in a couple of programs.

Thanks,

Dennis
 
J

Jon Lewis

The code will work for both your Close button and the X button of the form
(any indeed the application X button, Database Close and Access Exit menu
items). The order of events is important but the point about the Unload
event is that it has a Cancel argument.

When you set that to True in this event it will cancel whatever caused the
Form to commence unloading.

In the case of a Docmd method (as in your Command Button code) this will
cause error 2501 which the code in the error handler says in effect yeah I
know why I'm getting that error - just ignore it.

Jon
 

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