2

I am looking to pass a date variable from Access to a SQL Server stored procedure. Please see my code:

VBA:

searchDate = 03/07/2014
cmd.Parameters.Append _
        cmd.CreateParameter("@searchDate", adDBTimeStamp, adParamInput, , searchDate)

searchDate is a date formatted as MM/DD/YYYY

SQL:

ALTER PROCEDURE [dbo].[spAppendActivity]
    @searchDate as datetime,
AS
   SET NOCOUNT ON;

   delete * from tbl_activity_losses;

   select [Date] 
   into tbl_activity_losses 
   from tbl_master_rec 
   where [Date] = @searchDate

I get an ODBC error stating

[Microsoft][ODBC SQL Server Driver] Conversion failed when converting date and/or time from character string.

Any help on how to pass this date field to my stored procedure would be greatly appreciated.

9
  • 1
    Try printing out the value of @searchDate in your sproc. I bet it will be very different from what you think you are sending. Commented Jun 2, 2014 at 21:36
  • It is in the format (yyyymmddhhmmss plus a fraction in billionths). I am not sure how to alter my code to pass a value like this. Do you have any ideas? Commented Jun 2, 2014 at 21:39
  • 1
    Is there any reason you chose a adDBTimeStamp? Commented Jun 2, 2014 at 21:40
  • I tried using adbdDate and it returns the error "[Microsoft][ODBC SQL Server Driver] Optional Feature Not Implemented. " I have not found a different way to pass a date variable. Do you have any suggestions? Commented Jun 2, 2014 at 21:43
  • I see that you've posted this question before. Did adVarChar work? Commented Jun 2, 2014 at 21:54

1 Answer 1

3

The problem is adDBTimeStamp. It is converting your simple date string into a timestamp. (NOT a date)

When that timestamp is passed to your stored procedure, SQL server has no idea how to turn that long string into a date.

Changing the type from adDBTimeStamp to adVarChar will keep it as a string and SQL Server will know how to parse it properly.

Additionally, you can change your stored procedure type from DateTime to varchar as well. SQL Server will know how to parse that into a date.

Sign up to request clarification or add additional context in comments.

1 Comment

When I try to pass it as varchar, I get the following ERROR: Conversion failed when converting date and/or time from character string."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.