PC Review


Reply
Thread Tools Rate Thread

Sum a column every time a record is created

 
 
=?Utf-8?B?ZWNuYWxjaA==?=
Guest
Posts: n/a
 
      19th Jul 2006
I want to sum the Quantity field each time I create a record. I am defaulting
a 1 in the Quantity field and I have created a sum in the footer section. The
sum works whenever I manually enter a value but it does not work if I use the
default value of 1. In other words, if I simply press enter and accept the
default 1 for quantity, the sum does not update when I commit the entire
record. If I manually enter a value of 1 for Quantity it does update. How can
I force the sum to update each time I create a record even if I do not
manually enter a value?
 
Reply With Quote
 
 
 
 
Arvin Meyer [MVP]
Guest
Posts: n/a
 
      19th Jul 2006
Try forcing the subform to recalculate in the AfterUpdate event.

Sub Form_AfterUpdate()
Me.Recalc
End Sub
--
Arvin Meyer, MCP, MVP
Microsoft Access
Free Access downloads
http://www.datastrat.com
http://www.mvps.org/access

"ecnalch" <(E-Mail Removed)> wrote in message
news:368BB52D-7EA2-4067-9C5B-(E-Mail Removed)...
>I want to sum the Quantity field each time I create a record. I am
>defaulting
> a 1 in the Quantity field and I have created a sum in the footer section.
> The
> sum works whenever I manually enter a value but it does not work if I use
> the
> default value of 1. In other words, if I simply press enter and accept the
> default 1 for quantity, the sum does not update when I commit the entire
> record. If I manually enter a value of 1 for Quantity it does update. How
> can
> I force the sum to update each time I create a record even if I do not
> manually enter a value?



 
Reply With Quote
 
 
 
 
onedaywhen
Guest
Posts: n/a
 
      19th Jul 2006

ecnalch wrote:
> I want to sum the Quantity field each time I create a record. I am defaulting
> a 1 in the Quantity field


The DEFAULT must be a scalar (i.e. a single value) and either a literal
value (e.g. not a calculation) or a niladic function (e.g. DATE()).

Consider this table

CREATE TABLE Test (
amount INTEGER DEFAULT 0 NOT NULL,
CHECK (amount >= 0),
total_amount_when_added INTEGER DEFAULT 0 NOT NULL,
CHECK (total_amount_when_added >= 0)
);

To determine the value for the total_amount_when_added column on
insert, you could create a SQL procedure (parameterized query):

CREATE PROCEDURE TestProc (
new_amount INTEGER = 1
) AS
INSERT INTO Test (amount, total_amount_when_added)
SELECT new_amount AS amount,
IIF(SUM(amount) IS NULL, 0, SUM(amount))
+ IIF(new_amount IS NULL, 1, new_amount)
AS total_amount_when_added
FROM Test;

You would then remove INSERT permissions from the base table and grant
them to the procedure. This way, you would ensure that all inserts to
the table go via your procedure i.e. the value in the
total_amount_when_added column is under the control of the database
designer, not the front end code.

Jamie.

--

 
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
How to Sum<=40, sum, if >40 sum in next column justnotgettingit Microsoft Excel Worksheet Functions 5 6th Dec 2007 05:40 PM
Every record paired with every other record in the same report? Lucky Man Cree Microsoft Access 3 14th Mar 2006 09:39 AM
CHART: how to show (on every column top) the value of every column ? Martin Microsoft Access 0 23rd Nov 2005 08:38 AM
Form code: how do I run code every time a record is "loaded" by going to next record, last record, etc. Dave R. Microsoft Access Macros 1 24th Jun 2005 07:37 PM
Yearly Recurrence is every day every week every month =?Utf-8?B?RnJhc2Vy?= Microsoft Outlook Calendar 0 30th May 2004 10:51 PM


Features
 

Advertising
 

Newsgroups
 


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