Solved

How Can I use this function in a Query?

Posted on 2013-10-29
3
286 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 119

Accepted Solution

by:
Rey Obrero 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now