0
votes

In my GUI, I have several ways to filter a database. Due to my lack of knowledge, my VBA programming has exploded with nested IF statements. I am getting better at using ACCESS now, and would like to find a more succinct way to perform multiple filters. My form is continuous.

Is there a simple way to do the following task (I made a toy model example):

I have a combo box SITE where I can filter by work sites A, B, C. After filtering by SITE, I have three check boxes where the user can then filter by item number 1-10, 11-20, 21-30, depending on what the user selects.

Is there a way to append multiple filters (or filter filtered data)? For example, filter by SITE A, then filter A by item number 1-10?

Currently, for EACH check box, I then have an IF statement for each site. Which I then use Form.Filter = . . . And . . . and Form.FilterOn = True.

Can I utilize SQL on the property sheet to filter as opposed to using the VBA?

3
Yes you can, but you did not post any details about your tables (columns, relationships etc).forpas
You could do it as a subform with parent and child being, the combo "site" to site in the table, then alter the record source based on the clicking of the option buttons. SQL is a good option, but cant see any data structure, so not sure how to advise on that one. I believe that you can say [a]=10 AND [b]='nathan' for example in the filters, not sure though.Nathan_Sav
I don't really understand what you are asking, sample code would help. This may be of interest: allenbrowne.com/ser-62.htmlAndre

3 Answers

2
votes

What I do for these types of filters is to construct a SQL statement whenever one of the filter controls is changed. All of them reference the same subroutine to save on code duplication.

What you do with this SQL statement depends on what you're trying to do. Access is pretty versatile with it; use it as a RecordSource, straight execute it, and use the results for something else, even just printing it to a label.

To try to modularize the process, here's an example of how I do it:

Dim str As String
str = "SELECT * FROM " & Me.cListBoxRowSource

Me.Field1.SetFocus
If Me.Field1.Text <> "" Then
    str = AppendNextFilter(str)
    str = str & " SQLField1 LIKE '*" & Me.Field1.Text & "*'"
End If

Me.Field2.SetFocus
If Me.Field2.Text <> "" Then
    str = AppendNextFilter(str)
    str = str & " SQLField2 LIKE '*" & Me.Field2.Text & "*'"
End If

Me.Field3.SetFocus
If Me.Field3.Text <> "" Then
    str = AppendNextFilter(str)
    str = str & " SQLField3 LIKE '*" & Me.Field3.Text & "*'"
End If

Me.cListBox.RowSource = str

Variables edited to protect the guilty.

My AppendNextFilter method just checks to see if WHERE exists in the SQL statement already. If it does, append AND. Otherwise, append WHERE.

1
votes

Making quite a few assumptions (since you left out a lot of info in your question), you can do something like this:

Dim sSql as String

sSql = "Select * from MyTable"

Set W = Me.cboSite.Value

sSql = sSql & " WHERE MySite = " & W & ""

Set X = Me.Chk1
Set Y = Me.Chk2
Set Z = Me.Chk3

If X = True Then
  sSql = sSql & " And MyItem between 1 and 10"
If Y = True Then
  sSql = sSql & " And MyItem between 11 and 20"
If Z = True Then
  sSql = sSql & " And MyItem between 21 and 30"
End If

DoCmd.ExecuteSQL sSql

Again, this is entirely "air code", unchecked and probably needing some edits as I haven't touched Access in some time and my VBA is likely rusty. But it should put you on the right track.

0
votes

The way i use combobox filtering in access is first I design a Query that contains all the data to be filtered. The Query must contain fields to be used for filtering. QueryAllData => "SELECT Table.Site, Table.ItemNumber, FROM Table;" Then make a copy of the query and Name it QueryFilteredData and Design the report to display the data using QueryFilteredData. Then create a form with a Site ComboBox, ItemNumber Combo Box, and Sub Report Object and Assign SourceObject the Report Name. Use Value List as the combo box Row Source type and type in the values for Row Source to get it working. To get the report to update I always unassign the SubReport.SourceOject update the QueryFilteredData and then Reassign the SubReport.SourceObject

Combobox_Site_AfterUpdate()
    Combobox_ItemNumber_AfterUpdate
End Sub

Combobox_ItemNumber_AfterUpdate()
    Select Case Combobox_ItemNumber.value 
      Case Is = "1-10"
         Store_Filters 1,10
      Case Is = "11-20"
         Store_Filters 11,20
      Case Is = "21-30"
         Store_Filters 21,30
      Case Else
         Store_Filters 1,10
End Sub    

Private Sub Store_Filters(Lowest as integer, Highest as integer)
Dim SRpt_Recset As Object
Dim Temp_Query As Variant
Dim Temp_SourceObject as Variant
Temp_SourceObject = SubReport.SourceObject
SubReport.SourceObject =""
Set SRpt_Recset = CurrentDb.QueryDefs("QueryFilteredData")
Filter_Combo_Box1 = " ((QueryAllData.[Sites])= " & Chr(39) & Combo_Box1 & Chr(39) & ") " 
Filter_Combo_Box2 = (Filter_Combo_Box1 AND (QueryAllData.ItemNumber <= Highest)) OR (Filter_Combo_Box1 AND (QueryAllData.ItemNumber >= Lowest));"
Temp_Query = " SELECT " & Query_Name & ".* " & _
                                        "FROM " & Query_Name & " " & _
                                        "WHERE (" & Filter_Combo_Box2 & ") ORDER BY [Field_Name_For_Sorting];"
SRpt_Recset.SQL = Temp_Query
'Debug.print Temp_Query
SubReport.SourceObject = Temp_SourceObject                                       
End Sub

After the Combo Boxes Work if the Data is going to Change like Site and Item Number then you might want to change the Row Source of the combo boxes to Use a Query that uses Select Distinct Site From QueryAllData. I don't know if Filter_Combo_Box2 step so it may need some correction. Hope this helps.