Originally Posted by
monsterlucifer
If I input the following in design view, it works as expected: WrkDays: CalcWorkdays(#[Start Date]#,#[End Date)#)
It should not, as written. Missing ] after End Date, one too many ) plus the # are a problem if you are passing a date that is correctly formatted for your system's regional settings. Remove the #'s, add the ] and make sure the dates entered are compatible with your regional settings. Also, two digit years is a format that no one should be using anymore, IMHO. On my pc, this (31/07/15) is interpreted as year 1931.
The expression you have entered has an invalid date value.
Pretty sure it's the #'s around a valid date. Use these only when you are using a literal date string. The prompt converts a valid date string to a date data type (assuming it is convertible to a date).
When I run the query, it asks for Start Date & End Date but returns 0 as result which means that it is not working as expected.
Whenever your function fails, it will return 0 as you have designed it. Use this corrected expression: WrkDays: CalcWorkdays([Start Date],[End Date]), put a break on your function, then run the query and step through to follow the execution. I advise this as a learning excercise to be done before you make changes. Then I would repeat after making these changes:
Code:
If IsDate(StartDate) And IsDate(EndDate) Then
If EndDate <= StartDate Then 'if you reverse this, you do not need the next line or else portion
CalcWorkdays = 0 'you already have set function to zero, not needed
Else
LTotalDays = DateDiff("d", StartDate - 1, EndDate)
If IsDate(StartDate) And IsDate(EndDate) Then
If EndDate > StartDate Then
LTotalDays = DateDiff("d", StartDate - 1, EndDate)
Code:
LTotalDays = DateDiff("d", StartDate - 1, EndDate)
Your code will likely fail here as you have not declared the data types for startdate or enddate. Even though it will work up to here, the date is not interpreted by function as a date when I run it. Change to this
Code:
Function CalcWorkdays(StartDate as Date, EndDate as Date) As Integer OR
Function CalcWorkdays(StartDate, EndDate) As Integer
Dim StartDate as Date, EndDate as Date
notice each variable on a multi-declaration line MUST be declared individually, otherwise all others are variants.