473,385 Members | 2,210 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,385 software developers and data experts.

How do i calculate the Product of several fields???

49
Hi
I have a query off a database that works fins and displays a list of 4 numbers in the query in seperate fields, however i only want 1 field to be displayed and that wants to be the PRODUCT of the other 4 i.e. field1 * field2 * field3 * field4.....at some point this could need to be more numbers. Excel has an easy function "=PRODUCT" is there away of doing this in Acess?????
Mar 27 '07 #1
9 2755
ADezii
8,834 Expert 8TB
Hi
I have a query off a database that works fins and displays a list of 4 numbers in the query in seperate fields, however i only want 1 field to be displayed and that wants to be the PRODUCT of the other 4 i.e. field1 * field2 * field3 * field4.....at some point this could need to be more numbers. Excel has an easy function "=PRODUCT" is there away of doing this in Acess?????
__1. Create a New Query.
__2. Place the [Field1], [Field2], [Field3], and [Field4] Fields into the Query Grid.
__3. Create a Calculated Field called Total (see code snippet).
__4. Deselect the Show Box for Fields 1, 2, 3, and 4.
__5. Run the Query ==> Only the Total Field will be visible and will be the Product of the 4 other Fields.
Expand|Select|Wrap|Line Numbers
  1. Total: ([Field1]*[Field2]*[Field3]*[Field4])
Mar 27 '07 #2
Milkstr
49
Thanks for the speedy reply, but not quite what i was after, the fields i want the product of are on seperate records, so in the query there could just be for example 2 records filtered off, or there could be 5 filtered off and i want the product of the field regardless of how many potential records there is, hope that makes sense. Any Ides???
Mar 27 '07 #3
ADezii
8,834 Expert 8TB
Thanks for the speedy reply, but not quite what i was after, the fields i want the product of are on seperate records, so in the query there could just be for example 2 records filtered off, or there could be 5 filtered off and i want the product of the field regardless of how many potential records there is, hope that makes sense. Any Ides???
More complicated scenario. Post Query results illustrating exactly what you are looking for and I'll check back in later.
Mar 27 '07 #4
Milkstr
49
Multiple | FixtureID | GameDate | Odds | ResultHomeAwayDraw | Win
5 | 1 24/03/2007 140 Home 0
10 1 24/03/2007 140 Home 0
10 7 24/03/2007 130 Home 0
70 1 24/03/2007 140 Home 1
70 5 24/03/2007 200 Home 0
70 6 24/03/2007 120 Home 0

This is the list of the current query, where the "multiple" colum = 70 for example i want the 140 * 200 * 120 displayed.....i.e. where the "Multiple" Colum = 10 then i want 140 * 130 displayed.........etc so then i can run another query with just the product of the odds colum, if all that makes sense, i thought it would be easy!!!!! but its not!!!
Mar 27 '07 #5
Milkstr
49
Multiple | FixtureID | GameDate | Odds | ResultHomeAwayDraw |
5 | 1 | 24/03/2007 | 140 | Home
10 | 1 | 24/03/2007 | 140 | Home
10 | 7 | 24/03/2007 | 130 | Home
70 | 5 | 24/03/2007 | 140 | Home
70 | 5 | 24/03/2007 | 200 | Home
70 | 6 | 24/03/2007 | 120 | Home
Mar 27 '07 #6
ADezii
8,834 Expert 8TB
Multiple | FixtureID | GameDate | Odds | ResultHomeAwayDraw |
5 | 1 | 24/03/2007 | 140 | Home
10 | 1 | 24/03/2007 | 140 | Home
10 | 7 | 24/03/2007 | 130 | Home
70 | 5 | 24/03/2007 | 140 | Home
70 | 5 | 24/03/2007 | 200 | Home
70 | 6 | 24/03/2007 | 120 | Home
Let me think it over for a little and I'll get back to you.
Mar 27 '07 #7
ADezii
8,834 Expert 8TB
Multiple | FixtureID | GameDate | Odds | ResultHomeAwayDraw |
5 | 1 | 24/03/2007 | 140 | Home
10 | 1 | 24/03/2007 | 140 | Home
10 | 7 | 24/03/2007 | 130 | Home
70 | 5 | 24/03/2007 | 140 | Home
70 | 5 | 24/03/2007 | 200 | Home
70 | 6 | 24/03/2007 | 120 | Home
__1. I don't know the name of your Table but for the sake of simplicity, I'll call it tblGames.
__2. Create a Query call qryProduct whic will consist of 1 Field for now, namely [Multiple].
__3. Set the Sort Order of the [Multiple] Field in qryProduct to Ascending.
__4. Set the Unique Values Property to yes for qryProduct. This Query will now generate a list of 'Unique' [Multiple] values.
__5. Copy and Paste the following Function to a Standard Code Module.
Expand|Select|Wrap|Line Numbers
  1. Public Function fCalculateProduct(intMultiple As Long) As String
  2. Dim intNoOfMultiples As Integer, intCounter As Integer, lngProduct As Long
  3.  
  4. 'Calculate the # of Records for the [Multiple]
  5. intNoOfMultiples = DCount("*", "tblGames", "[Multiple]=" & intMultiple)
  6.  
  7. lngProduct = 1  'Initialize
  8.  
  9. If intNoOfMultiples = 1 Then
  10.   'Only 1 Record - just return [Odds]
  11.   fCalculateProduct = Format$(DLookup("[Odds]", "tblGames", "[Multiple]=" & intMultiple), "Standard")
  12. Else
  13.   'Now we got work to do
  14.   Dim MyDB As DAO.Database, MyRS As DAO.Recordset
  15.   Set MyDB = CurrentDb()
  16.   Set MyRS = MyDB.OpenRecordset("SELECT * From tblGames WHERE [Multiple] =" & intMultiple, dbOpenSnapshot)
  17.   MyRS.MoveFirst
  18.     Do While Not MyRS.EOF
  19.       lngProduct = lngProduct * MyRS![Odds]
  20.       MyRS.MoveNext
  21.     Loop
  22.       fCalculateProduct = Format$(lngProduct, "Standard")
  23.       MyRS.Close
  24. End If
  25. End Function
