Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error on query using function

Status
Not open for further replies.

bhaswar

MIS
Oct 9, 2002
8
IE
Hi
I wanted to calculate the number os working days in a query. So I went about wrint a function which accepts the start date end date and the calculates the number os working days:
Public Function getWorkingDays(startDate As String, endDate As String)

On Error GoTo Err_getWorkingDays

Dim intCounter As Long
Dim stDt As Date
Dim testDt As Date
Dim blnHoliday As Boolean
If IsNull(startDate) Or IsNull(endDate) Then
getWorkingDays = 0
Exit Function
End If
Debug.Print Month(startDate)
Debug.Print Month(endDate)

testDt = Format(startDate, "dd/mm/yyyy")
endDate = Format(endDate, "dd/mm/yyyy")
intCounter = 0

Do While testDt <= endDate
blnHoliday = True

Select Case testDt
Case #12/25/2003#, #12/26/2003#, #12/30/2003#, #12/31/2003#, #1/1/2004#
blnHoliday = True
Case Else
blnHoliday = False
End Select
If Weekday(testDt) = 6 Or Weekday(testDt) = 7 Then
blnHoliday = True
End If

If Not blnHoliday Then
intCounter = intCounter + 1
End If
testDt = testDt + 1
'Debug.Print testDt
Loop

getWorkingDays = intCounter

Exit_getWorkingDays:
Exit Function

Err_getWorkingDays:
Debug.Print Err.Number & Err.Description
If Err.Number = 13 Then
MsgBox (&quot;error here&quot;)
End If

Resume Exit_getWorkingDays

End Function

This works fine when there is a start and an end date. But whenever there is a blank end date it fails with a error 13 (type mismatch). There are also other errors that occur like 7 4 and 3.
I dont know what these other erro means!! I know 7 is out is memory, but why is it occuring?!
The query looks like:
SELECT Repairs.Customer, Repairs.[Product P/N], Repairs.[Product S/N], Repairs.[Rep Start Date], Repairs.[Rep Compl Date], Repairs.[Shipped Date], Repairs.[Stat Conf], Repairs.[Service Ref], getWorkingDays(Format([Rep Start Date],&quot;mm/dd/yy&quot;),Format([Rep Compl Date],&quot;mm/dd/yyyy&quot;)) AS TAT
FROM (Repairs INNER JOIN [Service Types] ON Repairs.FType = [Service Types].FType) INNER JOIN Products ON Repairs.[Product P/N] = Products.SPart;

being a newbie user I am lost. Could someone point me in the right direction.
1)How to deal with the errors especially 13
2) can I put a filter on my query to prevent blank end dates to go there?

Thanks

Bhaswar

 
Append at the end of the SQL ( before ; )

WHERE ([Rep Compl Date] is not null)

Best of luck
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top