Variable not working properly on Form_Load()

elsandalos

Registered User.
Local time
Today, 20:19
Joined
Jan 27, 2004
Messages
22
Hi all,
I'm using a variable called recordstotal to hold the total number of records on a form. I only need to initialise it once, on form load and i'm using the following code:



Dim recordstotal As Integer

Private Sub Form_Load()

recordstotal = Me.RecordsetClone.RecordCount

ContractNaviText = "Browsing Contract " & CurrentRecord & " of " & recordstotal

End Sub


This is not working as it should. I'm sure that the recordset should be loaded at this point, but recordstotal is staying at 1. The code works elsewhere (if i link it to a button for example) but not here, where i need it.

Any advice much appreciated.

Pete
 
Put it in the Form_Current event.

What version of Access are you using?
 
Hi Mile, thanks for the quick reply.

i've already tried this in Form_Current() and it does exactly the same. It returns the number 1, when there are actually 4 records. I have the same code running on a button which moves to the next record and it works fine there. So it's working when you change the record, but not when you first load the form.

I'm using Access 2002 SP-2 by the way

cheers
Pete
 
Ok,
looks like i solved this one. Seems that even when Form_Load() has happened, Access doesn't know how many records there are until you do something (like update or change a record), which is why i was getting '1' all the time. If you force a Me.RecordsetClone.MoveLast then Access will know how many records there are for when you do the RecordCount because it has already moved to the last one. Still seems that this shouldn't be necessary - Access should know everything after the form has been loaded, without you having to code it.

Code:
Private Sub Form_Load()

   Me.RecordsetClone.MoveLast
   recordstotal = Me.RecordsetClone.RecordCount

End Sub

Private Sub Form_Current()

   ContractNaviText = "Browsing Contract " & CurrentRecord & " of " & recordstotal

End Sub

cheers
El.
 
What you've done is called 'populating' the recordset.
 

Users who are viewing this thread

Back
Top Bottom