Form_Current event not functioning quite correctly

  • Thread starter Thread starter Jasonm
  • Start date Start date
J

Jasonm

Hi all (again!)

I am using the following code to show current position in a set of records
on a custom record counter:

Private Sub Form_Current()
Dim rs As Recordset, intCount As Integer
Dim strPosition As String ' Define Variables
Set rs = Me.RecordsetClone ' clone recordset to count records
strPosition = "Record No. " & Me.CurrentRecord & _
" of " & rs.RecordCount ' build a string with the position and count
txtPosition.Value = strPosition
End Sub

There may be an easier way to do this, but this was the only way that I
could figure out... The code works great when scrolling from record to
record, however it shows record 1 of 1 when the form loads and record 2 of
400 when I scroll to record #2. If I insert a break point at the point where
I set the value of strPosition the form loads with the correct value!

Does anyone have any ideas?

Thanks to all who have helped me in the past! I learn more by scrolling
through these groups in a couple o fhours than through weeks of classroom
training!

Jm
 
Hi Jason

I think your initial Current event is firing before the whole recordsource
is loaded. Try adding this line after Set rs:
If rs.RecordCount = 1 Then rs.MoveLast
 
Jasonm said:
Hi all (again!)

I am using the following code to show current position in a set of
records on a custom record counter:

Private Sub Form_Current()
Dim rs As Recordset, intCount As Integer
Dim strPosition As String ' Define Variables
Set rs = Me.RecordsetClone ' clone recordset to count records
strPosition = "Record No. " & Me.CurrentRecord & _
" of " & rs.RecordCount ' build a string with the position and
count txtPosition.Value = strPosition
End Sub

There may be an easier way to do this, but this was the only way that
I could figure out... The code works great when scrolling from record
to record, however it shows record 1 of 1 when the form loads and
record 2 of 400 when I scroll to record #2. If I insert a break point
at the point where I set the value of strPosition the form loads with
the correct value!

Does anyone have any ideas?

Thanks to all who have helped me in the past! I learn more by
scrolling through these groups in a couple o fhours than through
weeks of classroom training!

Jm

You need to force the recordsetclone to access the last record, before
the RecordCount property will be accurate. Do this:
 
Thanks fo rthe reply Graham. Do you suppose that when I am stepping through
the code in the debugger that the record set is finishing loading in the
background?
Your suggestion should fix the problem. (otherwise I'll be beggin' for help
again here in a couple of hours!)

Thanks again!
Jason
Graham Mandeno said:
Hi Jason

I think your initial Current event is firing before the whole recordsource
is loaded. Try adding this line after Set rs:
If rs.RecordCount = 1 Then rs.MoveLast
--
Good Luck!

Graham Mandeno [Access MVP]
Auckland, New Zealand


Jasonm said:
Hi all (again!)

I am using the following code to show current position in a set of
records on a custom record counter:

Private Sub Form_Current()
Dim rs As Recordset, intCount As Integer
Dim strPosition As String ' Define Variables
Set rs = Me.RecordsetClone ' clone recordset to count records
strPosition = "Record No. " & Me.CurrentRecord & _
" of " & rs.RecordCount ' build a string with the position and count
txtPosition.Value = strPosition
End Sub

There may be an easier way to do this, but this was the only way that I
could figure out... The code works great when scrolling from record to
record, however it shows record 1 of 1 when the form loads and record 2
of 400 when I scroll to record #2. If I insert a break point at the point
where I set the value of strPosition the form loads with the correct
value!

Does anyone have any ideas?

Thanks to all who have helped me in the past! I learn more by scrolling
through these groups in a couple o fhours than through weeks of classroom
training!

Jm
 
Dirk thanks for the help. Between you and Graham above I know that the
problem is solved. Why do you think that the code works reliably in the
debugger but not when run 'normally'?

Just curious

thanks again,
Jason
 
Jasonm said:
Dirk thanks for the help. Between you and Graham above I know that the
problem is solved. Why do you think that the code works reliably in
the debugger but not when run 'normally'?

Because the recordset is being loaded by a background thread. When you
step through in the debugger, you give it plenty of time to finish
loading.
 
Dirk, thanks much for the answer. It was one of those things that was going
to bug me... I figured it had to be a timing issue, but couln't quite figure
out why!

Thanks again. Enjoy the rest of your weekend.

Jason
 
Back
Top