__6. Create a Calculate Field in qryProduct called Product. This Calculated Field will call the fCalculateFunction() and pass to it the [Multiple] Field. It will look like this in the QBE Grid.
Expand|Select|Wrap|Line Numbers
  1. Multiple                      Product: fCalculateProduct([Multiple])
  2. tblGames
  3. Ascending
__7. Run the Query.

OUTPUT:
Expand|Select|Wrap|Line Numbers
  1. Multiple    Product
  2. 5    140.00
  3. 10    18,200.00
  4. 70    3,360,000.00
  5. 89    1,521,275,736.00
NOTE: 89 was my own little Test Value. If you have any other questions, please feel free to ask.
Mar 28 '07 #8
Milkstr
49
Thanks for your help there!!! I just can't quite get it to work, i get an error from the standard Module, on the line "myRs.MoveFirst" it errors there saying "no current record" any ideas what it could be? i've used all the same names as you in your example??
Mar 28 '07 #9
ADezii
8,834 Expert 8TB
Thanks for your help there!!! I just can't quite get it to work, i get an error from the standard Module, on the line "myRs.MoveFirst" it errors there saying "no current record" any ideas what it could be? i've used all the same names as you in your example??
That Error indicates that the SQL Statement did not return any Records but if you made it to this line, there logically must be because intNoOfMultiples cannot = 0 since it is being passed to the Function(), and the case has already been accounted for where intNoOfMultiples = 1. Are [Multiple] and [Odds] Required Fields? Do Records actually exist in tblGames and are the Field Names identical? Also, check for a Typo in the OpenRecordset() line. Let me know how you made out.
Mar 28 '07 #10

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

Similar topics

3
by: Zsolt Koppany | last post by:
Hi, I use dynamic menus and would like to know how I can figure out the height of a text (for exalple "Product"). I need it to calculate text positions. Zsolt
4
by: Sven Seljom | last post by:
I want to take the value from one form-field, deduct the VAT and return the value in another form-field. I have made a Public Function for this and attached it to the text-box. Can anybody help me...
7
by: Eric Slan | last post by:
Hello All: I'm having a problem that's been baffling me for a few days and I seek counsel here. I have an Access 2000 DB from which I want to run several reports. These reports are...
3
by: carla | last post by:
I am using Access 2000 and have a table, tblCurrent. That table contains several fields - three of those fields are , and . What I want to accomplish in my data entry form, is first entering the...
1
by: enrique.rojas | last post by:
Hello I have been trying to make an SQL statement that will get the the last product audited in a database. The problem is that this database holds data for several emplyees that audit...
3
ChaseCox
by: ChaseCox | last post by:
Hi all, I have a problem that I have been looking at for a couple days now and I can not quite get it to work. I would like to calculate the cumulative percent failure of a certain product in...
1
by: ollielaroo | last post by:
Hi guys, Firstly I did do a search for this one first but I couldn't find anything related in this forum. I am using Dreamweaver MX and trying to build admin pages for an ASP site. My problem is...
6
by: rrstudio2 | last post by:
I am using the following vba code to calculate the median of a table in MS Access: Public Function MedianOfRst(RstName As String, fldName As String) As Double 'This function will calculate the...
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.