Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access
|
Access Discussion of Microsoft Access database design and programming. See also the forums for Access ASP and Access VBA.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 25th, 2007, 02:45 AM
Authorized User
 
Join Date: Mar 2006
Posts: 83
Thanks: 1
Thanked 0 Times in 0 Posts
Default Counting 'groups' of records in a report

Hiya folks!

I have a report that shows something like the following (note that some articles are repeated):

PRODUCT DATE QUANTITY SOLD
Coca Cola 01/07/07 10
Ginger Ale 01/07/07 20
Seven Up 01/07/07 15
Coca Cola 02/07/07 6
Seven Up 03/07/07 4
Fanta 04/07/07 13

I have another report that is exactly like this one, but using a PRODUCT_HEADER section in the report, I have managed to summarize the above in the following way:

PRODUCT QUANTITY SOLD
Coca Cola 16
Ginger Ale 20
Seven Up 19
Fanta 13

The problem I have is when I try to make a count of the different types of articles sold in the second one, it keeps giving me the total count of articles counted in the first one.

I should have a count of 4 in the summarized version, but it keeps giving me a result of 6. Is there any way to count the number of Product Groups (in this case 4) or to count unique values in the detail section of the report to give me a result of 4 instead of 6?

Thank you!
 
Old July 25th, 2007, 05:01 AM
Authorized User
 
Join Date: Mar 2006
Posts: 83
Thanks: 1
Thanked 0 Times in 0 Posts
Default

The resulting value of 4 will then be used in a formula located in a separate field.

 
Old July 25th, 2007, 10:55 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

Okay, this works, but there may be more elegant way of doing it:

Private Sub PageHeaderSection_Format(Cancel As Integer, FormatCount As Integer)
Dim sSQL As String
Dim rs As ADODB.Recordset
Public sValue As String

sSQL = "SELECT Count(Product) As PCount FROM qryYourReportQuery"
Set rs = New ADODB.Recordset
rs.Open sSQL, CurrentProject.Connection, adOpenStatic, adLockReadOnly
    sValue = rs("PCount")

Me.txtYourTextBox = sValue
rs.Close

End Sub


Now, I am not sure if the actual value in the text box will be available for your next formula, but the Public variable sValue WILL be. So try referring to the text box with something like:

=AVG([QtySold]/[txtYourTextBox])

And if that doesn't work, then do the calculation in code.

Did that help?

mmcdonal
 
Old July 25th, 2007, 10:58 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

Sorry,

sSQL = "SELECT DISTINCT Count(Product) As PCount FROM qryYourReportQuery"

Alternatively, make a query off the query for your report that selects distinct count from your report query, and then just do this:

sSQL = "SELECT PCount FROM qrySelectDistinctQuery"




mmcdonal
 
Old July 30th, 2007, 10:41 AM
Authorized User
 
Join Date: Mar 2006
Posts: 83
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Hello again!

Thanks for your suggestion!

I am getting a compile error on this line of the code now:

rs As ADODB.Recordset

I guess I need to add this library, right? Can you tell me how to add libraries?

Thank you!






Similar Threads
Thread Thread Starter Forum Replies Last Post
Counting Child Records BrianWren VB Databases Basics 1 June 2nd, 2008 04:32 PM
AbsolutePosition counting filtered records Brad_S VB Databases Basics 1 August 17th, 2006 04:24 AM
Counting records Jonas Access 4 August 4th, 2006 09:07 AM
A Cursor counting records chubnut SQL Server 2000 12 July 8th, 2004 01:58 PM
Grouping and Counting records Con Access VBA 1 September 17th, 2003 11:31 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.