Access VBA Parameter in passthrough query to SQL Server

后端 未结 4 1582
情歌与酒
情歌与酒 2020-12-11 10:51

I have several queries in an MS Access database. Some of these use parameters. I use the following code in VBA to provide the query with these parameters:

VB

相关标签:
4条回答
  • 2020-12-11 11:31

    Simply create a pass-though query in Access and save it.

    Ensure that the PT query works. It will likely look like:

    Exec MySpName '2017-01-01', '2017-05-31'

    Again: 100% Make sure the query works when you click on it in Access. At this point you not written any VBA code.

    Once you have above pass through query working, then in VBA you can do this:

    Dim strStartDate    As String
    Dim strEndDate      As String
    Dim strSQL          As String
    
    strStartDate = "'" & Format(Me.dpFrom, "yyyy-mm-dd") & "'"
    strEndDate = "'" & Format(Me.dpTo, "yyyy-mm-dd") & "'"
    
    
    strSQL = "exec MyStoreProc " & strStartDate & "," & strEndDate
    
    With CurrentDb.QueryDefs("QryMyPass")
    
      .SQL = strSQL
      Set rst = .OpenRecordset
    
    End With
    
    0 讨论(0)
  • 2020-12-11 11:33

    If I remember right, in a pass-through query, you are passing the query definition directly to the engine in which it is going to run. So, you will have to use the SQL Server syntax for your query instead of the Access VBA syntax. Give that a try.

    Also, the same goes for a Stored procedure. Use the syntax like you were to execute through SSMS.

    "exec sp_mysp var1 var2" and so on.

    0 讨论(0)
  • 2020-12-11 11:38

    The reply from Albert Kallal was spot on. Thanks Albert. I tried to comment after signing up but... did not have enough points to comment so... Hoping this goes through....

    The only thing I changed was....

    I replaced the Set rst = .OpenRecordset with...CurrentDb.QueryDefs("q_PTO_SubmitNewRequest").Execute

    Thanks again for posting this. It really was a HUGE help. I had many complex .adp projects years ago and am working with a client needing similar functionality. It looks like I can mirror the .adp functionality using the pass through queries. Very Cool :)

    With CurrentDb.QueryDefs("q_PTO_SubmitNewRequest")

      .SQL = strSQL
    

    End With

    CurrentDb.QueryDefs("q_PTO_SubmitNewRequest").Execute
    
    0 讨论(0)
  • 2020-12-11 11:53

    Consider building a named stored procedure that resides in SQL Server and have MS Access call it passing parameters using ADO as opposed to your current DAO method since you require parameterization. Then bind results to a recordset:

    SQL Server Stored Proc

    CREATE PROCEDURE myStoredProc 
       @InvLineEntryDateBegin DATE = '2017-01-01',
       @InvLineEntryDateEnd DATE = '2017-05-31'
    AS
    
    BEGIN
       SET NOCOUNT ON;    
    
       SELECT WarehouseCode, Count(PickOrderNr) as Som
       FROM ( bla bla bla ... ;
    
    END
    

    VBA

    ' SET REFERENCE TO Microsoft ActiveX Data Object #.# Library
    Dim conn As ADODB.Connection, cmd As ADODB.Command, rst As ADODB.Recordset
    Dim startDate As Date, endDate As Date   
    
    If IsNull(Me.dpFrom) Or IsNull(Me.dpTo) Then
        MsgBox "Please select a date!", vbCritical, "MISSING DATE"
        Exit Sub
    End if    
    If (Me.dpFrom.Value > Me.dpTo.Value) Then
        MsgBox "Start date is bigger than the end date!", vbCritical, "INCORRECT RANGE"
        Exit Sub
    End if
    
    startDate = Me.dpFrom.Value: endDate = Me.dpTo.Value
    
    ' OPEN CONNECTION
    Set conn = New ADODB.Connection         
    conn.Open "DRIVER={SQL Server};server=servername;database=databasename;UID=username;PWD=password;"
    
    ' OPEN/DEFINE COMMAND OBJECT
    Set cmd = New ADODB.Command     
    With cmd
        .ActiveConnection = conn
        .CommandText = "myStoredProc"
        .CommandType = adCmdStoredProc
    
        ' BIND PARAMETERS
        .Parameters.Append .CreateParameter("@InvLineEntryDateBegin", adDate, adParamInput, 0, startDate)
        .Parameters.Append .CreateParameter("@InvLineEntryDateEnd", adDate, adParamInput, 0, endDate)
    En With
    
    ' BIND RESULTS TO RECORDSET
    Set rst = cmd.Execute
    ...
    
    0 讨论(0)
提交回复
热议问题