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





"Marshall Barton" <marshbarton(a)wowway.com> wrote in message
news:n8fes5pjc78k5mkbkg231hag22tlbca8o7(a)4ax.com...
> 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]


From: Jon Lewis on
Please see the correction to my first reply in my reply to Marshall's post
below.


"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: Dennis on
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
--
Dennis

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

"Dennis" <Dennis(a)discussions.microsoft.com> wrote in message
news:DD6ED406-F01C-449E-A930-759C9B7A075E(a)microsoft.com...
> 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
> --
> Dennis
>


From: Marshall Barton on
Dennis wrote:
>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.


Both the Close and Unload events are triggered by anything
that tries to close the form, whether it be code in your
button or the form's X button, closing Access or whatever.

I think you should try using this kind of logic in the
Unload event:

If pstrCalledBy <> "" Then
Me.Visible = False 'hide the form, but
Cancel = True 'keep it open
Else
'doing nothing allows closing the form to proceed normally
End If

--
Marsh
MVP [MS Access]