From: John W. Vinson on
On Wed, 14 Apr 2010 17:38:04 -0700, Dennis <Dennis(a)discussions.microsoft.com>
wrote:

>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.
--

John W. Vinson [MVP]
From: Dennis on
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
From: Jeanette Cunningham on
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


"Dennis" <Dennis(a)discussions.microsoft.com> wrote in message
news:78D02E57-F200-4DD3-9338-6957297BE373(a)microsoft.com...
> 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


From: Jon Lewis on
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





"Dennis" <Dennis(a)discussions.microsoft.com> wrote in message
news:78D02E57-F200-4DD3-9338-6957297BE373(a)microsoft.com...
> 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


From: Marshall Barton on
Jon Lewis wrote:

>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


There's no need for that kind of complication once the code
logic is cleared up. I.e. it's just a mess if the
close/unload events try to close the form.

--
Marsh
MVP [MS Access]