Solved

How Can I use this function in a Query?

Posted on 2013-10-29
3
301 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

738 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