简体   繁体   中英

Multi-Select Listbox in Access 2010

have a search form in Access 2010 that filters projects based on certain criteria and opens them in another form. One of the criteria is an unbound multi-select list box, txtArea. The data is stored in a table, ProjectActivity. There is another table called LookupArea that divides ProjectActivity by area as North, South, or Both (ie. Having parts in north and south area).

I also have a query, qrySelectArea:

SELECT DISTINCT Area FROM LookupArea;

This is the code I'm trying to work with:

Private Sub OpenReport_Click()
   Dim db As DAO.Database
   Dim qdf As DAO.QueryDef
   Dim varItem As Variant
   Dim strCriteria As String
   Dim strSQL As String
   Set db = CurrentDb()
   Set qdf = db.QueryDefs("qryLookupArea")
   If Me!txtArea.ItemsSelected.Count > 0 Then
      For Each varItem In Me!txtArea.ItemsSelected
         strCriteria = strCriteria & "ProjectActivity.Area = " & Chr(34) _
                   & Me!.ItemData(varItem) & Chr(34) & "OR "
      Next varItem
      strCriteria = Left(strCriteria, Len(strCriteria) - 3)
   Else
      strCriteria = "ProjectActivity.Area Like '*'"
   End If
   strSQL = "SELECT * FROM ProjectActivity " & _
        "WHERE " & strCriteria & ";"
   qdf.SQL = strSQL
   DoCmd.OpenQuery "qryLookupArea"

DoCmd.Close acForm, "Search Form"
DoCmd.Open "ReportForm", acNormal
DoCmd.Close acQuery, "qryLookupArea"

End Sub

When I click the OpenReport button, the query runs (I can see it in the background while the report form opens) but the report form shows all the projects regardless of how I filter it. I'm not super big in VBA so I'd really appreciate any suggestions.

最简单的方法是,当您打开表单时,需要按如下所示通过选择条件(只需确保您的“ strCriteria”是有效的语法(即“ ProjectActivity.Area Like'*'”)):

DoCmd.OpenForm "ReportForm", acNormal, ,  strCriteria

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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