MS Access 2007 - 循环浏览列表框中的值以获取 SQL 语句的 ID [英] MS Access 2007 - Cycling through values in a list box to grab id's for a SQL statement
问题描述
假设我有两个表,一个用于事务,另一个表的主键是第一个表中的外键,这种关系只是将位置与事务相关联.
Lets say I have two tables, one for transactions, and another table who's primary key is the foreign key in the first table, and this relationship simply associates locations to transactions.
我有一个带有列表框的表单,其中显示了所有可能的位置,以便用户可以打开一些仅与给定位置相关的仪表板表单.所以我知道如何将数据从选择传递到仪表板,但是我现在希望用户能够从第一个列表中选择多个位置.
I have a form with a list box that shows all the potential locations, so that the user can open some dashboard forms that only pertain to a given location. So I know how to pass the data from the selection to the dashboard, however I would now like the user to have the capability to select multiple locations from the first list.
所以如果我使用 SQL 语句,WHERE 子句就像
so if I use a SQL statement the WHERE clause is like
.... WHERE LocationID = " & me.lstLocations.value & ";"
但是我如何将这种类型的方法等同于选择多项选择?我确信有某种类型的循环可以让我逃脱.
but how would I equate this type of method to selecting multiple choices? I am sure there is some type of loop that escapes me.
谢谢贾斯汀
推荐答案
您可以使用
WHERE LocationID IN (" & listofvalues & ");"
可以像这样获得列表:
For Each itm In Me.ListBox.ItemsSelected
listofvalues = listofvalues & "," & Me.ListBox.Column(0, itm)
Next
listofvalues = Mid(listofvalues,2)
这是一个数字列表,字符串列表需要引号.
This is for a numeric list, a list of strings needs quotes.
这篇关于MS Access 2007 - 循环浏览列表框中的值以获取 SQL 语句的 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!