0

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 ?

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.