View Full Version : subform events & record counts


AlanS
05-31-2001, 01:36 PM
I have a main form showing customer records, and a subform showing visit records. When I navigate between records on the main form, the subform correctly shows only those visit records linked to the currently displayed customer. On each form, I need to access both the current record number (CurRec) and the total number of records (TotRec) from VBA code. On the main form, I can do this easily with the following code:

Private Sub Form_Activate()
DoCmd.GoToRecord , , acLast
DoCmd.GoToRecord , , acFirst
End Sub

Private Sub Form_Current()
Dim CurRec As Integer, TotRec As Integer
...
CurRec = Me.CurrentRecord
TotRec = Me.RecordsetClone.RecordCount
...
End Sub

However, the same approach does not work on the subform, apparently at least in part because its Activate event never seems to fire, and its Open and Load events fire before the corresponding events on the main form. Since the two forms are linked, the total number of records in the subform must be recalculated each time the main form record changes.

Any suggestions?

AlanS
06-01-2001, 06:43 AM
I solved it with the following code, in the Current event for the main form. Now, I can generate accurate subform record counts without first navigating in the subform:

With [Visits subform]
.Visible = OldRec
If OldRec Then
On Error Resume Next
With .Form
.RecordsetClone.MoveLast
.Bookmark = [Visits subform].Form.RecordsetClone.Bookmark
.RecordsetClone.MoveFirst
.Bookmark = [Visits subform].Form.RecordsetClone.Bookmark
End With
End If
End With

OldRec is a Boolean variable indicating whether or not the current main form record is an existing record. The error trapping is necessary, because when the main form is opened, the Current event fires at least once before there is any current record, which causes an error in the MoveLast and MoveFirst methods.