Solved

How Can I use this function in a Query?

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

705 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