Solved

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

Posted on 2011-03-09
10
363 Views
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()
         MyRS.MoveFirst

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

         MyQry.Close
         MyRS.Close
         MyDb.Close
         
Exit_function:
   Exit Function
         
ODBCErrHandler:
   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
   Else
      Debug.Print "VBA Error"
      Debug.Print Err.Number
      Debug.Print Err.Description
   End If
   Resume Exit_function
     
  End Function
0
Comment
Question by:psueoc
  • 5
  • 5
10 Comments
 
LVL 15

Expert Comment

by:derekkromm
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

0
 

Author Comment

by:psueoc
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?

?ParamSPT2(2011-01-01)
1905-07-01 00:00:00
VBA Error
 3265
Item not found in this collection.
0
 
LVL 15

Expert Comment

by:derekkromm
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.
0
 

Author Comment

by:psueoc
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?

2009
VBA Error
 3265
Item not found in this collection.
0
 
LVL 15

Accepted Solution

by:
derekkromm earned 500 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"
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:psueoc
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 & "'"
0
 
LVL 15

Expert Comment

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

Author Comment

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

?ParamSPT2(2011-01-01)

?ParamSPT2(2011/01/01)

?ParamSPT2(20110101)
0
 
LVL 15

Assisted Solution

by:derekkromm
derekkromm earned 500 total points
ID: 35082861
how about this:

?ParamSPT2("2011-01-01")

0
 

Author Closing Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

The new Microsoft OS looks great, is easier than ever to upgrade to, it is even free.  So what's the catch?  If you don't change the privacy settings, Microsoft will, in accordance with the (EULA) you clicked okay to without reading, collect all the…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

867 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

12 Experts available now in Live!

Get 1:1 Help Now