Access 2010中的多选列表框

时间:2014-06-18 00:32:24

标签: vba ms-access listbox access-vba ms-access-2010

所以我有这个搜索表单,根据设计器类型过滤项目。设计师可以是内部,外部或组合。它最初是使用组合框完成的。我被要求制作它以便用户可以通过多个选项进行过滤,因此我将其更改为列表框,并考虑使用多选选项。

列表框工作正常,直到我将multiselct选项设置为除none之外的任何内容。然后,返回所有项目而不是所选项目。

我在Access或VBA方面不是很有经验,所以非常感谢任何帮助。以下是本节的代码。

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

编辑: 而且,

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

1 个答案:

答案 0 :(得分:1)

我会做这样的事情:

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子句中添加列表框中的每个选定项。

您还可以尝试连接所有值并将查询更改为:

And [Tblbackendfiles] In (" & comma_separated_list & ")