Limit # of records returned based on a form control

家住魔仙堡 提交于 2019-12-23 02:17:15

问题


Is there a way to limit the number of records returned in a query based on the user input in a text box? I know that I could use the Top Values if the number of records were constant, but this number will change. I tried:

SELECT TOP[Forms![frm_GenerateMailout]![MailoutSize]]

..but that didn't work.


回答1:


It depends how, and where, you are using the SQL statement, but if you construct it as a string you need to concatenate it correctly:

"SELECT TOP " & Forms!frm_GenerateMailout!MailoutSize & ".. etc."



回答2:


"Is there a way to limit the number of records returned in a query based on the user input in a text box?"

Access SQL does not accept a parameter of any kind with SELECT TOP.

Unfortunately, when you try, the error message (#3141) does not identify the problem clearly:

"The SELECT statement includes a reserved word or an argument name that is misspelled or missing, or the punctuation is incorrect."

If you can build your SELECT with VBA, you can work around that easily enough by building the number into the string as Andy described. Or you can use Replace() to substitute your MailoutSize value for a place-holder in a pre-built SQL statement.

So starting with ...

strTemplateSql = "SELECT TOP 100 PERCENT tblFoo.*" & vbCrLf & _
   "FROM tblFoo;"

You can do this ...

strSelect = Replace(strTemplateSql, "100 PERCENT", _
    Forms!frm_GenerateMailout!MailoutSize)

There are other possibilities, too. If neither of our suggestions is appropriate in your situation, tell us about where and how you need to use the query.



来源:https://stackoverflow.com/questions/18434063/limit-of-records-returned-based-on-a-form-control

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