Solved

How Can I use this function in a Query?

Posted on 2013-10-29
3
291 Views
Last Modified: 2013-10-30
I have stored the following function in a module and I have tried to use it in a query as follows but I am getting a Data Type mismatch error when I run the query yet when I step through the function the result is a date field as expected. What am I doing wrong?

SELECT Tasks.*, Components.*, fStartOrder([StartOn],[DueDate]) AS Sort1, Nz([pkTaskType],"zzzz") AS Expr2, Nz([Priority],999) AS Expr3
FROM Tasks LEFT JOIN (Components LEFT JOIN tblTaskTypes ON Components.TaskType = tblTaskTypes.pkTaskType) ON Tasks.ID = Components.fkID
WHERE (((Components.Completed)<>True))
ORDER BY fStartOrder([StartOn],[DueDate]), Tasks.ID;



Function fStartOrder(StartOn As Date, DueDate As Date) As Date
    'Used in query to set the order of the StartOn value
    Dim StartOrder As Date
   
    If IsNull(StartOn) And IsNull(DueDate) Then
        StartOrder = Date
    ElseIf IsNull(DueDate) And Not IsNull(StartOn) Then
        StartOrder = StartOn
    ElseIf IsNull(StartOn) And Not IsNull(DueDate) Then
        StartOrder = DueDate
    ElseIf DueDate <= StartOn Then
        StartOrder = DueDate
    ElseIf StartOn < DueDate Then
        StartOrder = StartOn
    Else 'trap for missing entries
        StartOrder = #1/1/2001#
    End If
       
   fStartOrder = StartOrder
End Function
0
Comment
Question by:Rob4077
3 Comments
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 500 total points
ID: 39609050
try changing this line

Function fStartOrder(StartOn As Date, DueDate As Date) As Date

with

Function fStartOrder(StartOn, DueDate) As Date


or leave as is
Function fStartOrder(StartOn As Date, DueDate As Date) As Date

but change the way you call the function in the query, like this

fStartOrder(nz([StartOn],#1/1/1900#),nz([DueDate],#1/1/1900#))
0
 
LVL 31

Expert Comment

by:Helen_Feddema
ID: 39609116
Or do some testing for valid dates in the function itself, using the IsDate function.  Just checking for the value not being Null may not be enough.
0
 

Author Comment

by:Rob4077
ID: 39610921
Thanks for your suggestions. The fields DueDate and StartOn are both defined as date fields in the table so I thought that passing them to a function as date fields would work. Looks like it wasn't feeding information where the date field was null. As suggested by Capricorn1, changing it to a variant made it work (i.e. Function fStartOrder(StartOn, DueDate) As Date).
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question