473,399 Members | 3,919 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,399 software developers and data experts.

Counting specific Week days

I have tried using the following code to count the specific number of
each weekday but get a compile error "User defined type not defined"
which I think relates to the first line of the function: -

Public Function HowManySpecificDays(StartDate As Date, EndDate As
Date) As DayCounts

and specifically the word "DayCounts"

Can anyone help?

Full code is as follows.

Private Sub CalcDays_Click()

Call HowManySpecificDays("01/01/2002", "31/12/2002")

Debug.Print "Between these 2 dates there are:"
Debug.Print Count.Mon & " Mondays"
Debug.Print Count.Tue & " Tuesdays"
Debug.Print Count.Wed & " Wednesdays"
Debug.Print Count.Thu & " Thursdays"
Debug.Print Count.Fri & " Fridays"
Debug.Print Count.Mon + Count.Tue + Count.Wed + Count.Thu +
Count.Fri & " Chargeable Days"
Debug.Print Count.TotDays & " Total Days"
End Sub
Public Function HowManySpecificDays(StartDate As Date, EndDate As
Date) As DayCounts

On Error GoTo HowManySpecificDays_Error

Dim dteDateStart As Date
Dim dteDateEnd As Date

Dim intMondays As Integer
Dim intTuesdays As Integer
Dim intWednesdays As Integer
Dim intThursdays As Integer
Dim intFridays As Integer
Dim intTotDays As Integer

Dim intDay As Integer
Dim intMonth As Integer
Dim intMonthCount As Integer

dteDateStart = Format(StartDate, "dd/mm/yyyy")

dteDateEnd = Format(EndDate, "dd/mm/yyyy")

intMonthCount = 1
intMonth = Month(dteDateStart)

'Loop through the date range and work out how many days in the
period
Do Until dteDateStart = dteDateEnd + 1

intDay = WeekDay(dteDateStart)

If Not intMonth = Month(dteDateStart) Then
intMonth = Month(dteDateStart)
intMonthCount = intMonthCount + 1
End If
Select Case intDay

Case 2 'Mondays
intMondays = intMondays + 1

Case 3 'Tuesdays
intTuesdays = intTuesdays + 1

Case 4 'Wednesdays
intWednesdays = intWednesdays + 1

Case 5 'Thursdays
intThursdays = intThursdays + 1

Case 6 'Fridays
intFridays = intFridays + 1

Case Else

End Select

dteDateStart = dteDateStart + 1
intTotDays = intTotDays + 1
Loop

With Count
.Mon = intMondays
.Tue = intTuesdays
.Wed = intWednesdays
.Thu = intThursdays
.Fri = intFridays
.Months = intMonthCount
.TotDays = intTotDays
End With

HowManySpecificDays = Count

On Error GoTo 0
Exit Function

HowManySpecificDays_Error:

MsgBox "Error " & Err.Number & " (" & Err.Description & ") in
procedure HowManySpecificDays"

End Function
Nov 12 '05 #1
2 3394

You haven't got a declaration for Count.

This will work
'************************************************* **
' Version 1
'************************************************* **
Private Type DayCounts
Mon As Long
Tue As Long
Wed As Long
Thu As Long
Fri As Long
Months As Long
TotDays As Long
End Type

Private Count As DayCounts

Private Sub CalcDays_Click()
Call HowManySpecificDays("01/01/2002", "31/12/2002")

Debug.Print "Between these 2 dates there are:"
Debug.Print Count.Mon & " Mondays"
Debug.Print Count.Tue & " Tuesdays"
Debug.Print Count.Wed & " Wednesdays"
Debug.Print Count.Thu & " Thursdays"
Debug.Print Count.Fri & " Fridays"
Debug.Print Count.Mon + Count.Tue + Count.Wed + Count.Thu +
Count.Fri & " Chargeable Days"
Debug.Print Count.TotDays & " Total Days"
End Sub
Public Function HowManySpecificDays(StartDate As Date, EndDate As Date) As
DayCounts

On Error GoTo HowManySpecificDays_Error

Dim dteDateStart As Date
Dim dteDateEnd As Date

Dim intMondays As Integer
Dim intTuesdays As Integer
Dim intWednesdays As Integer
Dim intThursdays As Integer
Dim intFridays As Integer
Dim intTotDays As Integer

Dim intDay As Integer
Dim intMonth As Integer
Dim intMonthCount As Integer

dteDateStart = Format(StartDate, "dd/mm/yyyy")

dteDateEnd = Format(EndDate, "dd/mm/yyyy")

intMonthCount = 1
intMonth = Month(dteDateStart)

'Loop through the date range and work out how many days in the period
Do Until dteDateStart = dteDateEnd + 1

intDay = Weekday(dteDateStart)

If Not intMonth = Month(dteDateStart) Then
intMonth = Month(dteDateStart)
intMonthCount = intMonthCount + 1
End If
Select Case intDay

Case 2 'Mondays
intMondays = intMondays + 1

Case 3 'Tuesdays
intTuesdays = intTuesdays + 1

Case 4 'Wednesdays
intWednesdays = intWednesdays + 1

Case 5 'Thursdays
intThursdays = intThursdays + 1

Case 6 'Fridays
intFridays = intFridays + 1

Case Else

End Select

dteDateStart = dteDateStart + 1
intTotDays = intTotDays + 1
Loop

With Count
.Mon = intMondays
.Tue = intTuesdays
.Wed = intWednesdays
.Thu = intThursdays
.Fri = intFridays
.Months = intMonthCount
.TotDays = intTotDays
End With

HowManySpecificDays = Count

On Error GoTo 0
Exit Function

HowManySpecificDays_Error:

MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure
HowManySpecificDays"

End Function
'************************************************* **
' /Version 1
'************************************************* **

But this is better
'************************************************* **
' Version 2
'************************************************* **
' In a module on it's own
Public Type DayCounts
Mon As Long
Tue As Long
Wed As Long
Thu As Long
Fri As Long
Months As Long
TotDays As Long
End Type

Public Function HowManySpecificDays(StartDate As Date, EndDate As Date) As
DayCounts

On Error GoTo HowManySpecificDays_Error

Dim dteDateStart As Date
Dim dteDateEnd As Date

Dim intMondays As Integer
Dim intTuesdays As Integer
Dim intWednesdays As Integer
Dim intThursdays As Integer
Dim intFridays As Integer
Dim intTotDays As Integer

Dim intDay As Integer
Dim intMonth As Integer
Dim intMonthCount As Integer

dteDateStart = Format(StartDate, "dd/mm/yyyy")

dteDateEnd = Format(EndDate, "dd/mm/yyyy")

intMonthCount = 1
intMonth = Month(dteDateStart)

'Loop through the date range and work out how many days in the period
Do Until dteDateStart = dteDateEnd + 1

intDay = Weekday(dteDateStart)

If Not intMonth = Month(dteDateStart) Then
intMonth = Month(dteDateStart)
intMonthCount = intMonthCount + 1
End If
Select Case intDay

Case 2 'Mondays
intMondays = intMondays + 1

Case 3 'Tuesdays
intTuesdays = intTuesdays + 1

Case 4 'Wednesdays
intWednesdays = intWednesdays + 1

Case 5 'Thursdays
intThursdays = intThursdays + 1

Case 6 'Fridays
intFridays = intFridays + 1

Case Else

End Select

dteDateStart = dteDateStart + 1
intTotDays = intTotDays + 1
Loop

With HowManySpecificDays
.Mon = intMondays
.Tue = intTuesdays
.Wed = intWednesdays
.Thu = intThursdays
.Fri = intFridays
.Months = intMonthCount
.TotDays = intTotDays
End With
On Error GoTo 0
Exit Function

HowManySpecificDays_Error:

MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure
HowManySpecificDays"

End Function

' Then called like this from any module
Private Sub CalcDays_Click()
Dim Count As DayCounts
Count = HowManySpecificDays("01/01/2002", "31/12/2002")

Debug.Print "Between these 2 dates there are:"
Debug.Print Count.Mon & " Mondays"
Debug.Print Count.Tue & " Tuesdays"
Debug.Print Count.Wed & " Wednesdays"
Debug.Print Count.Thu & " Thursdays"
Debug.Print Count.Fri & " Fridays"
Debug.Print Count.Mon + Count.Tue + Count.Wed + Count.Thu +
Count.Fri & " Chargeable Days"
Debug.Print Count.TotDays & " Total Days"
End Sub
'************************************************* **
' /Version 2
'************************************************* **

Although whether the code for the HowManySpecificDays function is correct I
wouldn't like to say.

Terry
"David Mitchell" <da**************@talk21.com> wrote in message
news:c3*************************@posting.google.co m...
I have tried using the following code to count the specific number of
each weekday but get a compile error "User defined type not defined"
which I think relates to the first line of the function: -

Public Function HowManySpecificDays(StartDate As Date, EndDate As
Date) As DayCounts

and specifically the word "DayCounts"

Can anyone help?

Full code is as follows.

Private Sub CalcDays_Click()

Call HowManySpecificDays("01/01/2002", "31/12/2002")

