PC Review


Reply
Thread Tools Rate Thread

Counting Number Records per Unique ID, saving it to another table:

 
 
=?Utf-8?B?YWRuYQ==?=
Guest
Posts: n/a
 
      5th Nov 2007
I have a form frmVendors with a subform frmPayments. The main form shows
information on vendors, and the subform is a continuous form which lists the
checks paid out to each vendor. Users are typically entering new payments
into the subform on the main form.

The forms are linked to tables: Linked by VendorID (one to many)
tblVendor: Key=VendorID, Vendor Name, Vendor Address, Vendor State, Vendor
City
tblPayments: Key=CheckID, VendorID, PaymentAmount

There is a field in tblVendor called NumberChecks to show the total amount
of checks paid out to that vendor. There is a text box on the main form to
show NumberChecks. Right now, the user must increment NumberChecks manually
when they add a new payment amount.

I need a way to count the number of checks for each VendorID in tblPayments,
and store it in tblVendors in the NumberChecks field. I also want the
NumberChecks field on frmVendors to update when a new payment is added or if
a payment is deleted.
Any Ideas?

 
Reply With Quote
 
 
 
 
Jeff Boyce
Guest
Posts: n/a
 
      5th Nov 2007
Stop storing the "number of checks"! This value can be derived using a
simple Totals query, or by using the DCount() function.

That way, no one needs to remember to update the field.

Regards

Jeff Boyce
Microsoft Office/Access MVP

"adna" <(E-Mail Removed)> wrote in message
news:F6F614FB-E27C-491A-AB5D-(E-Mail Removed)...
>I have a form frmVendors with a subform frmPayments. The main form shows
> information on vendors, and the subform is a continuous form which lists
> the
> checks paid out to each vendor. Users are typically entering new payments
> into the subform on the main form.
>
> The forms are linked to tables: Linked by VendorID (one to many)
> tblVendor: Key=VendorID, Vendor Name, Vendor Address, Vendor State,
> Vendor
> City
> tblPayments: Key=CheckID, VendorID, PaymentAmount
>
> There is a field in tblVendor called NumberChecks to show the total amount
> of checks paid out to that vendor. There is a text box on the main form
> to
> show NumberChecks. Right now, the user must increment NumberChecks
> manually
> when they add a new payment amount.
>
> I need a way to count the number of checks for each VendorID in
> tblPayments,
> and store it in tblVendors in the NumberChecks field. I also want the
> NumberChecks field on frmVendors to update when a new payment is added or
> if
> a payment is deleted.
> Any Ideas?
>



 
Reply With Quote
 
Douglas J. Steele
Guest
Posts: n/a
 
      5th Nov 2007
You shouldn't store calculated values. As fellow MVP John Vinson likes to
say, "Storing derived data such as this in your table accomplishes three
things: it wastes disk space; it wastes time (almost any calculation will be
MUCH faster than a disk fetch); and most importantly, it risks data
corruption. If one of the underlying fields is subsequently edited, you will
have data in your table WHICH IS WRONG, and no automatic way to detect that
fact. "

Use a query to calculate the total. The SQL would be something like:

SELECT VendorID, [Vendor Name], [Vendor Address], Count(*) AS NumberChecks
FROM tblVendor INNER JOIN tblPayments
ON tblVendor.VendorID = tblPayments.VendorID
GROUP BY VendorID, [Vendor Name], [Vendor Address]

Use that query anywhere you would otherwise have used tblVendor.

--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)


"adna" <(E-Mail Removed)> wrote in message
news:F6F614FB-E27C-491A-AB5D-(E-Mail Removed)...
>I have a form frmVendors with a subform frmPayments. The main form shows
> information on vendors, and the subform is a continuous form which lists
> the
> checks paid out to each vendor. Users are typically entering new payments
> into the subform on the main form.
>
> The forms are linked to tables: Linked by VendorID (one to many)
> tblVendor: Key=VendorID, Vendor Name, Vendor Address, Vendor State,
> Vendor
> City
> tblPayments: Key=CheckID, VendorID, PaymentAmount
>
> There is a field in tblVendor called NumberChecks to show the total amount
> of checks paid out to that vendor. There is a text box on the main form
> to
> show NumberChecks. Right now, the user must increment NumberChecks
> manually
> when they add a new payment amount.
>
> I need a way to count the number of checks for each VendorID in
> tblPayments,
> and store it in tblVendors in the NumberChecks field. I also want the
> NumberChecks field on frmVendors to update when a new payment is added or
> if
> a payment is deleted.
> Any Ideas?
>



 
Reply With Quote
 
