Why do I get Runtime Error 2501?

N

Niklas Östergren

Hi!

I do get runtime error 2501 when I try to close a form
[frmCriteriaPhonenumbers] from another forms Close_Event [frmCriteriaEmail]
using DoCmd.Close. But first I do check if the form´s open with help of
function IsLoaded (see below) which I know works ok. At least it worked
earlier.

What I could do is, offcourse, to take care of this runtime error (2501) in
an error event handler but is this realy neccesary and is it the best way to
deal with this?

TIA!
// Niklas


Hers the code where it breakes:
======================================
Private Sub Form_Close() ' In form [frmCriteriaEmail]

' If frmCriteriaMembers is open close it before close Me.
If IsLoaded("frmCriteriaMembers") Then
DoCmd.Close acForm, "frmCriteriaMembers", acSaveNo
End If

' If frmCriteriaPhonenumbers is open close it before close Me.
If IsLoaded("frmCriteriaPhonenumbers") Then
DoCmd.Close acForm, "frmCriteriaPhonenumbers", acSaveNo '***(Here´s
the break)***
End If
End Sub
========================================

Her´s the code for IsLoaded:
=========================================

Function IsLoaded(ByVal strFormName As String) As Integer

'***************************************************************************
**
' Description: Returnes True if form is opened.
' Returns: Integer (True/False)
' Argument: String (FormName)
' Author: Unknown
' Date: ?? ?? ??
'***************************************************************************
***
Const conObjStateClosed = 0
Const conDesignView = 0

If SysCmd(acSysCmdGetObjectState, acForm, strFormName) <>
conObjStateClosed Then
If Forms(strFormName).CurrentView <> conDesignView Then
IsLoaded = True
End If
End If

End Function
===================================================
 
G

Graham Mandeno

Hi Niklas

Error 2501 indicates that an action was cancelled, either by the user or in
code.

Are you sure that you don't have something that is cancelling the closure of
the form? For example, if frmCriteriaPhonenumbers has a Form_Unload event
procedure which sets Cancel=True then this error will be raised.
 
N

Niklas Östergren

OK! I have learnt something new today thanks to you!

I do have code in Form_Unload event for frmCriteriaPhonenumbers (which
stores selected criteria in tblStorage, using DAO method) but I don´t set
Cancel = True anywhere else than in cmdCancel which I use to close the form
frmCriteriaPhonenumbers.

What is actually going to happen is that a report is going to bee launched
and in that form´s Open event I close the form´s that is opened (one of them
is frmCriteriaPhonenumbers ).

With your description it can either be the report trying to close the form
OR the code that I use to store selected value. Since I don´t know which I
have posted the codes below.

The reason to my Q is that I´m qurious and want to know what´s happening.
But I want you to know that I have allready taken care of this runtime error
with help of error handler so it´s not any major problem for me.

TIA!
// Niklas

Her´s the code in form frmCriteriaPhonenumbers Unload event:
========================================================
========================================================

' Declare database object and recordset object.
Dim intNewValue As Integer
Dim curNewValue As Currency
Dim booNewValue As Boolean
Dim strNewValue As String
Dim strNewDescription As String

' If NO criteria have been selected then don´t do anything.
If Not chkTypeOfPlace And Not chkHavePhonenumber And Not
chkPrivatePhonenumber Then Exit Sub


'====================================================
' HAVE PHONENUMBER
'====================================================

'***********************************************************************
' If have phone number selected then store value in tblStorage
If chkHavePhonenumber And Not Me.cboHavePhonenumber = "" Then
strNewValue = Me![cboHavePhonenumber]
strNewDescription = "Selected have phonenumber, " &
Me.cboHavePhonenumber & "."

Call SetStringValueIntblStorage("tblStorage", "CriteriaHavePhonenumber",
strNewValue, strNewDescription)

'If no value in cboHavePhonenumber or chkHavePhonenumber = False then
' set Null as value in tblStorage.
Else
strNewDescription = "Selected have phonenumber, " &
Me.cboHavePhonenumber & "."
Call SetNullStringValueIntblStorage("tblStorage",
"CriteriaHavePhonenumber", strNewDescription)

End If

'====================================================
' PRIVATE PHONENUMBER
'====================================================
'***********************************************************************
' If private phone number selected then store value in tblStorage
If chkPrivatePhonenumber And Not IsNull(Me.chkPrivatePhonenumber) Then
booNewValue = Me![chkPrivatePhonenumber]
strNewDescription = "Selected private phonenumbers, " &
Me.chkPrivatePhonenumber & "."

Call SetBooleanValueIntblStorage("tblStorage",
"CriteriaPrivatePhonenumber", booNewValue, strNewDescription)

'If no value in cboPrivatePhonenumber or chkPrivatePhonenumber = False then
' set Null as value in tblStorage.
Else
strNewDescription = "Selected private phonenumbers, " &
Me.chkPrivatePhonenumber & "."
Call SetNullBooleanValueIntblStorage("tblStorage",
"CriteriaPrivatePhonenumber", strNewDescription)

