How to use parameterized query in Excel using column as parameter?

泪湿孤枕 提交于 2019-12-17 16:39:15

问题


I am trying to develop a spreadsheet that can locate corresponding records in an external data source. So, let's say I have Column A with a list of identity values. I want to develop Column B, which perhaps shows a count of rows in the table with that value. Something like:

A              B
758348    "=SELECT COUNT(*) FROM MYTABLE WHERE IDVALUE=$A$1"
173483    "=SELECT COUNT(*) FROM MYTABLE WHERE IDVALUE=$A$2"

... and so on. So, I thought I would use a parameterized query (where IDVALUE=?), but that prompts me to input the parameter value, not use the value from the cell to the left. Is there any way I can do this?


回答1:


I would use a parameterized query (where IDVALUE=?), but that prompts me to input the parameter value

From what you mentioned I understand you are using MS Query and you need to follow the following steps

Steps to make a parameterized query in Excel use a cell value as parameter

  1. First go to the Data tab in Excel and Select MS Query under External Data Sources

  1. A pop up appears asking you to choose data source. Select the data source or add a new data source and select it. Make sure to uncheck Use the query wizard to create or edit queries it is not very useful in most cases

  1. Then a pop up appears asking you to add table select any table for now. It is better to select a table with less rows.

  1. Then a Query window appears with the table you have selected and its data

  1. Then click on the view menu and uncheck tables. This removes the graphically representation in query window and makes it easier to alter you sql query

  1. Click on the SQL button below the format menu in query window. A pop up comes where you can edit the query as you wish. Make sure to add question mark(s) for the parameter(s) you want.

  1. Then a pop up appears ask you for parameter value(s) enter some valid value for now

  1. Then the query window shows the result of query based on the parameter entered

  1. Click on the exit button below the view menu in query window. Then the query window closes and Import Data pop up comes asking where in excel to display the result. Choose accordingly

  1. Then click on Properties button on left before you click OK. A new pop up appears with the default usage tab selected.

  1. Make any changes needed in this tab and click on Definition tab. Click on Parameters button in bottom of the pop up next to edit query button

  1. Parameters pop up comes displaying the parameters used in the query. Here you must select the Get the value from following cell radio button to select the value of cell as the parameter for the query. Click OK and you should be done.


This method is for people with no VBA experience. If you know VBA then refer to this answer to achieve the something similar using VBA.




回答2:


So to do this I would create a UDF and have the backend of that function connecting to your access database. Something like this:

Public Function countMyTable(IDValue As Double) As Long
'Requires:// Microsoft Access 16.0 Object Library
'Requires:// Microsoft Office 16.0 Access database engine Object Library
Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = Access.DBEngine.Workspaces(0).OpenDatabase(DBFilePath, False, False)
Set rs = db.OpenRecordset("SELECT COUNT(1) FROM MyTable WHERE IDValue = " & IDValue, dbOpenSnapshot)
rs.MoveLast
countMyTable = rs(0)
db.close
End Function

Hope this helps, TheSilkCode



来源:https://stackoverflow.com/questions/26413092/how-to-use-parameterized-query-in-excel-using-column-as-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!