Debug.Print "Between these 2 dates there are:"
Debug.Print Count.Mon & " Mondays"
Debug.Print Count.Tue & " Tuesdays"
Debug.Print Count.Wed & " Wednesdays"
Debug.Print Count.Thu & " Thursdays"
Debug.Print Count.Fri & " Fridays"
Debug.Print Count.Mon + Count.Tue + Count.Wed + Count.Thu +
Count.Fri & " Chargeable Days"
Debug.Print Count.TotDays & " Total Days"
End Sub
Public Function HowManySpecificDays(StartDate As Date, EndDate As
Date) As DayCounts

On Error GoTo HowManySpecificDays_Error

Dim dteDateStart As Date
Dim dteDateEnd As Date

Dim intMondays As Integer
Dim intTuesdays As Integer
Dim intWednesdays As Integer
Dim intThursdays As Integer
Dim intFridays As Integer
Dim intTotDays As Integer

Dim intDay As Integer
Dim intMonth As Integer
Dim intMonthCount As Integer

dteDateStart = Format(StartDate, "dd/mm/yyyy")

dteDateEnd = Format(EndDate, "dd/mm/yyyy")

intMonthCount = 1
intMonth = Month(dteDateStart)

'Loop through the date range and work out how many days in the
period
Do Until dteDateStart = dteDateEnd + 1

intDay = WeekDay(dteDateStart)

If Not intMonth = Month(dteDateStart) Then
intMonth = Month(dteDateStart)
intMonthCount = intMonthCount + 1
End If
Select Case intDay

Case 2 'Mondays
intMondays = intMondays + 1

Case 3 'Tuesdays
intTuesdays = intTuesdays + 1

Case 4 'Wednesdays
intWednesdays = intWednesdays + 1

Case 5 'Thursdays
intThursdays = intThursdays + 1

Case 6 'Fridays
intFridays = intFridays + 1

Case Else

End Select

dteDateStart = dteDateStart + 1
intTotDays = intTotDays + 1
Loop

With Count
.Mon = intMondays
.Tue = intTuesdays
.Wed = intWednesdays
.Thu = intThursdays
.Fri = intFridays
.Months = intMonthCount
.TotDays = intTotDays
End With

HowManySpecificDays = Count

On Error GoTo 0
Exit Function

HowManySpecificDays_Error:

MsgBox "Error " & Err.Number & " (" & Err.Description & ") in
procedure HowManySpecificDays"

End Function

Nov 12 '05 #2
"Terry Kreft" <te*********@mps.co.uk> wrote in message news:<bn**********@newsreaderm1.core.theplanet.net >...
You haven't got a declaration for Count.


Terry,

I use this group daily, both to answer questions I have and to
increase my knowledge of all things Access. All to often someone
takes the time to post an answer to a question and there is no follow
up from the original poster.

Thank you, thank you, thank you!

Your solution works perfectly and has transformed my application.

Very best regards

David
Nov 12 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jack | last post by:
To whom may concern: In Asp.Net Calender control, how to display week days, like Wednesday as "Wed", Monday as "Mon", even my windows' region is set to Chinese? Thanks in advance! Jack...
5
by: Jeff | last post by:
Ok, first off, thanks to Bob for all the previous help. I got things running smooth now. My next dilemma isn't from that tour that I inherited, but on my own golf server. On this, only the...
6
by: aarklon | last post by:
Hi folks, I found an algorithm for calculating the day of the week here:- http://www.faqs.org/faqs/calendars/faq/part1/index.html in the section titled 2.5 what day of the week was 2 august...
7
by: Sam | last post by:
Hi, I use C# in my ASP.NET projects. Here's what I need to do: I want to add x business days to a given date i.e. add 12 business days to today's date. What is the best, fastest and most...
4
by: MLH | last post by:
MyString = "All men are created equal" Debug.PrintLen(MyString) Now that's easy. But how about just counting the letter "e"? Or, if I were curious to know how many commas were in the string....
4
by: Darknight850 | last post by:
Hello all, I am having a rather simple problem that for some reason i can not get to work. i have a date field, and i want to simply count all of the current dates(as in today) in that field. ...
3
by: Jim | last post by:
Guys I'm rusty. Haven't messed with Access in a while. I'm trying to determine how many workdays (or weekdays) between two dates. I don't think their are any "canned" functions within access -...
3
by: Keo Sophon | last post by:
Fredrik Lundh wrote: Hi, I've tried calendar.month_name, it displays empty string, while calendar.month_name is "January"? Why does calendar.month_name's index not start with index 0 as...
7
by: Mike | last post by:
I have a routine that's calculating business days but its not counting the weekend days that are between the start date and end date. If my start date is 9/26/08 and my end date is 10/01/08, I...
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
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: 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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
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.