Access VBA Parameter in passthrough query to SQL Server

后端 未结 4 1596
情歌与酒
情歌与酒 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
    

提交回复
热议问题