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

How do I count the number or entries in a certain month?

Seth Schrock
2,965 Expert 2GB
I'm designing a billing database that I bill every six months. One of my customers have a special charge for all items past the third one in each month. Currently, I'm using Excel to do this using an array formula, but I'm not sure how to do this in Access. If it helps at all, here is the formula that I'm using in Excel:

{=IF(SUM((MONTH($A$8:$A$20)=MONTH(A8))*(YEAR($A$8: $A$20)=YEAR(A8))*1)>2,$A$5,$B$5)}

A2:A20 is where the dates are that I'm analyzing. A5 is the price for the third item and B5 is the normal price. I'll try to get a zip file to upload of my Excel file, but currently my compression software isn't working so I'll have to get help from IT before I can do that.
Aug 18 '11 #1

✓ answered by Rabbit

Leave out the month criteria in the WHERE clause but add the month to the SELECT and GROUP BY clauses.

13 3272
Rabbit
12,516 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. SELECT Count(*)
  2. FROM tableName
  3. WHERE Month(someDate) = 8
  4.       AND Year(someDate) = 2011
  5. HAVING Count(*) > 3;
Aug 18 '11 #2
Seth Schrock
2,965 Expert 2GB
Is there a way to pull the month and year of the current record? Otherwise, I'll have to change the year every year and the month for each of the 6 months of the billing period.
Aug 18 '11 #3
beacon
579 512MB
Hi Seth,

Assuming that you're using forms to capture the data, make the calculation, and then populate the bound table, this shouldn't be too difficult to do with VBA and a query.

You can setup your form to capture a date for each bill that you input. You could add a command button that either calculates the bill prior to submitting it to the table, or you could create a command button that calculates and saves it at the same time...it's personal preference. I'm going to assume you want a calculate button and will handle the saving separately.

First, I would use the Month(), Year(), and DateSerial() functions to capture the month and year from a text box on the form and assign the values to variables to create your date range from the first of the month to the last day of the month(unless it's also assumed that you will always be dealing with the current year, in which case you could use Year(Now) below instead of referencing your billing date). Then I'd use the variables to create a SQL query that I'd use to create a QueryDef in DAO to find the number of records returned. If the number of records is greater than 2, then I'd apply the special conditions to the bill. Once the bill is calculated, you can save it however you see fit.

Here's a bit of code as an example:
Expand|Select|Wrap|Line Numbers
  1. Private Sub BillCalculation()
  2.  
  3.      Dim db As DAO.Database
  4.      Dim rstRecords As DAO.Recordset
  5.      Dim startDate As Date, endDate As Date
  6.      Dim strCompany As String
  7.      Dim myBill As Currency
  8.      Dim specialCharge As Currency
  9.      Dim strSQL As String
  10.      Dim monthlyRecords As Long
  11.  
  12.      'Identify the special charge
  13.      specialCharge = 15.75
  14.  
  15.      'Capture the bill from the form
  16.      myBill = Me.txtFormBill
  17.  
  18.      'Capture the month and year from the form to determine date range
  19.      startDate = CDate(DateSerial(Year(Me.BillDate), Month(Me.BillDate), 1)
  20.      endDate = CDate(DateSerial(Year(Me.BillDate), Month(Me.BillDate) + 1, 0))
  21.  
  22.      'I'm assuming you know how to create a SQL SELECT QUERY
  23.      'The fields, tables, and
  24.      strSQL = "SELECT * FROM myTable WHERE BillDate BETWEEN #" & startDate & "# AND #" & endDate
  25.  
  26.      Set db = CurrentDb
  27.      Set rstRecords = db.OpenRecordset(strSQL)
  28.  
  29.      If Not rstRecords.BOF And Not rstRecords.EOF Then
  30.           rstRecords.MoveLast
  31.           monthlyRecords = rstRecords.RecordCount
  32.      End If
  33.  
  34.      If monthlyRecords > 2 Then
  35.           myBill = myBill * specialCharge
  36.      End If
  37.  
  38.      'Reassign the calculation back to the form field
  39.      Me.txtFormBill = myBill
  40.  
  41.      Set rstRecords = Nothing
  42.      Set db = Nothing
  43.  
  44. End Sub
  45.  
This analyzes the bills individually as they are entered into the form. It may not be the best option, but it's something to work with and is somewhat close to what you were using in Excel.

Hope this helps,
beacon
Aug 18 '11 #4
Seth Schrock
2,965 Expert 2GB
Sorry it has taken me so long to respond.

Beacon, I would rather do it through a query than to have it put into a field in a table (normalization theory).

Rabbit, that is close to what I want. My problem is that I'll be evaluating 1/1/20xx to 6/30/20xx and I need it to find each month in that range that has three or more records. From what I'm guessing that your SQL code would produce, it would only search for one month out of the 6 month period. Is there a way to test each of the 6 months individually in the same query?
Aug 25 '11 #5
Rabbit
12,516 Expert Mod 8TB
Leave out the month criteria in the WHERE clause but add the month to the SELECT and GROUP BY clauses.
Aug 25 '11 #6
Seth Schrock
2,965 Expert 2GB
Here is what I've got:
Expand|Select|Wrap|Line Numbers
  1. SELECT Count(*), Month(EffectiveDate)
  2. GROUP BY Month(EffectiveDate)
  3. FROM tblACHFiles
  4. WHERE Year(EffectiveDate) = 2011
  5. HAVING Count(*) >3;
It doesn't work. When I did your original query, I got zero results even though there are 3 records that should meet the criteria in the table.
Aug 26 '11 #7
Rabbit
12,516 Expert Mod 8TB
That shouldn't even run. The GROUP BY has to be after the WHERE clause. But because you said it ran means that you either posted SQL that you didn't actually use or there was an error and you misspoke when you said it returned no rows.
Aug 26 '11 #8
Seth Schrock
2,965 Expert 2GB
It didn't run. The query that did run is the one from your first post. After moving the GROUP BY statement to be right after the WHERE statement and before the HAVING statement, it does run now, but again, no results. I'm not very familiar with SQL code so there might be something I'm missing, but here is what I have:
Expand|Select|Wrap|Line Numbers
  1. SELECT Count(*), Month(EffectiveDate)
  2. FROM tblACHFiles
  3. WHERE Year([EffectiveDate])=2011
  4. GROUP BY Month(EffectiveDate)
  5. HAVING Count(*)>3;
  6.  
The field that I'm checking in the query is the EffectiveDate field. I need it to find the third record in each month (that might not exist, but my test records do have a third record). Is there any other information that you need about what I've got?
Aug 26 '11 #9
Seth Schrock
2,965 Expert 2GB
Well, I figured out part of the problem. I needed the count to be >= 3, not just >3. I should have caught that before. Now I'm just working on making it find each of the records that is the third of the month. I'll keep working on it. I'm going to go back over your solution now I have the Count function corrected.
Aug 26 '11 #10
Seth Schrock
2,965 Expert 2GB
Okay, I've got most of it figured out. Here is what I've got:
Expand|Select|Wrap|Line Numbers
  1. SELECT Count(*) AS Count, Month(EffectiveDate) AS Month
  2. FROM tblACHFiles
  3. WHERE Year(EffectiveDate)=2011
  4. GROUP BY Month(EffectiveDate)
  5. HAVING Count(*) >=3
Now, how do I make the year not be equal to 2011, but make it able to change without manually change it every year? The query runs if I take out the WHERE line. Is there any potential problems that I could run up against if I leave that out?
Aug 26 '11 #11
Rabbit
12,516 Expert Mod 8TB
Well, if you don't have a year, then it's going to count January of every year together and so on. You can have it prompt for a year that the user types in by using [Enter Year] or something like that. Or if you want it only for the current year, you can use Year(Date()).
Aug 26 '11 #12
Seth Schrock
2,965 Expert 2GB
Okay. I think that it will be okay since I'm going to select a date range of only six months, but I'll keep that in mind as a potential problem that I need to plan for.

Thanks for all your help!
Aug 26 '11 #13
Rabbit
12,516 Expert Mod 8TB
No problem, good luck.
Aug 26 '11 #14

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

Similar topics

1
by: vic | last post by:
Hello, Dean Try this: select distinct c1, c2 into #tmp_1 from t1 select count(*) as cnt from #tmp_1 drop table #tmp_1 With best regards.
2
by: Andy | last post by:
Could anyone tell me how to round off a number to certain significant figures using C++? For example, how to round off a number 12567 to 13000 (2 significant figures)?
0
by: DataFreakFromUtah | last post by:
Hello! No question here, just a procedure for the archive. Search critera: count records imported count data imported count number of rows imported count number of records imported record import...
1
by: tranky | last post by:
hi, only one information,please... how to count number of anonymous online users? I use ASP.NET 2.0 thank u tranky
5
by: isabelle | last post by:
hi, every body.. I have two program I couldn’t solve them So, can any body help me. please!! 1-Write a program that accepts a character and count number of occurrences in a file. The file...
11
by: Mack | last post by:
Hi all, I want to write a program to count number of bits set in a number. The condition is we should not loop through each bit to find whether its set or not. Thanks in advance, -Mukesh
2
by: mfaisalwarraich | last post by:
Hi Everybody, I am using the following code to get the recordset of an external database. Dim dbPatients As Database Dim rsCountPatients As Recordset ' to count number of...
5
by: jambonjamasb | last post by:
I am wanting to create a report that summarises the number of items within a date range. For example I have a FIELD called System_Change. This is a drop down COMBOBOX that uses words like unix,...
1
by: jlt206 | last post by:
This code <?php include("counter.php")?> on the webpage produces the count number. (function code below) I want to place the current number into a variable $MemberNo or into a FormField to be sent...
6
by: cathrine babe | last post by:
How To Count Number Of Words In A Sentence
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:
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
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
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.