?
Solved

How Can I use this function in a Query?

Posted on 2013-10-29
3
Medium Priority
?
307 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 2000 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

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.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

809 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