Results 1 to 6 of 6
  1. #1
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672

    Run Parameterized SQL Server Stored Proc From VBA

    I am attempting to run a sql server stored procedure from access vba, but I keep getting an error of


    Code:
    Conversion failed when converting date and/or time from character string.
    This is my syntax - what do I need to alter in order for this to successfully execute?

    Code:
    Private Sub btnConnect()
    Dim cmd As ADODB.Command, d1 As String, d2 As String
    	Set cmd = New ADODB.Command
    	d1 = "'" & Me.d1 & "'"
    	d2 = "'" & Me.d2 & "'"
    	cmd.ActiveConnection = "Provider=sqloledb;Server=Server;Database=DB;Trusted_Connection=yes;"
    	cmd.CommandType = adCmdStoredProc
    	cmd.CommandText = "SQLStoredProc"
    	cmd.Parameters.Append cmd.CreateParameter("@d1", adVarChar, adParamInput, 255, d1)
    	cmd.Parameters.Append cmd.CreateParameter("@d2", adVarChar, adParamInput, 255, d2)
    	cmd.Execute
    End Sub
    
    
    
    
    ALTER Procedure [dbo].[SQLStoredProc]
    (
    	   @d1 varchar(100)
    	   ,@d2 varchar(100) 
    )
    As
    
    
    Select name, address, phone from employees where hiredate between @d1 And @d2

  2. #2
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    If hiredate is a date/time field, I'd declare the variables as such, and just pass dates without the apostrophes.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672
    Quote Originally Posted by pbaldy View Post
    If hiredate is a date/time field, I'd declare the variables as such, and just pass dates without the apostrophes.
    I am unfortunately not able to modify the stored procedure. So I am stuck dealing with it as a varchar value. However, it will be passing in a date 03/15/2017 format

  4. #4
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672
    If I format my dates like so
    Code:
    d1 = Format(Me.d1, "YYYY-MM-DD")
    I now get a query timeout error. How do I extend the amount of time the stored procedure can run before it times out?

  5. #5
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I'd have to test using varchar, which I'll try to do if it slows down. Have you tried a pass through query? I only use a command object to execute action queries. A simple select I'd use a pass-through.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672
    I am actually running a huge CTE query that runs an insert into a table and I select from that table, so I do not think a pass-thro would be beneficial here (think being the key word). I only posted enough for example sake to show what I was trying to achieve.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 2
    Last Post: 06-01-2016, 08:52 AM
  2. Database stored on server read-only
    By GGCR in forum Access
    Replies: 2
    Last Post: 05-23-2014, 09:42 AM
  3. Access DB stored on Mac server
    By willrollo in forum Access
    Replies: 0
    Last Post: 03-05-2013, 01:06 PM
  4. Calling Stored Proc in MS Access 2007 without creating query?
    By DistillingAccess in forum Programming
    Replies: 1
    Last Post: 08-03-2010, 09:38 AM
  5. Replies: 3
    Last Post: 05-23-2010, 05:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums