Hi all
I am new to Access/VBA and I have this issue which I have not been able to figure out.
I am building this function to count the number of Thursday between two days base on the pay frequency(1 Weekly, 2 Biweekly, 3 Monthly). The start date and ending date is not fix. Each employee could have different start and end date.
The function works fine when I am testing on the module. From January to December I expect 52/26/12 according to the pay frequency, but when I use in a query it does not give me any value for 2017. I got #Error. if I use start and day on 2016 it does works.
Option Compare Database
Option Explicit
Public Function CountNODays(Optional STDate As Date, Optional EndDate As Date, Optional PRFrq As Integer) As Integer
Dim ICount As Integer
Dim STDateNO As Integer
'STDate = #12/1/2017#
EndDate = #12/31/2017#
STDate = STDate + (8 - Weekday(STDate, vbThursday))
'PRFrq = 1
STDateNO = Weekday(STDate)
'If STDateNO = 5 Then
' 'looping up to thursday and then use this date as pay date
'
'Else
' STDate = CDate(STDate)
' Do Until STDateNO = 5
' STDate = DateAdd("d", 1, STDate)
' STDateNO = STDateNO + 1
'
' Loop
'
'
'
'End If
STDate = CDate(STDate)
ICount = 0
'Selecting the pay frequency to calculate the weeks
Select Case PRFrq
Case 1
Do While STDate <= EndDate
STDate = DateAdd("d", 7, STDate)
ICount = ICount + 1
Loop
CountNODays = ICount
Case 2
Do While STDate <= EndDate
STDate = DateAdd("d", 14, STDate)
ICount = ICount + 1
Loop
CountNODays = ICount
Case 3
Do While STDate <= EndDate
STDate = DateAdd("m", 1, STDate)
ICount = ICount + 1
Loop
CountNODays = ICount
End Select
CountNODays = ICount
End Function
I am new to Access/VBA and I have this issue which I have not been able to figure out.
I am building this function to count the number of Thursday between two days base on the pay frequency(1 Weekly, 2 Biweekly, 3 Monthly). The start date and ending date is not fix. Each employee could have different start and end date.
The function works fine when I am testing on the module. From January to December I expect 52/26/12 according to the pay frequency, but when I use in a query it does not give me any value for 2017. I got #Error. if I use start and day on 2016 it does works.
Option Compare Database
Option Explicit
Public Function CountNODays(Optional STDate As Date, Optional EndDate As Date, Optional PRFrq As Integer) As Integer
Dim ICount As Integer
Dim STDateNO As Integer
'STDate = #12/1/2017#
EndDate = #12/31/2017#
STDate = STDate + (8 - Weekday(STDate, vbThursday))
'PRFrq = 1
STDateNO = Weekday(STDate)
'If STDateNO = 5 Then
' 'looping up to thursday and then use this date as pay date
'
'Else
' STDate = CDate(STDate)
' Do Until STDateNO = 5
' STDate = DateAdd("d", 1, STDate)
' STDateNO = STDateNO + 1
'
' Loop
'
'
'
'End If
STDate = CDate(STDate)
ICount = 0
'Selecting the pay frequency to calculate the weeks
Select Case PRFrq
Case 1
Do While STDate <= EndDate
STDate = DateAdd("d", 7, STDate)
ICount = ICount + 1
Loop
CountNODays = ICount
Case 2
Do While STDate <= EndDate
STDate = DateAdd("d", 14, STDate)
ICount = ICount + 1
Loop
CountNODays = ICount
Case 3
Do While STDate <= EndDate
STDate = DateAdd("m", 1, STDate)
ICount = ICount + 1
Loop
CountNODays = ICount
End Select
CountNODays = ICount
End Function