RecordCount error with New Record

F

Freeflyer

Hi,

I have a confusing problem when I try to obtain the RecordCount of a new
record.

I have a main form with two tabbed pages, one of which contains a subform.
If the subform has any records I wish to show that tab, otherwise I want the
other tab to display. I have the following code in the OnCurrent event of the
main form so that it is triggered everytime a new record is displayed.

If Me!sfrmQuoteDetail.Form.Recordset.RecordCount = 0 Then
Me.tabQuote.Value = 0
Else
Me.tabQuote.Value = 1
End If

This code works fine for me normally. If I open the form I see the correct
tab (tab1 usually) and when I navigate to a new record it shows tab0.
However, I then added the following code to the OnOpen event so that the form
would default to a new record.

Private Sub Form_Open(Cancel As Integer)
' On opening, go to new record
DoCmd.GoToRecord , , acNewRec
End Sub

Now I get an error message everytime I try to open the form:
"Runtime Error 2455: you entered an expression that has an invalid reference
to the property Form/Report."

Any idea why going straight to a new record should trigger this when opening
on an existing record before going to a new record does not?
 
A

Allen Browne

The problem may be simply that the subform has not loaded at the time you
attempt to run this code.

If that's the cause, a simple solution might be to add error handling to
trap and ignore this error, since it will only occur initially.

An even simpler solution might be to forget the code, and put something like
this into the Control Source of your text box:
=-([sfrmQuoteDetail].[Form].[RecordsetClone].[RecordCount]<>0)
 
F

Freeflyer

Hi Allen,

Thanks for the response. I am using the error handling solution at the
moment, but it isn't a 'neat' solution and so it bugs me. :blush:)

I did wonder about hether or not the subform was loaded, but given that it
works ok without the OnOpen event code, I'd assumed that the subform must be
loading ok. Could the OnOpen code slow down the subform loading?

I'm not sure what difference your proposed code makes by using
RecordSetClone instead of RecordSet. The code is being used in a VBA module
to determine which page of a tab control to display by default, not in a
textbox. I tried changing my original code to use RecordSetClone, but it
didn't seem to have any effect.

Anyway, I can live with the error trapping solution if I have to, I just
thought there had to be a more elegant solution.

Allen Browne said:
The problem may be simply that the subform has not loaded at the time you
attempt to run this code.

If that's the cause, a simple solution might be to add error handling to
trap and ignore this error, since it will only occur initially.

An even simpler solution might be to forget the code, and put something like
this into the Control Source of your text box:
=-([sfrmQuoteDetail].[Form].[RecordsetClone].[RecordCount]<>0)

--
Allen Browne - Microsoft MVP. Perth, Western Australia

Reply to group, rather than allenbrowne at mvps dot org.

Freeflyer said:
I have a confusing problem when I try to obtain the RecordCount of a new
record.

I have a main form with two tabbed pages, one of which contains a subform.
If the subform has any records I wish to show that tab, otherwise I want
the
other tab to display. I have the following code in the OnCurrent event of
the
main form so that it is triggered everytime a new record is displayed.

If Me!sfrmQuoteDetail.Form.Recordset.RecordCount = 0 Then
Me.tabQuote.Value = 0
Else
Me.tabQuote.Value = 1
End If

This code works fine for me normally. If I open the form I see the correct
tab (tab1 usually) and when I navigate to a new record it shows tab0.
However, I then added the following code to the OnOpen event so that the
form
would default to a new record.

Private Sub Form_Open(Cancel As Integer)
' On opening, go to new record
DoCmd.GoToRecord , , acNewRec
End Sub

Now I get an error message everytime I try to open the form:
"Runtime Error 2455: you entered an expression that has an invalid
reference
to the property Form/Report."

Any idea why going straight to a new record should trigger this when
opening
on an existing record before going to a new record does not?
 

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