简体   繁体   中英

Change SQL Server Pass Through Query Variable

So I have a SQL Server pass through query in MS-Access that I created with the query design. I just double click it and it runs and opens up in datasheet view. Then I can export it. The query looks like so:

DECLARE @acyr AS varchar(4);
 SELECT @acyr = '2018'

SELECT *
FROM Table
WHERE year = @acyr

I also have a form with a listBox labeled acyrList where the user picks from 2017, 2018, 2019. I would like for acyrList.Value to be passed to the @acyr in the pass through query and return the records for that year.

How can I achieve this ?

You can rebuild the SQL in the Click event for the listbox and then assign that SQL to the pass through query. Something like:

Private Sub acyrList_Click()
  Dim strSQL As String
  strSQL = "DECLARE @acyr AS varchar(4); " & _
           "SELECT @acyr = '" & acyrList.value & "'; " & _
           "SELECT * From Table WHERE year = @acyr;"

  'MsgBox strSQL
  CurrentDb.QueryDefs("passthrough_query_name").sql = strSQL
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM