Counting the number of days in a month (1 Viewer)

Alvin85

Registered User.
Local time
Today, 15:19
Joined
Jul 7, 2018
Messages
17
I have a textbox by the name txt_date. The user will key in the date. The program is supposed to count the total number of days in the month which the user had key in. May i know what is the coding to do the counting?

Example 1: user keyed in 5/2/2020. The program supposed to display day in 5/2/2020 is 29
Example 2: user keyed in 5/2/2018. The program supposed to display day in 5/2/2018 is 28.
 

moke123

AWF VIP
Local time
Today, 03:19
Joined
Jan 11, 2013
Messages
3,908
You can get the 0 day of the next month which is the last day of the previous month, if that makes sense.

Code:
Function DayCount(dte As Date) As Integer

DayCount = CInt(Day(DateSerial(Year(dte), Month(dte) + 1, 0)))

End Function

this should always give you the last day of the month even if it is a leap year.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 03:19
Joined
Feb 19, 2002
Messages
43,196
count the total number of days in the month
Except for February, each month has a consistent number of days so I don't understand the question. Can you try again.
 

moke123

AWF VIP
Local time
Today, 03:19
Joined
Jan 11, 2013
Messages
3,908
Except for February, each month has a consistent number of days so I don't understand the question. Can you try again.
I think February or Leap Year is his issue. His sample data is confusing as its formatted day/month/year
 

Gasman

Enthusiastic Amateur
Local time
Today, 08:19
Joined
Sep 21, 2011
Messages
14,216
I think February or Leap Year is his issue. His sample data is confusing as its formatted day/month/year

That is how we enter dates in the UK, so not confusing to us.;)
In fact Access catches a lot of UK newbies out having the m/d/y format. :banghead:
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 08:19
Joined
Jul 9, 2003
Messages
16,268
I always thought the way of finding the last day of a month was very clever. All you do is find the first day of the next month and take one off!

Sent from my SM-G925F using Tapatalk
 

moke123

AWF VIP
Local time
Today, 03:19
Joined
Jan 11, 2013
Messages
3,908
That is how we enter dates in the UK, so not confusing to us.;)
In fact Access catches a lot of UK newbies out having the m/d/y format. :banghead:

It always confuses me especially when the posters location is unknown.:confused:
Luckily us Yanks don't have to worry about access confusing our date formats:p
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 08:19
Joined
Jul 9, 2003
Messages
16,268
Luckily us Yanks don't have to worry about access confusing our date formats:p


I could never really understand why Microsoft, who provide international software, didn't use the international date format as standard... I do understand as an American company that they would have a tendency to go with their own countries date format, however underlying their software is a programming language, VBA, and the international date is just perfect for programming in that it sorts ascending/descending properly. So from the programming point of view it makes a lot of sense to use the international format...
 

Gasman

Enthusiastic Amateur
Local time
Today, 08:19
Joined
Sep 21, 2011
Messages
14,216
What is confusing to a newbie is if you place a date parameter in a query which then asks for the date, one can enter 14/07/2018 (dd/mm/yyy format) and Access will convert it. However if you write code you need it in mm/dd/yyyy format.

That is what I feel catches people out, until one learns the difference.
 

NauticalGent

Ignore List Poster Boy
Local time
Today, 03:19
Joined
Apr 27, 2015
Messages
6,317
Luckily for us yanks, MS is an American company so the date format is in our favor. Just about everywhere else in the world uses the dd/mm/yy format. Gets confusing sometimes.
In the military, to avoid this, we were taught to use DD-MMM-YY.

Also, here in Italy, regarding currency they use a period as the thousands separator and a comma as the decimal point. For example $25,000.67 would be represented as €25.000,67.
Not as confusing as the date format but still curious as to why the cultures are so diverse.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 03:19
Joined
Feb 19, 2002
Messages
43,196
However if you write code you need it in mm/dd/yyyy format.
Dates are stored internally as double precision numbers. That is how all relational databases work. The origin date is different depending on the RDBMS and the precision of the number but dates are not strings.

I can't say why MS (and others) decided on the US standard of mm/dd/yyyy as the default STRING date format. It certainly would have made more sense to use yyyy/mm/dd but no one asked me. Perhaps they settled on mm/dd/yyyy since doing so would only annoy half the world whereas using yyyy/mm/dd would annoy everyone. When working with dates in a region where the STRING default is NOT mm/dd/yyyy, you need to format the date to mm/dd/yyyy OR yyyy/mm/dd yourself but ONLY if you are creating an SQL String.

For example, if you are using a querydef and you are referencing a form control, as long as that form control is either bound to a date data type field or has its format property set as a date regardless of your local format then -
Select .... From ... Where SomeDate = Forms!yourform!yourdatecontrol

Will work.

However, if you build the same query in VBA, you are creating a STRING and the Access expression engine cannot help you so you need to specifically format the date.

strSQL = "Select ... From ... Where SomeDate = #" & Format(Me.yourdatecontrol, "mm/dd/yyyy") & "#"

So in the first example, the Access expression evaluator knows it needs to format the date field as the SQL engine expects it and in the second example, you are building the string so you are responsible for the formatting if necessary.

For your own sanity, there is nothing wrong with you always using yyyy/mm/dd for string formats.

strSQL = "Select ... From ... Where SomeDate = #" & Format(Me.yourdatecontrol, "yyyy/mm/dd") & "#"

Even dd/mm/yyyy format works as long as day is > 12 so it can't be confused for a month. This tends to confuse people because sometimes their date works but sometimes it doesn't. This is just Access being too helpful. It would probably be better if Access complained about an invalid date rather than trying to help us.

One thing to ALWAYS remember is to NEVER, EVER format a date EXCEPT for display. When creating queries that work with dates, no formatting is required UNLESS you are using a string date. So comparing one date field to another in a query, even one built with VBA works correctly as long as you do not format the date fields. The only time I ever format date fields in a query is if I want to export the query to Excel and I want to specifically remove the time component. Otherwise, formatting, if necessary, should be done using form control properties or by simply leaving the format alone and relying on your regional settings.

When I make databases that will be used on both sides of the pond, I do format dates but I use dd-mmm-yyyy which is non-ambiguous and people like it better than yyyy/mm/dd
 
Last edited:

Gasman

Enthusiastic Amateur
Local time
Today, 08:19
Joined
Sep 21, 2011
Messages
14,216
In France they have AZERTY keyboards, not QWERTY keyboards.

That takes a bit of getting used to, and again when you get back to your qwerty keyboard.

Luckily for us yanks, MS is an American company so the date format is in our favor. Just about everywhere else in the world uses the dd/mm/yy format. Gets confusing sometimes.
In the military, to avoid this, we were taught to use DD-MMM-YY.

Also, here in Italy, regarding currency they use a period as the thousands separator and a comma as the decimal point. For example $25,000.67 would be represented as €25.000,67.
Not as confusing as the date format but still curious as to why the cultures are so diverse.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 02:19
Joined
Feb 28, 2001
Messages
27,122
Pat Hartman said:
Perhaps they settled on mm/dd/yyyy since doing so would only annoy half the world whereas using yyyy/mm/dd would annoy everyone

Pat, I've NEVER known Microsoft to avoid annoying everyone. They do it with such ease, mostly I guess because of how much practice they have had in doing so. Which is why I am surprised they didn't pick yyyy/mm/dd format.
 

Users who are viewing this thread

Top Bottom