End If

'====================================================
' TYPE OF PLACE
'====================================================
'If there is a value in cboTypeOfPlace and chkTypeOfPlace = True then
' set new value in tblStorage.
If chkTypeOfPlace And Not IsNull(Me.cboTypeOfPlace) Then
intNewValue = Me![cboTypeOfPlace]
strNewDescription = "Selected type of place for e-mail, " &
Me.cboTypeOfPlace & "."

Call SetIntegerValueIntblStorage("tblStorage",
"CriteriaTypeOfPlaceForPhonenumbers", intNewValue, strNewDescription)

'If no value in cboTypeOfPlace or chkTypeOfPlace = False then
' set Null as value in tblStorage.
Else
strNewDescription = "Selected type of place for e-mail, " &
Me.cboTypeOfPlace & "."
Call SetNullIntegerValueIntblStorage("tblStorage",
"CriteriaTypeOfPlaceForPhonenumbers", strNewDescription)
End If


End Sub

==========================================
==========================================
Her´s the code in report open event:
==========================================
==========================================
Private Sub Report_Open(Cancel As Integer)

If IsLoaded("frmCriteriaMembers") Then
DoCmd.Close acForm, "frmCriteriaMembers", acSaveNo
End If

If IsLoaded("frmCriteriaPhonenumbers") Then
DoCmd.Close acForm, "frmCriteriaPhonenumbers", acSaveNo
End If

If IsLoaded("frmCriteriaEmail") Then
DoCmd.Close acForm, "frmCriteriaEmail", acSaveNo
End If

End Sub

===============================================
===============================================
And her´s an example of code used to stora values in tblStorage
===============================================
===============================================
Public Function SetStringValueIntblStorage(strTableName As String,
strVariableName As String, _
strValueToSetInTable As String, strDescriptionMsg As
String)


'strTableName Holds table name to store value in.
'strVariableName Holds variablename to seek for in table.
'curValueToSetInTable Holds value to set in table.
'strDescriptionMsg Holds description to put in table.

Dim db As Database, rst As Recordset


Set db = Currentdb
Set rst = db.OpenRecordset(strTableName)
rst.Index = "PrimaryKey"

rst.Seek "=", strVariableName

' If not found, create the entry.
If rst.NoMatch Then
rst.AddNew
rst![Variable] = strVariableName
rst![StringValue] = strValueToSetInTable
rst![Description] = strDescriptionMsg
rst.Update ' Update the recordset.

Else ' Else save the customer ID of the current record.

rst.Edit
rst![StringValue] = strValueToSetInTable
rst![Description] = strDescriptionMsg
rst.Update ' Update the recordset.

End If

rst.Close ' Close the recordset.

End Function


Graham Mandeno said:
Hi Niklas

Error 2501 indicates that an action was cancelled, either by the user or in
code.

Are you sure that you don't have something that is cancelling the closure of
the form? For example, if frmCriteriaPhonenumbers has a Form_Unload event
procedure which sets Cancel=True then this error will be raised.

--
Good Luck!

Graham Mandeno [Access MVP]
Auckland, New Zealand

Niklas Östergren said:
Hi!

I do get runtime error 2501 when I try to close a form
[frmCriteriaPhonenumbers] from another forms Close_Event [frmCriteriaEmail]
using DoCmd.Close. But first I do check if the form´s open with help of
function IsLoaded (see below) which I know works ok. At least it worked
earlier.

What I could do is, offcourse, to take care of this runtime error (2501) in
an error event handler but is this realy neccesary and is it the best
way
to
deal with this?

TIA!
// Niklas


Hers the code where it breakes:
======================================
Private Sub Form_Close() ' In form [frmCriteriaEmail]

' If frmCriteriaMembers is open close it before close Me.
If IsLoaded("frmCriteriaMembers") Then
DoCmd.Close acForm, "frmCriteriaMembers", acSaveNo
End If

' If frmCriteriaPhonenumbers is open close it before close Me.
If IsLoaded("frmCriteriaPhonenumbers") Then
DoCmd.Close acForm, "frmCriteriaPhonenumbers", acSaveNo '***(Here´s
the break)***
End If
End Sub
========================================

Her´s the code for IsLoaded:
=========================================

Function IsLoaded(ByVal strFormName As String) As Integer
'***************************************************************************
**
' Description: Returnes True if form is opened.
' Returns: Integer (True/False)
' Argument: String (FormName)
' Author: Unknown
' Date: ?? ?? ??
'***************************************************************************
***
Const conObjStateClosed = 0
Const conDesignView = 0

If SysCmd(acSysCmdGetObjectState, acForm, strFormName) <>
conObjStateClosed Then
If Forms(strFormName).CurrentView <> conDesignView Then
IsLoaded = True
End If
End If

End Function
===================================================
 

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