简体   繁体   English

在IN语句中使用文本框值

[英]Using textbox values in IN statement

I'm trying to get this to work? 我想让它工作? How can I set a textbox to be used in an IN() statement? 如何设置要在IN()语句中使用的文本框? I would like users to be able to list out countries they'd like to query on 我希望用户能够列出他们要查询的国家/地区

SELECT *
FROM Table
WHERE CountryID IN (forms!frmImport!txtCountries)

You can't just put the name of the textbox directly into the SQL string. 您不能只将文本框的名称直接放入SQL字符串中。
Instead, you need to use the content of the textbox to build a SQL string like this: 相反,您需要使用文本框的内容来构建如下的SQL字符串:

SELECT * FROM Table WHERE CountryID IN (1,2,3)

If your users enter a comma-separated list of CountryIDs into the textbox, you can build the SQL like this: 如果您的用户在文本框中输入以逗号分隔的CountryID列表,则可以按以下方式构建SQL:

Dim SQL As String

SQL = "SELECT * FROM Table WHERE CountryID IN (" & Forms!frmImport!txtCountries & ")"

But I wouldn't do it this way because it's simple, but prone to input errors and SQL injection. 但是我不会这样做,因为它很简单,但是容易出现输入错误和SQL注入。

A better way would be to use a list box, set the Multi Select property (so that multiple entries can be selected) and fill it with a list of all available countries. 更好的方法是使用列表框,设置“ Multi Select属性(以便可以选择多个条目) ,然后用所有可用国家/地区的列表填充它。

Then, the user can select the ones that he wants, and you can build the SQL string using the selected items from the listbox: 然后,用户可以选择所需的内容,然后可以使用列表框中的选定项来构建SQL字符串:

Dim ListItem As Variant
Dim CountryList As String
Dim SQL As String

For Each ListItem In Forms!frmImport!lstCountries.ItemsSelected
    If CountryList > "" Then
        CountryList = CountryList & ","
    End If
    CountryList = CountryList & ListItem
Next

SQL = "SELECT * FROM Table WHERE CountryID IN (" & CountryList & ")"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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