[英]Multiselect listbox in Access 2010
So I have this search form that filters projects based on the designer type. 因此,我有此搜索表单,可根据设计者类型过滤项目。 The designer can be internal, external, or combination. 设计人员可以是内部,外部或组合的。 It was originally done using a combobox. 最初是使用组合框完成的。 I was asked to make it so that the user can filter by multiple options, so I changed it to a listbox with the idea of using the multiselect option. 我被要求这样做,以便用户可以按多个选项进行过滤,因此我将其更改为一个列表框,以使用multiselect选项。
The listbox works fine, until I set the multiselct option to anything besides none. 列表框工作正常,直到我将multiselct选项设置为除无选项以外的任何选项。 Then, all the projects are returned instead of the ones that are selected. 然后,将返回所有项目,而不是所选择的项目。
I'm not very experienced in Access or VBA so any assistance would be greatly appreciated. 我对Access或VBA的经验不是很丰富,因此将不胜感激。 The following is the code for this section. 以下是本节的代码。
Private Sub toDesignerExcel_Click()
Dim db As DAO.Database
Dim rs1, rs2 As DAO.Recordset
Dim sSQL1, sSQL2, SourceExcel, FileName, Path As String
Set db = CurrentDb
sSQL1 = "Select ExcelPath from tblBackendFiles where Setting = 'ExcelDesignerParameters'"
sSQL2 = "Select Setting from tblBackendFiles where Code = 'SourceExcel'"
Set rs1 = db.OpenRecordset(sSQL1)
FileName = Nz(rs1!ExcelPath, "")
Set rs2 = db.OpenRecordset(sSQL2)
SourceExcel = Nz(rs2!Setting, "")
Path = SourceExcel + "\" + FileName
rs1.Close
rs2.Close
db.Close
Set rs1 = Nothing
Set rs2 = Nothing
Set db = Nothing
Shell "C:\WINDOWS\explorer.exe """ & Path, vbNormalFocus
End Sub
EDIT: And also, 编辑:而且,
If Forms(formName).txtDesigner <> "" And Not IsNull(Forms(formName).txtDesigner) Then
If selEngConditions <> "" Then
selEngConditions = selEngConditions & " AND "
End If
selEngConditions = selEngConditions & "[Activity].[GWPDesigner] = '" & Forms(formName).txtDesigner & "'"
End If
I would do something like this : 我会做这样的事情:
Private Sub toDesignerExcel_Click()
Dim db As DAO.Database
Dim rs1, rs2 As DAO.Recordset
Dim sSQL1, sSQL2, SourceExcel, FileName, Path As String
Set db = CurrentDb
sSQL1 = "Select ExcelPath from tblBackendFiles where Setting = 'ExcelDesignerParameters' And [Tblbackendfiles] = '"
sSQL2 = "Select Setting from tblBackendFiles where Code = 'SourceExcel' And [Tblbackendfiles] = '"
For Each it In Me.ListBoxName.ItemsSelected
Set rs1 = db.OpenRecordset(sSQL1 & it & "'")
FileName = Nz(rs1!ExcelPath, "")
Set rs2 = db.OpenRecordset(sSQL2 & it & "'")
SourceExcel = Nz(rs2!Setting, "")
Path = SourceExcel + "\" + FileName
Shell "C:\WINDOWS\explorer.exe """ & Path, vbNormalFocus
rs1.Close
rs2.Close
Set rs1 = Nothing
Set rs2 = Nothing
Next it
db.Close
Set db = Nothing
End Sub
Where you basically iterate over all the selected items in the listbox. 您基本上在其中迭代列表框中所有选定项目的位置。 This adds each selected item from the listbox in the where
clause of the query. 这将从查询的where
子句中的列表框中添加每个选定的项目。
You can also try concatenating all the values and changing the query to something like : 您也可以尝试连接所有值并将查询更改为:
And [Tblbackendfiles] In (" & comma_separated_list & ")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.