=?Utf-8?B?TGFuY2U=?=
Guest
Posts: n/a
 
      5th Nov 2007
if your subform is filtered to contain only checks for a single vendor you
can use the recordcount property of its recordset object to get the number of
checks for that vendor.

Or you could write a VBA function to open a recordset, run some SQL
statement, and return the record count of the recordset. Pretty much the
same effect, but wouldn't require the subform to be filter.. or even be open.


"adna" wrote:

> I have a form frmVendors with a subform frmPayments. The main form shows
> information on vendors, and the subform is a continuous form which lists the
> checks paid out to each vendor. Users are typically entering new payments
> into the subform on the main form.
>
> The forms are linked to tables: Linked by VendorID (one to many)
> tblVendor: Key=VendorID, Vendor Name, Vendor Address, Vendor State, Vendor
> City
> tblPayments: Key=CheckID, VendorID, PaymentAmount
>
> There is a field in tblVendor called NumberChecks to show the total amount
> of checks paid out to that vendor. There is a text box on the main form to
> show NumberChecks. Right now, the user must increment NumberChecks manually
> when they add a new payment amount.
>
> I need a way to count the number of checks for each VendorID in tblPayments,
> and store it in tblVendors in the NumberChecks field. I also want the
> NumberChecks field on frmVendors to update when a new payment is added or if
> a payment is deleted.
> Any Ideas?
>

 
Reply With Quote
 
=?Utf-8?B?YWRuYQ==?=
Guest
Posts: n/a
 
      5th Nov 2007
Hi all,
Thanks for the advice. No one ever told me not to store calculated fields
(i'm self taught) but it does make sense. I had been working on a solution
where I had a hidden field txtCheckCount on the subform with control source
=Count([PaymentAmount]).
Then on the main form I had an unbound field with control source
=Forms!frmVendor![frmPayment]!txtCheckCount

This was working to show the number of checks but I wasn't able to store it
in the database. Now I will simply take the NumberChecks field out of
tblVendor calculate the value where needed.

Thanks!

"adna" wrote:

> I have a form frmVendors with a subform frmPayments. The main form shows
> information on vendors, and the subform is a continuous form which lists the
> checks paid out to each vendor. Users are typically entering new payments
> into the subform on the main form.
>
> The forms are linked to tables: Linked by VendorID (one to many)
> tblVendor: Key=VendorID, Vendor Name, Vendor Address, Vendor State, Vendor
> City
> tblPayments: Key=CheckID, VendorID, PaymentAmount
>
> There is a field in tblVendor called NumberChecks to show the total amount
> of checks paid out to that vendor. There is a text box on the main form to
> show NumberChecks. Right now, the user must increment NumberChecks manually
> when they add a new payment amount.
>
> I need a way to count the number of checks for each VendorID in tblPayments,
> and store it in tblVendors in the NumberChecks field. I also want the
> NumberChecks field on frmVendors to update when a new payment is added or if
> a payment is deleted.
> Any Ideas?
>

 
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
Counting unique records jj Microsoft Access 9 10th Apr 2008 11:36 PM
Listbox for counting number of records in table =?Utf-8?B?UGV0ZXIgQWRlbWE=?= Microsoft Access VBA Modules 5 18th Feb 2005 08:17 AM
Counting Number of Records Related to a Parent Table =?Utf-8?B?ZGFycmVw?= Microsoft Access Reports 6 26th Jan 2005 04:08 AM
Counting Unique Records Andy Microsoft Excel Programming 2 14th Oct 2004 11:34 AM
Counting number of records in a table Rosemary Microsoft Access VBA Modules 2 18th Sep 2004 02:28 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:12 PM.