I'm using a workdays function I picked up from one of the access forums, and have encountered a strange issue. When using this within a query and setting criteria, i.e. > 2 or = 4 etc, it works fine so long as I don't have criteria for other fields that are e.g. <> X AND <> Y. It's fine if the criteria for the other fields are e.g. = X. With the former, it returns an error message saying datatype mismatch. Incidentally, the exact function is not important, I've tried 2 or 3 different workday functions as well as one I wrote myself, and all return the same error. What's odd is that if I apply the sql equivalent of the any of these functions, the query works fine. Any answers?
6 2119
Hi, Kevin.
Could you post the function code or link to the source?
It seems that just changing the function returning value type to Variant may solve the problem.
Hi FishVal, here are three that I used, all with the same result. I think I did play around the data type though couldn't swear to it. - Option Compare Database
-
Option Explicit
-
'not used in programme, causes problems when using with criteria in filtered events "all other breaches"
-
'... but sql equivalent does work.
-
Public Function fWeekends(StartDate As Date) As Integer
-
-
Select Case Weekday(StartDate)
-
Case 5
-
fWeekends = (Date - StartDate) - 3
-
Case 6
-
fWeekends = (Date - StartDate) - 2
-
Case Else
-
fWeekends = (Date - StartDate) - 1
-
End Select
-
-
End Function
-
Public Function fWorkDays(StartDate As Date, EndDate As Date) As Long
-
On Error GoTo Err_fWorkDays
-
-
Dim intCount As Integer
-
Dim rst As DAO.Recordset
-
Dim DB As DAO.Database
-
-
Set DB = CurrentDb
-
Set rst = DB.OpenRecordset("SELECT [HolidayDate] FROM tblHolidays", dbOpenSnapshot)
-
-
'StartDate = StartDate + 1
-
'To count StartDate as the 1st day comment out the line above
-
-
intCount = 0
-
-
Do While StartDate <= EndDate
-
-
If Not IsNull(StartDate) And Not IsNull(EndDate) Then
-
rst.FindFirst "[HolidayDate] = #" & StartDate & "#"
-
If Weekday(StartDate) <> vbSunday And Weekday(StartDate) <> vbSaturday Then
-
If rst.NoMatch Then
-
intCount = intCount + 1
-
End If
-
End If
-
End If
-
-
StartDate = StartDate + 1
-
-
Loop
-
-
fWorkDays = intCount
-
-
Exit_fWorkDays:
-
Exit Function
-
-
Err_fWorkDays:
-
Select Case Err
-
-
Case Else
-
MsgBox Err.Description
-
Resume Exit_fWorkDays
-
End Select
-
-
End Function
-
-
'not used in programme, reference only
-
Function fWorkDays1(StartDate As Date, EndDate As Date) As Long
-
'Function designed by Thom Rose - Permission to use is granted as long as you acknowledge the author
-
'This function calculates the number of workdays between two dates.
-
'The number of workdays is inclusive of the beginning and ending dates.
-
'There must be a table present called tblHolidays which contains the field dtObservedDate in the format date/time
-
'The table must list the dates of holidays to be observed.
-
'The user may include other fields in that table, for example, a description of the holiday being observed.
-
-
Dim lngTotalDays As Long
-
Dim lngTotalWeeks As Long
-
Dim dtNominalEndDay As Date
-
Dim lngTotalHolidays As Long
-
-
'Check to see if dtStartDay > dtEndDay. If so, then switch the dates
-
If StartDate > EndDate Then
-
dtNominalEndDay = StartDate
-
StartDate = EndDate
-
EndDate = dtNominalEndDay
-
End If
-
'Here are how many weeks are between the two dates
-
lngTotalWeeks = DateDiff("w", StartDate, EndDate)
-
'Here are the number of weekdays in that total week
-
lngTotalDays = lngTotalWeeks * 5
-
'Here is the date that is at the end of that many weeks
-
dtNominalEndDay = DateAdd("d", (lngTotalWeeks * 7), StartDate)
-
'Now add the number of weekdays between the nominal end day and the actual end day
-
While dtNominalEndDay <= EndDate
-
If Weekday(dtNominalEndDay) <> 1 Then
-
If Weekday(dtNominalEndDay) <> 7 Then
-
lngTotalDays = lngTotalDays + 1
-
End If
-
End If
-
dtNominalEndDay = dtNominalEndDay + 1
-
Wend
-
'Here are how many holiday days there are between the two days
-
lngTotalHolidays = DCount("HolidayDate", "tblHolidays", "HolidayDate <= #" & EndDate & "# AND HolidayDate >= #" & StartDate & "# AND Weekday(HolidayDate) <> 1 AND Weekday(HolidayDate) <> 7")
-
Debug.Print lngTotalHolidays
-
'Here are how many total days are between the two dates - this is inclusive of the start and end date
-
fWorkDays1 = lngTotalDays - lngTotalHolidays
-
-
End Function
Hi, Kevin.
Just to ensure we are mentioning the same.
Where the error occurs, in code or in query?
Hi FishVal
Errors occur when I use the functions in queries. Here's a typical example; - SELECT *
-
FROM qryFilterBase
-
WHERE (((fworkdays([DatePosted],Date()))>10) And ((qryFilterBase.ActionID)=36) And ((qryFilterBase.DateCompleted) Is Null));
Earlier, the trigger for the problem seemed to be the use of additional criteria, i.e. if I removed the "And ((qryFilterBase.ActionID)=36) " section from the string as a test, it worked. But not today - sometimes it does, sometimes it doesn't. I don't really understand why it's failing - in this instance there's a date in the [dateposted] field for every record, and replacing that part of the string with "((Date()-[dateposted])>10)" returns just 5 or 6 records, so you'd think it wouldn't struggle too much. I suspect that this function is behind the db suddenly running incredibly slowly when split and deployed on the network - when not bothering to calculate workdays it ran quite well. Any fixes/alternative suggestions would be very helpful; I have to return around 30k - 40k records where the number of workdays is calculated, and right now I'm starting to think I should find another way altogether!
Thanks
Kevin
Hi FishVal
Errors occur when I use the functions in queries. Here's a typical example; - SELECT *
-
FROM qryFilterBase
-
WHERE (((fworkdays([DatePosted],Date()))>10) And ((qryFilterBase.ActionID)=36) And ((qryFilterBase.DateCompleted) Is Null));
Earlier, the trigger for the problem seemed to be the use of additional criteria, i.e. if I removed the "And ((qryFilterBase.ActionID)=36) " section from the string as a test, it worked. But not today - sometimes it does, sometimes it doesn't. I don't really understand why it's failing - in this instance there's a date in the [dateposted] field for every record, and replacing that part of the string with "((Date()-[dateposted])>10)" returns just 5 or 6 records, so you'd think it wouldn't struggle too much. I suspect that this function is behind the db suddenly running incredibly slowly when split and deployed on the network - when not bothering to calculate workdays it ran quite well. Any fixes/alternative suggestions would be very helpful; I have to return around 30k - 40k records where the number of workdays is calculated, and right now I'm starting to think I should find another way altogether!
Thanks
Kevin
Hi, Kevin.
I've taken a look at the code. It has some weak places and doesn't seem to be optimal. -
Public Function fWorkDays(StartDate As Date, EndDate As Date) As Long
-
On Error GoTo Err_fWorkDays
-
-
Dim intCount As Integer
-
Dim rst As DAO.Recordset
-
Dim DB As DAO.Database
-
-
Set DB = CurrentDb
-
Set rst = DB.OpenRecordset("SELECT [HolidayDate] FROM tblHolidays", dbOpenSnapshot)
-
-
'StartDate = StartDate + 1
-
'To count StartDate as the 1st day comment out the line above
-
-
intCount = 0
-
-
Do While StartDate <= EndDate
-
-
If Not IsNull(StartDate) And Not IsNull(EndDate) Then
-
rst.FindFirst "[HolidayDate] = #" & StartDate & "#"
-
If Weekday(StartDate) <> vbSunday And Weekday(StartDate) <> vbSaturday Then
-
If rst.NoMatch Then
-
intCount = intCount + 1
-
End If
-
End If
-
End If
-
-
StartDate = StartDate + 1
-
-
Loop
-
-
fWorkDays = intCount
-
-
Exit_fWorkDays:
-
Exit Function
-
-
Err_fWorkDays:
-
Select Case Err
-
-
Case Else
-
MsgBox Err.Description
-
Resume Exit_fWorkDays
-
End Select
-
-
End Function
-
- Function arguments are Date type. Though you say you have no nulls in your table, it would be generally a good idea to declare them as Variants and check for Null before calculations.
- You search recordset for each day in the range. I suppose it will be more effective to check each record in recordset whether it falls in the given range.
- Saturdays and Sundays quantity may be calculated using simple math.
Hi Fishval
[*] Function arguments are Date type. Though you say you have no nulls in your table, it would be generally a good idea to declare them as Variants and check for Null before calculations.[*] You search recordset for each day in the range. I suppose it will be more effective to check each record in recordset whether it falls in the given range.[*] Saturdays and Sundays quantity may be calculated using simple math.
Thanks, I'll try these suggestions. I've already found that once I checked for nulls properly, the function worked as expected, so that's half the battle won.
Kevin
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Thomas Matthews |
last post by:
Hi,
I'm getting linking errors when I declare a variable in the
global scope, but not inside a function. The declarations
are the same (only the names have been changed...).
class Book
{...
|
by: Clay |
last post by:
I really need to compute elapsed time using Access and VBA. I have two
date/time Access table fields, one for Start and one for Finish. I am
trying to calculate net workday work hours. If a trouble...
|
by: jimfortune |
last post by:
I have an A97 module called modWorkdayFunctions in:
http://www.oakland.edu/~fortune/WorkdayFunctions.zip
It allows the counting of workdays taking into consideration up to 11
U.S. holidays. ...
|
by: Dixie |
last post by:
I am trying to calculate the number of workdays between two dates with
regards to holidays as well. I have used Arvin Meyer's code on the Access
Web, but as I am in Australia and my date format is...
|
by: Larax |
last post by:
Best explanation of my question will be an example, look below at this
simple function:
function SetEventHandler(element)
{
// some operations on element
element.onclick =
function(event)
{
|
by: brnkstyle |
last post by:
I figured out how to calculate the work day given two values but all of my records have dates and i want to be able to calcuate the average time it takes to do a job per month so basically take all...
|
by: CryptiqueGuy |
last post by:
Consider the variadic function with the following prototype:
int foo(int num,...);
Here 'num' specifies the number of arguments, and assume that all the
arguments that should be passed to this...
|
by: souporpower |
last post by:
Hello All
I am trying to activate a link using Jquery. Here is my code;
<html>
<head>
<script type="text/javascript" src="../../resources/js/
jquery-1.2.6.js"</script>
<script...
|
by: ctj951 |
last post by:
I have a very specific question about a language issue that I was
hoping to get an answer to. If you allocate a structure that
contains
an array as a local variable inside a function and return...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
|
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,...
|
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...
|
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...
| |