Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
796 views
in Technique[技术] by (71.8m points)

vba - 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.

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.

Thanks Justin

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use

WHERE LocationID IN (" & listofvalues & ");"

The list can be obtained like so:

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...