Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Erroneous results using date parameter in an Access function passed to a SQL stored procedure

Posted on 2011-03-09
Medium Priority
Last Modified: 2012-05-11
I am receiving strange results when trying to pass a date value as a function parameter to a SQL stored procedure.
The following code is what I am using.  NOTE: I have tried various solutions such as; a date and/or a string type for the function parameter, also, a hard-coded value.

Here is the code:

Option Explicit

     'Function ParamSPT2(MyParam As String)
     Function ParamSPT2(MyParam As String)

     'MyParam = Format(MyParam, "yyyy-mm-dd hh:nn:ss")
     On Error GoTo ODBCErrHandler

        Dim MyDb As Database, MyQry As QueryDef, MyRS As Recordset
         Set MyDb = CurrentDb()
         Set MyQry = MyDb.CreateQueryDef("")

         ' Type a connect string using the appropriate values for your
         ' server.
         MyQry.connect = "ODBC;DSN=EPM_DEV;Description=EPM_Development;UID=user1;Trusted_Connection=Yes;DATABASE=PSEOC IBIXImport"

'MyParam = 2011 - 1 - 1
        MyParam = Format(MyParam, "yyyy-mm-dd hh:nn:ss")
         Debug.Print MyParam
         ' Set the SQL property and concatenate the variables.
         'MyQry.SQL = "sp_server_info " & MyParam
         MyQry.SQL = "Get_IBIX_Success_Totals_By_Date" & MyParam
         MyQry.ReturnsRecords = True
         Set MyRS = MyQry.OpenRecordset()

         Debug.Print MyRS!attribute_id, MyRS!attribute_name, MyRS!attribute_value

   Exit Function
   Dim errX As DAO.Error

   If Errors.Count > 1 Then
      For Each errX In DAO.Errors
         Debug.Print "ODBC Error"
         Debug.Print errX.Number
         Debug.Print errX.Description
      Next errX
      Debug.Print "VBA Error"
      Debug.Print Err.Number
      Debug.Print Err.Description
   End If
   Resume Exit_function
  End Function
Question by:psueoc
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
  • 5
  • 5
LVL 15

Expert Comment

ID: 35082504
MyQry.SQL = "Get_IBIX_Success_Totals_By_Date" & MyParam

there's no space between the proc name and the date - this may be the source of the issue

also, you probably need to wrap the parameter in quotes, like this:

MyQry.SQL = "Get_IBIX_Success_Totals_By_Date '" & MyParam & "'"

Open in new window


Author Comment

ID: 35082554
That helped but not out of the woods yet here are the results:
Why is it converting it to 1905-07-01 00:00:00?

1905-07-01 00:00:00
VBA Error
Item not found in this collection.
LVL 15

Expert Comment

ID: 35082608
I'm not sure - is that from the debug.print?

Try removing the format(). SQL should be able to handle that date format just fine without any additional processing.

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.


Author Comment

ID: 35082712
Yes the 1901... was from debug.print.
I have removed formatting, here are the results.  The item not found probably means no records? Because it doesn't have the month and day portion needed for the query to run results?

VBA Error
Item not found in this collection.
LVL 15

Accepted Solution

derekkromm earned 2000 total points
ID: 35082724
Did you put quotes around the whole thing? Copy/paste the line of code from my post above. You need to put single quotes around the parameter. Right now its seeing 2011-1-1 and interpreting it as an expression: hence 2009 being sent as a parameter instead of "2011-1-1"

Author Comment

ID: 35082775
I'm not sure about that because I copied your recommendation form above and it returned 2009.

MyQry.SQL = "Get_IBIX_Success_Totals_By_Date '" & MyParam & "'"
LVL 15

Expert Comment

ID: 35082792
can you debug.print the myparam?
also debug.print this: "Get_IBIX_Success_Totals_By_Date '" & MyParam & "'"

Author Comment

ID: 35082842
Additional information:
 I have tried entering the parameter as follows at the Immediate window but without successful results:



LVL 15

Assisted Solution

derekkromm earned 2000 total points
ID: 35082861
how about this:



Author Closing Comment

ID: 35083036
Thank you very much. You have solved this problem.  I have follow-up which I will post immediately for additional points.

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
As with any other System Center product, the installation for the Authoring Tool can be quite a pain sometimes. This article serves to help you avoid making these mistakes and hopefully save you a ton of time on troubleshooting :)  Step 1: Make sur…
Viewers will learn how to maximize accessibility options in an Excel workbook for users with accessibility issues.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

636 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