473,414 Members | 1,928 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Dynamic run on sum field in query

Hello i have been trying to figure out this problem for a very long
time now, and thought maybe its worth asking for some help.

I want to figure out if there is a way to create a field in a select
query that acts as a "COUNT" for the fields in the query.
No matter what order the rest of the fields will be it will always
remain in numerical order from 0+. Field one will be 1, field 2 will be
2, field 3 will be 3...etc.

The reason i need to know this is because i have a run on sum for a
balance field for a table called "tbltrans" where i have got it to
work, but only when the ID is in an assending order.

my sql statement:
-------------------------------------------------------------
SELECT t1.id, t1.charge, t1.pay, t1.date, [t1].[charge]-[t1].[pay] AS
due, Sum([t2].[charge]-[t2].[pay]) AS balance
FROM tbltrans AS t1, tbltrans AS t2
WHERE ((([t2].[id])<=[t1].[id]))
GROUP BY t1.id, t1.charge, t1.pay, t1.date, [t1].[charge]-[t1].[pay];
-------------------------------------------------------------

If i decide to change the order of any field than this changes the
order of the ID, and the balance since every field is linked to
eachothers order. I need the balance field which is a run on sum, to be
dynamic. This means it is not linked to the rest of the field. the
balance will change according to the order it is in.
Is there a way i can get a field that is constantly in numerical order
from 0+ and it will never change. It will not be linked to the rest of
the fields. Once i can accomplish that than in i can change my "WHERE"
SQL statement to be like:
(the new count field will be named [count] for this example.)
---------------------
WHERE ((([t2].[id])<=[t1].[count]))
---------------------

quick demo to explain what i mean:
ID ASSCENDING ORDER
id / charge / pay / balance
------------------------------
1 / 50 / 0 / 50
2/ 0 / 5 / 45
3/ 60 / 0 / 105
4/ 0 / 10 / 95

ID DESSCENDING ORDER
id / charge / pay / balance
------------------------------
4 / 0 / 10 / 95
3/ 60 / 0 / 105
2/ 0 / 5 / 45
1/ 50 / 0 / 50

Notice how the balance has followed the rest of the fields.
I want to get the balance to dynamically recalculate correctly.
If i succeed it should look like:

ID DESSCENDING ORDER *CORRECT WAY
id / charge / pay / balance
------------------------------
4 / 0 / 10 / -10
3/ 60 / 0 / 50
2/ 0 / 5 / 45
1/ 50 / 0 / 95

Also note that the laste field should always show the final sum of the
balance.
It is important i figure this out, so any help would be highly
appreciated!
Thanks,
Gil

Nov 13 '05 #1
1 1863
i just found out a way to have a dynamic record counter in the query by
a previous post:
------------------------------------
Global gIncNum As Long

Function ZeroIncNum()
gIncNum = 0
End Function

Function IncNum(Dummy As Variant) As Long
gIncNum = gIncNum + 1
IncNum = gIncNum
End Function

'Then add two fields to your Query:
'Expr1: ZeroIncNum()
'RecNum: IncNum (ID)
------------------------------------------------------------
(ID means the tables ID that you want to count)
RecNum brings you the number of the record, but it doesnt work in a my
join query. It begins skipping numbers.
Any ideas?

Nov 13 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Jack | last post by:
Hi, I am trying to test a sql statement in Access which gives me the error as stated in the heading. The sql statement is built as a part of asp login verification, where the userid and password...
1
by: Nathan Bloomfield | last post by:
Does anyone know if there is any documentation which relates to Access2k + ? or can anyone help adjust the code? I am having trouble converting the DAO references. TITLE :INF: How to...
3
by: Peter Bailey | last post by:
Could someone please tell me how to pass criteria as if it were a parameter. I have a routine now that creates the sql string (well almost). at present the parameter is so I can pass one item ie...
3
by: deejayquai | last post by:
Hi I've created a crosstab query and displayed it as a sub-report in my main report. This is fine until the data changes and the column names become incorrect. I know I have to create a...
2
by: deejayquai | last post by:
Hi I'm trying to produce a report based on a dynamic crosstab. Ultimately i'd like the report to actually become a sub report within a student end of year record of achievement. The dynamic...
9
by: Jimbo | last post by:
Hello, I have a user request to build a form in an Access database where the user can check off specific fields to pull in a query. For example, let's say I have 10 fields in a table. The user...
5
by: devx777 | last post by:
Hello, I am trying to find some information or an example on how to build a dynamic query in DB2 that would allow me to join a table which its name is stored as a field value on another table....
4
by: Philip_collins | last post by:
Hi! I have a dynamic query that has a form attached. I want to add up a field with the sum funtion, the field is netkg. Dim rec As Recordset Dim qdf As QueryDef Dim strsql Set db =...
2
by: Snichols | last post by:
I am attempting a set record ID to make a number field dynamic. 1st - doe the field have to be an "autonumber" or can it be a number field. Also, when I set the query in the stringobject, it gives...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.