I am starting to use access and i have reached the maximum of my knowledge of access 2003.
The problem is that i need a custom VBA to build a custom function for my access application.
I need to do a calculation, i have a query which contains partnumbers and amounts on stock.
Now i have these amounts sort by highest value to lowest value.
What i need in the next field is:
in the first row of the the table; the sum of the first highest value
in the second row of the table ; the sum of the first 2 highest values
in the third row of the table ; the sum of the first 3 highest values
and so on and so on
Now i was thinking, maybe i can put an id next to the amounts and than do something like sumif in excell.
But there must be an easier way.
I hope you can help me.
There definitely has to be a more efficient, SQL-based approach, but for now it is alluding me. The following Logic will work until the SQL Gang comes through with a better solution. - Declare a Public Variable in a Standard Code Module to keep track of the Cumulative Sub-Totals:
- Public glngSubTotal As Long
- Execute Code that RESETS the Public variable, then Opens a Query:
- 'RESET for each Query Execution
-
glngSubTotal = 0
-
-
DoCmd.OpenQuery "qryInventory", acViewNormal, acReadOnly
- Generate the Sub-Totals in a Calculated Field in a Query that calls a Function to do the dirty work:
- SELECT tblInventory.partnumber, tblInventory.amount, fCalcSubTotals([amount]) AS subtotal
-
FROM tblInventory
-
ORDER BY tblInventory.amount DESC;
- Function Definition:
- Public Function fCalcSubtotals(lngAmount As Long) As Long
-
glngSubTotal = glngSubTotal + lngAmount
-
-
fCalcSubtotals = glngSubTotal
-
End Function
- View the Results:
-
partnumber amount subtotal
-
0000098491628 455 455
-
0000016508467 405 860
-
0000004861936 342 1202
-
0000018118690 342 1544
-
0000500326896 339 1883
-
0000500339745 333 2216
-
0000002992300 307 2523
-
0000042099076 300 2823
-
0000002994057 277 3100
-
17 2031
Does the [PartNumber] Fieled play any Role in the Logic, or is it just the Consecutive Totalling of the Amounts?
@ADezii
Well, the partnumber is linked to the amounts.And has to be kept linked. Maybe i can do something with subtotals i guess.
@ALESI:
You are going to have to Post some Data as well as the Desired results, so that we can fully comprehend what you are requesting.
@ADezii
partnumber amount subtotal
0000098491628---455---455
0000016508467---405---860
0000018118690---342---1202
0000004861936---342---1544
0000500326896---339---1883
0000500339745---333---2216
0000002992300---307---2523
0000042099076---300---2823
0000002994057---277---3100
These first 2 columns i have, now the calculation i need is for the subtotals.
The amounts are sorted descending.
There definitely has to be a more efficient, SQL-based approach, but for now it is alluding me. The following Logic will work until the SQL Gang comes through with a better solution. - Declare a Public Variable in a Standard Code Module to keep track of the Cumulative Sub-Totals:
- Public glngSubTotal As Long
- Execute Code that RESETS the Public variable, then Opens a Query:
- 'RESET for each Query Execution
-
glngSubTotal = 0
-
-
DoCmd.OpenQuery "qryInventory", acViewNormal, acReadOnly
- Generate the Sub-Totals in a Calculated Field in a Query that calls a Function to do the dirty work:
- SELECT tblInventory.partnumber, tblInventory.amount, fCalcSubTotals([amount]) AS subtotal
-
FROM tblInventory
-
ORDER BY tblInventory.amount DESC;
- Function Definition:
- Public Function fCalcSubtotals(lngAmount As Long) As Long
-
glngSubTotal = glngSubTotal + lngAmount
-
-
fCalcSubtotals = glngSubTotal
-
End Function
- View the Results:
-
partnumber amount subtotal
-
0000098491628 455 455
-
0000016508467 405 860
-
0000004861936 342 1202
-
0000018118690 342 1544
-
0000500326896 339 1883
-
0000500339745 333 2216
-
0000002992300 307 2523
-
0000042099076 300 2823
-
0000002994057 277 3100
-
Thank you ADezii,
But i can't get it to work...:(
This is what i have put in 1 module in VBA:
Public Function glngSubTotal() As Long
glngSubTotal = 0
DoCmd.OpenQuery "abc bepaling calculatie", acViewNormal, acReadOnly
End Function
Public Function fCalcSubtotals(lngAmount As Long) As Long
glngSubTotal = glngSubTotal + lngAmount
fCalcSubtotals = glngSubTotal
End Function
And this is in my SQL:
SELECT [abc bepaling aantallen].abondn, [abc bepaling aantallen].abantl, fCalcSubTotals([abc bepaling aantallen].[abantl]) AS subtotal
FROM [abc bepaling aantallen]
ORDER BY [abc bepaling aantallen].abantl DESC;
But than it gives a compile error in VBA:
Public Function fCalcSubtotals(lngAmount As Long) As Long
glngSubTotal = glngSubTotal + lngAmount
glngSubTotal is marked blue and it says:
function sign on the left side of the commitment must give a variant or object as result.
Am i doing something wrong?
Thanks for your help so far.I am getting in the right direction.
I see no Declaration in a Standard Code Module for the Public Variable: - Public glngSubTotal As Long
Sorry,
But what should i declare between the brackets ()
Public glngSubTotal() As Long
Should it be public or public function?
The Declaration is exactly as what was depicted in Post #8.
@ADezii
I choose this as the best help from this site, but untill now it does not work yet. My query gives as result exactly the same as what is in the column amount.
When I get the chance, I'll send you a Demo showing how it should work.
Ok, great!
Well i have got it to work now, sort of. the glng doesnt seem to work.
I have it working now with lng in the function.
The only thing now is that it doesnt reset to zero.
If this is for reporting purposes, why not use a running sum?
I am really a novice in Access... this is a query....
how do i use a runnning sum?
In the Text Box of a Report.
@ ADezii
No i really need this in a Query... I have got it to work now.
The only thing it does not is the reset:
Public Function ResetSubtotals()
'RESET for each Query Execution
lngSubTotal = 0
DoCmd.OpenQuery "abcaantcalc", acViewNormal, acReadOnly
End Function
What is wrong in this code...
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Jerome |
last post by:
Hi, I'm a bit confused ... when would I rather write an database
application using MS Access and Visual Basic and when (and why) would I
rather write it using Visual Studio .Net?
Is it as easy...
|
by: bill |
last post by:
I am trying to convince a client that dotNet is preferable to an Access
project (ADP/ADE).
This client currently has a large, pure Access MDB solution with 30+ users,
which needs to be upgraded....
|
by: Dave |
last post by:
Hello NG,
Regarding access-declarations and member using-declarations as used to
change the access level of an inherited base member...
Two things need to be considered when determining an...
|
by: Simon Bailey |
last post by:
I am a newcomer to databases and am not sure which DBMS to use. I have
a very simplified knowledge of databases overall. I would very much
appreciate a (simplifed) message explaining the advantages...
|
by: Olav.NET |
last post by:
I am a .NET/C++ developer who is supposed to do some work with Access.
I do not know much about it except for the DB part.
Questions:
*1*
I am looking for INTENSIVE books to get quickly up to...
|
by: John |
last post by:
Hi
What future does access have after the release of vs 2005/sql 2005? MS
doesn't seem to have done anything major with access lately and presumably
hoping that everyone migrates to vs/sql.
...
|
by: com |
last post by:
Extreme Web Reports 2005 - Soft30.com
The wizard scans the specified MS Access database and records
information such
as report names, parameters and subqueries. ......
|
by: Mell via AccessMonster.com |
last post by:
Is there a way to find out where an application was created from?
i.e. - work or home
i.e. - if application sits on a (work) server/network, the IT people know the
application is sitting...
|
by: jasmith |
last post by:
How will Access fair in a year? Two years? .... The new version of
Access seems to service non programmers as a wizard interface to
quickly create databases via a fancy wizard. Furthermore, why...
|
by: eitsubashkumars |
last post by:
I have configured remote access VPN with local pool in ASA firewall however im accessing all the resources(my private network such as servers ) through Asa firewall after getting connected the VPN...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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...
|
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,...
|
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,...
| |