PC Review


Reply
Thread Tools Rate Thread

Calculate an amount for a group of records based on number of reco

 
 
Nigel
Guest
Posts: n/a
 
      21st Sep 2008
I have an invoice form and the amount of the invoice is to be shared between
a number of employees. THe employees to receive a portion of the invoice are
listed on a subform on the invoice form. I am getting a count of the number
of employees on a particular invoice. When I calculate the invoice I can
calculate the amount each employee gets (and it is always equal between those
employees) but I need a way to update each record with the amount that the
employee will recieve.

For example there are 3 employees on the invoice subform and the invoice is
for 600.00, therefor for each record on the employee subform (3 in total) the
amount field should be updated to 200.00 each

invoice form is called CustInvoice and subform is called empshare and the
field to be updated is called amount

Thanks in advance
 
Reply With Quote
 
 
 
 
code_monkey_number_9
Guest
Posts: n/a
 
      21st Sep 2008
First off, I would question whether you actually need to store this
information in the recordsource underlying the subform - you already have the
data stored in the form of the invoice amount / number of related records.
This is a simple calculation that can be made whenever you need that
particular data, without duplicating data already stored elsewhere. Not
saying you shouldn't, just saying I'd question the need.

You have a couple of problems facing whatever method you use to update this
field: one, if for some reason the invoice amount changes, you need to
recalculate the values stored in the amount field, and two, if employee
records are added or subtracted from the invoice, you need to update these
values. So I think the way I would approach that would be to run the same
piece of code on both the After Update event of the invoice amount field on
the main form, and the After Insert event of the subform. Note that this
approach fails if the invoice amount changes or employees are added outside
of the forms (if, for example, you input data into the table directly). You
can avoid this problem by not storing the data, and instead just displaying
the results of a calculation.

Function gmGetEmployeeAmount(lngInvoiceID as Long)

'function retrieves the total invoice amount for invoice lngInvoiceID,
'divides that amount equally among related employee records, and finally
'stores the resulting amount in the 'amount' field in the employee records
'This function needs to be accessable to two forms, so save in modules

Dim strSQL as String

'requires a reference to Microsoft ActiveX Data Objects Library
Dim rs as New ADODB.Recordset

strSQL = "SELECT Table1.Invoice, Table2.Amount " _
& "FROM Table1 LEFT JOIN Table2 ON Table1.ID = Table2.InvoiceID " _
& "WHERE (((Table1.ID)=" & lngInvoiceID & "));"
rs.Open strSQL, CurrentProject.Connection, adOpenKeyset, adLockOptimistic
With rs
Do While Not .EOF
![Amount] = Nz(![Invoice],0)/.RecordCount
.Update
.MoveNext
Loop
End With
rs.Close

End Function

To simply display the results of a calculation, set the subform's amount
field control source to a function stored in the subform's form module:

=lmGetAmount([InvoiceID])

Private Function lmGetAmount(lngInvoiceID as Long) As Currency

Dim strSQL As String

strSQL = "SELECT Table1.Invoice " _
& "FROM Table1 LEFT JOIN Table2 ON Table1.ID = Table2.InvoiceID " _
& "WHERE (((Table1.ID)=" & lngInvoiceID & "));"

rs.Open strSQL, CurrentProject.Connection, adOpenKeyset, adLockOptimistic
If Not rs.EOF Then
lmGetAmount = Nz(rs![Amount],0)/rs.RecordCount
End If
rs.Close
 
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: Condition Group Footer to be visible based on number of records in detail sectio Marshall Barton Microsoft Access Reports 0 30th Apr 2009 10:31 PM
Calculate Amount Based on Year N Microsoft Excel Worksheet Functions 3 10th Nov 2008 05:58 PM
Re: Calculate number of records in a Group Greg Glynn Microsoft Excel Programming 0 19th Dec 2006 02:43 AM
How can I calculate amount of time left based on amount spent? =?Utf-8?B?S0xE?= Microsoft Excel Worksheet Functions 3 23rd May 2006 04:20 PM
how do i calculate total amount of records in a table =?Utf-8?B?cXVlcnkgcHJvYmxlbXMgOig=?= Microsoft Access Queries 2 5th May 2005 12:44 PM


Features
 

Advertising
 

Newsgroups
 


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