473,387 Members | 1,420 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,387 software developers and data experts.

VBA problem in access

9
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.
Nov 5 '11 #1

✓ answered by ADezii

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.
  1. Declare a Public Variable in a Standard Code Module to keep track of the Cumulative Sub-Totals:
    Expand|Select|Wrap|Line Numbers
    1. Public glngSubTotal As Long
  2. Execute Code that RESETS the Public variable, then Opens a Query:
    Expand|Select|Wrap|Line Numbers
    1. 'RESET for each Query Execution
    2. glngSubTotal = 0
    3.  
    4. DoCmd.OpenQuery "qryInventory", acViewNormal, acReadOnly
  3. Generate the Sub-Totals in a Calculated Field in a Query that calls a Function to do the dirty work:
    Expand|Select|Wrap|Line Numbers
    1. SELECT tblInventory.partnumber, tblInventory.amount, fCalcSubTotals([amount]) AS subtotal
    2. FROM tblInventory
    3. ORDER BY tblInventory.amount DESC;
  4. Function Definition:
    Expand|Select|Wrap|Line Numbers
    1. Public Function fCalcSubtotals(lngAmount As Long) As Long
    2. glngSubTotal = glngSubTotal + lngAmount
    3.  
    4. fCalcSubtotals = glngSubTotal
    5. End Function
  5. View the Results:
    Expand|Select|Wrap|Line Numbers
    1. partnumber    amount    subtotal
    2. 0000098491628    455     455
    3. 0000016508467    405     860
    4. 0000004861936    342    1202
    5. 0000018118690    342    1544
    6. 0000500326896    339    1883
    7. 0000500339745    333    2216
    8. 0000002992300    307    2523
    9. 0000042099076    300    2823
    10. 0000002994057    277    3100
    11.  

17 2031
ADezii
8,834 Expert 8TB
Does the [PartNumber] Fieled play any Role in the Logic, or is it just the Consecutive Totalling of the Amounts?
Nov 5 '11 #2
ALESI
9
@ADezii
Well, the partnumber is linked to the amounts.And has to be kept linked. Maybe i can do something with subtotals i guess.
Nov 5 '11 #3
ADezii
8,834 Expert 8TB
@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.
Nov 5 '11 #4
ALESI
9
@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.
Nov 5 '11 #5
ADezii
8,834 Expert 8TB
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.
  1. Declare a Public Variable in a Standard Code Module to keep track of the Cumulative Sub-Totals:
    Expand|Select|Wrap|Line Numbers
    1. Public glngSubTotal As Long
  2. Execute Code that RESETS the Public variable, then Opens a Query:
    Expand|Select|Wrap|Line Numbers
    1. 'RESET for each Query Execution
    2. glngSubTotal = 0
    3.  
    4. DoCmd.OpenQuery "qryInventory", acViewNormal, acReadOnly
  3. Generate the Sub-Totals in a Calculated Field in a Query that calls a Function to do the dirty work:
    Expand|Select|Wrap|Line Numbers
    1. SELECT tblInventory.partnumber, tblInventory.amount, fCalcSubTotals([amount]) AS subtotal
    2. FROM tblInventory
    3. ORDER BY tblInventory.amount DESC;
  4. Function Definition:
    Expand|Select|Wrap|Line Numbers
    1. Public Function fCalcSubtotals(lngAmount As Long) As Long
    2. glngSubTotal = glngSubTotal + lngAmount
    3.  
    4. fCalcSubtotals = glngSubTotal
    5. End Function
  5. View the Results:
    Expand|Select|Wrap|Line Numbers
    1. partnumber    amount    subtotal
    2. 0000098491628    455     455
    3. 0000016508467    405     860
    4. 0000004861936    342    1202
    5. 0000018118690    342    1544
    6. 0000500326896    339    1883
    7. 0000500339745    333    2216
    8. 0000002992300    307    2523
    9. 0000042099076    300    2823
    10. 0000002994057    277    3100
    11.  
Nov 5 '11 #6
ALESI
9
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.
Nov 5 '11 #7
ADezii
8,834 Expert 8TB
I see no Declaration in a Standard Code Module for the Public Variable:
Expand|Select|Wrap|Line Numbers
  1. Public glngSubTotal As Long
Nov 5 '11 #8
ALESI
9
Sorry,

But what should i declare between the brackets ()

Public glngSubTotal() As Long

Should it be public or public function?
Nov 6 '11 #9
ADezii
8,834 Expert 8TB
The Declaration is exactly as what was depicted in Post #8.
Nov 6 '11 #10
ALESI
9
@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.
Nov 7 '11 #11
ADezii
8,834 Expert 8TB
When I get the chance, I'll send you a Demo showing how it should work.
Nov 7 '11 #12
ALESI
9
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.
Nov 7 '11 #13
Rabbit
12,516 Expert Mod 8TB
If this is for reporting purposes, why not use a running sum?
Nov 7 '11 #14
ALESI
9
I am really a novice in Access... this is a query....

how do i use a runnning sum?
Nov 7 '11 #15
ADezii
8,834 Expert 8TB
In the Text Box of a Report.
Nov 7 '11 #16
ALESI
9
@ 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...
Nov 8 '11 #17
ADezii
8,834 Expert 8TB
Looks like a Typo:
Expand|Select|Wrap|Line Numbers
  1. glngSubTotal = 0
Nov 8 '11 #18

Sign in to post your reply or Sign up for a free account.

Similar topics

63
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...
13
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....
1
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...
13
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...
20
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...
64
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. ...
1
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. ......
17
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...
37
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...
2
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...
0
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
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,...

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.