"Judy" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)
> I am trying to "Do While" based on the number of records on a form.
> There is a text box on the form CNT which has a control source
> =Count(*). This accurately displays the record count. I then set a
> variable Cntr to be equal to the number of records. I had a bunch
> more code, but then I realized the cntr was not initializing so i took
> everthing else out. I added another test text box to my form (Text22)
> to verify that the counter was not initializing. Two odd things: 1) I
> use this exact scenario elsewhere and it works fine. 2) If the form is
> opened already it works.
>
> I have included my code and would be very grateful for any help.
> Thanks. Judy
>
>
> Private Sub Command48_Click()
> Dim Cntr As Integer
>
> DoCmd.OpenForm "frmMYVAr", acNormal, "", "", , acNormal
> DoCmd.GoToRecord acForm, "frmMyVar", acFirst
>
>
> Cntr = Forms![frmMyVar]![CNT]
> Forms![frmMyVar]![Text22] = Cntr
>
> Do While Cntr > 0
>
>
> Forms![frmMyVar]![ActForYtd] = "55"
>
>
> DoCmd.GoToRecord acForm, "frmMYVar", acNext
> Cntr = Cntr - 1
> Loop
>
>
> End Sub
I think the problem is that aggregate functions in controlsources are
executed asynchronously, so it's probably a timing issue: the control's
value hasn't been computed yet when you check it immediately after
opening the form. And since the form is already on the first record,
your "DoCmd.GoToRecord ... acFirst" doesn't delay things enough.
If you need the record count, it's probably safest and easiest to get it
from the form's RecordsetClone:
With Forms![frmMyVar].RecordsetClone
.MoveLast
Cntr = .RecordCount
End With
But what are you trying to accomplish with your sample code? It looks
like the whole thing could be accomplished by executing a single update
query, along the lines of
CurrentDb.Execute _
"UPDATE <recordsource of frmMyVAr> " & _
"SET ActForYtd = '55'",
dbFailOnError
I understand that your sample code may be much simplified over what
you're really trying to do, but still the approach of looping through
the records on a form just to update them in code seems much less
efficient than just updating the records in the table where they are
stored.
--
Dirk Goldgar, MS Access MVP
www.datagnostics.com
(please reply to the newsgroup)