Error 2501 - The OpenForm action was cancelled

G

Guest

Access 2000

Hi I'm trying to open a form using a command button - code as follows:

Private Sub cmd_lta_Click()
On Error GoTo Err_cmd_lta_Click

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "frm_bpd_lta"

stLinkCriteria = "[member]=" & Me![memberkey]
DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_cmd_lta_Click:
Exit Sub

Err_cmd_lta_Click:
MsgBox Err.Description, , Err.Number
Resume Exit_cmd_lta_Click
End

The form has the following code in it's open and load events:

Private Sub Form_Open(Cancel As Integer)
If Me.Recordset.BOF = True And Me.Recordset.EOF = True Then
Me.AllowAdditions = True
DoCmd.GoToRecord , , acNewRec
Me.Member.DefaultValue = [Forms]![frm_bpd]![memberkey]


End If

End Sub


Private Sub Form_Load()
cmd_edit_save.Visible = False
cmd_edit_cancel.Visible = False
If PrtctnType.Value = "E" Then
Me.PrtctdLSAmount.Visible = False
Me.PrtctdLSPercent.Visible = True
Else
Me.PrtctdLSAmount.Visible = True
Me.PrtctdLSPercent.Visible = False
End If
End Sub

When click on the cmd button I get error 2501. This seems to be regardless
of whether the member has a record in the LTA table

Anyone able to help?

Thanks in advance
Simon
 
W

Wayne Morgan

The 2501 error would normally be received if you Cancel the opening of the
form in the form's Open event. The code you show here doesn't do that. Are
you doing anything else to close the form?

You are trying to manipulate the form's Recordset in the Open event. The
recordset is not guaranteed to be available until the Load event. Also, if
you AllowAdditions and the form currently has no records, you will be at a
new record. There should be no reason to move to one. You are also setting
the DefaultValue of a control after moving to a new record. When you move to
a new record, the DefaultValue of the control will be picked up, but that
value should already be set. Once you're at the new record, you simply need
to set the Value of the control, not the DefaultValue, unless you want it
there for the next new record you move to.
 
G

Guest

Thanks Wayne

I moved the code from the Open event in to a command button on the form and
it works fine now

Wayne Morgan said:
The 2501 error would normally be received if you Cancel the opening of the
form in the form's Open event. The code you show here doesn't do that. Are
you doing anything else to close the form?

You are trying to manipulate the form's Recordset in the Open event. The
recordset is not guaranteed to be available until the Load event. Also, if
you AllowAdditions and the form currently has no records, you will be at a
new record. There should be no reason to move to one. You are also setting
the DefaultValue of a control after moving to a new record. When you move to
a new record, the DefaultValue of the control will be picked up, but that
value should already be set. Once you're at the new record, you simply need
to set the Value of the control, not the DefaultValue, unless you want it
there for the next new record you move to.

--
Wayne Morgan
MS Access MVP


Simon Cleal said:
Access 2000

Hi I'm trying to open a form using a command button - code as follows:

Private Sub cmd_lta_Click()
On Error GoTo Err_cmd_lta_Click

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "frm_bpd_lta"

stLinkCriteria = "[member]=" & Me![memberkey]
DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_cmd_lta_Click:
Exit Sub

Err_cmd_lta_Click:
MsgBox Err.Description, , Err.Number
Resume Exit_cmd_lta_Click
End

The form has the following code in it's open and load events:

Private Sub Form_Open(Cancel As Integer)
If Me.Recordset.BOF = True And Me.Recordset.EOF = True Then
Me.AllowAdditions = True
DoCmd.GoToRecord , , acNewRec
Me.Member.DefaultValue = [Forms]![frm_bpd]![memberkey]


End If

End Sub


Private Sub Form_Load()
cmd_edit_save.Visible = False
cmd_edit_cancel.Visible = False
If PrtctnType.Value = "E" Then
Me.PrtctdLSAmount.Visible = False
Me.PrtctdLSPercent.Visible = True
Else
Me.PrtctdLSAmount.Visible = True
Me.PrtctdLSPercent.Visible = False
End If
End Sub

When click on the cmd button I get error 2501. This seems to be regardless
of whether the member has a record in the LTA table

Anyone able to help?

Thanks in advance
Simon
 

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