PC Review


Reply
Thread Tools Rate Thread

Count(*) in form

 
 
Judy
Guest
Posts: n/a
 
      19th Apr 2006
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

 
Reply With Quote
 
 
 
 
Dirk Goldgar
Guest
Posts: n/a
 
      19th Apr 2006
"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)


 
Reply With Quote
 
 
 
 
Judy
Guest
Posts: n/a
 
      19th Apr 2006
Thanks Dirk. I am obviously a novice. Record sets are a mystery to me.
I use forms so i can write little bits of code at a time and make sure
they are working. I'll get out my "For Dummies" book and see if I can
come up to speed in record sets, because that will ultimatley be the
most efficient way to go. Much thanks. Judy

 
Reply With Quote
 
Dirk Goldgar
Guest
Posts: n/a
 
      19th Apr 2006
"Judy" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)
> Thanks Dirk. I am obviously a novice. Record sets are a mystery to
> me. I use forms so i can write little bits of code at a time and
> make sure they are working. I'll get out my "For Dummies" book and
> see if I can come up to speed in record sets, because that will
> ultimatley be the most efficient way to go. Much thanks. Judy


You're welcome, Judy. The main thing to remember is that forms do not
hold data, they're just windows on the tables that actually hold the
data. Forms provide a means for the user to view and edit the data, but
for manipulating data in code, other mechanisms are much more efficient.
Generally, executing action queries (e.g., update, delete, and append
queries) is most efficient, and recordset operations are less so -- but
still more efficient than manipulating the Form user-interface object.

Good luck in your learning! It will be very rewarding. And come on
back to the newsgroups when you have questions. Google Groups is a
great tool, too, since most questions you might have have been asked
before.

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Temporary Form with 2 Sub form Update Main Form And its Sub form ? Jeanette Cunningham Microsoft Access Form Coding 0 25th Nov 2009 03:27 AM
Link for sub form on form to sub form in same form billybanter67 Microsoft Access Forms 2 24th May 2008 09:32 AM
Sub-Form within a Sub-Form within a Form Michael Regan via AccessMonster.com Microsoft Access Forms 2 6th Apr 2005 12:21 AM
Company Form which opens a Client form and returns the client idto the COmpany form Michael C. Blair Microsoft Access 1 4th Dec 2004 07:00 AM
how do I keep a form open but only process code after the form after a certain button on the form is clicked? Keith G Hicks Microsoft Access Form Coding 1 3rd Dec 2003 09:14 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:23 AM.