Results 1 to 9 of 9
  1. #1
    Glenley is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Dec 2021
    Posts
    91

    Call a stored procedure in VBA while passing parameters with variables


    I'm trying to call a stored procedure in SQL Server with VBA. The stored proc has three parameters, @CC int, @SDate datetime2, @EDate datetime2. I'm able to get the SQL string correct for the integer but once I try to pass the dates, it throws errors. I'll paste the code below

    Code:
        Dim db As Database, qd As QueryDef, strSQL As String
        Dim ConCompany As Long, SDate As Date, EDate As Date
    
    
        SDate = InputBox("Start date")
        EDate = InputBox("End date")
        ConCompany = Forms!MainMenuF!cmbCompany
    
        Set db = CurrentDb
        Set qd = db.QueryDefs("spApplicationReportPTQ")
    
    
        qd.Connect = ConString
        qd.SQL = "EXECUTE dbo.spApplicationReport " & ConCompany & ", " & SDate & "," & EDate
        DoCmd.OpenQuery ("spApplicationReportPTQ")
    
    
        qd.Close
        db.Close
        Set qd = Nothing
        Set db = Nothing

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Concatenate the dates with Format for USA dates.?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    Glenley is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Dec 2021
    Posts
    91
    I figured it out. Needed to surround each param with single quotes and then double

    Code:
    qd.SQL = "EXECUTE dbo.spApplicationReport '" & ConCompany & "', '" & SDate & "','" & EDate & "'"

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Really?
    Surely that makes SDate and EDate just literals?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  5. #5
    Glenley is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Dec 2021
    Posts
    91
    SQL Server reads them correctly. It returns the desired records. I've tried tweaking the syntax a million different ways and this was the only way that worked. SDate and EDate are dimmed as Date and not String so maybe that's why

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    SDate and EDate are not literals. They are concatenated variables in the code and their values are in the compiled and executed expression. The apostrophes are also concatenated into the compiled expression as data delimiters. A number type field should not need these delimiters. In an Access query the date values would be delimited with # characters.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Quote Originally Posted by June7 View Post
    SDate and EDate are not literals. They are concatenated variables in the code and their values are in the compiled and executed expression. The apostrophes are also concatenated into the compiled expression as data delimiters. A number type field should not need these delimiters. In an Access query the date values would be delimited with # characters.
    Yes, I see my mistake there, down to the fact I have never used a ' with dates, then again I have not used Acess with SQL Server either.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  8. #8
    Join Date
    Apr 2017
    Posts
    1,681
    To avoid all this hassle with various possible date formats and regional settings, I'd have both date parameters as integers. The calling app converts dates to integers and uses those, and the function will convert them to dates before using them. (In case you want to have a possibility to use dates as parameters too, format date parameters as Variant, and add additional parameter to determine date parameter type).

  9. #9
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    SQL server would want a date to be passed in as '2023-10-09' yyyy-mm-dd .

    Apart from the delimiter Access will accept the same layout and it is unambiguous so I use it all the time.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

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

Similar Threads

  1. Failure to call stored procedure
    By JAPA1972 in forum SQL Server
    Replies: 3
    Last Post: 02-11-2022, 03:01 AM
  2. Replies: 3
    Last Post: 10-09-2019, 10:26 AM
  3. Passing Params To Stored Procedure
    By jo15765 in forum Programming
    Replies: 3
    Last Post: 04-11-2017, 07:19 AM
  4. Replies: 1
    Last Post: 11-14-2016, 04:14 PM
  5. Passing variable values to Stored Procedure
    By rodrigopcnet in forum Access
    Replies: 1
    Last Post: 04-14-2010, 10:35 AM

Tags for this Thread

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