Question : Need assistance with filtering a report from a form

I have a question about filtering reports from a form. I currently have a form that includes 3 combo boxes and a subreport. The first combo box selects the table to view and populates the second combo box. The second allows you to select which field to filter, and also populates the third with values from the given table / field. The third combo box lets you select an item and then filters the report based on all 3 combo boxes with concatenation. (table-field-record) When I finish the third box it will filter out everything in the report, leaving only a blank record. Can anyone help me with this? Here is the code I used for the form to see how I have it done:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
Option Explicit

Private Sub cboReport_Change()
    Dim strReport As String
    
    'Assign variables
        strReport = Me.cboReport.Value
    
    'Populates cboField with options based on cboReport.
        Me.cboField.RowSource = strReport
        Me.cboField.RowSourceType = "Field List"
        Me.cboField.Value = ""
        Me.cboFilter.Value = ""
        Me.Refresh
    
    'This statement finds the selected item in the combo box and opens the respective
    'report in the sub Report area.
        If IsNull(cboReport.Value) Then
            Me.subReport.SourceObject = ""
        Else
            If cboReport.Value = "Employee" Then
                Me.subReport.SourceObject = "Report.rptEmployee"
            Else
                Me.subReport.SourceObject = "Report.rptInventory"
            End If
        End If
    
End Sub

Private Sub cboField_Change()
    Dim strReport As String, strField As String, strFilter As String

    'Assign variables
        strReport = Me.cboReport.Value
        strField = Me.cboField.Value
        strFilter = "SELECT distinct " & "[" & strReport & "]" & "." & "[" & strField & "]" & " FROM " & "[" & strReport & "]" & ";"
        
    'Populates cboFilter with options based on cboReport and cboField.
        Me.cboFilter.RowSourceType = "Table/Query"
        Me.cboFilter.RowSource = strFilter
        Me.cboFilter.Value = ""
        Me.Refresh
        
End Sub

Private Sub cboFilter_Change()
    Dim strField As String, strFilter As String, strApply As String
    
    'Assign variables
        strField = Me.cboField.Value
        strFilter = Me.cboFilter.Value
        strApply = "[" & strField & "] = " & """ & strFilter & """
    
    'Apply selected filter to subReport
        Me.subReport.Report.Filter = strApply
        Me.subReport.Report.FilterOn = True
        Me.Refresh
        
End Sub

Answer : Need assistance with filtering a report from a form

Assuming that all the fields that the user could select from in each of these tables are text, try:

strApply = "[" & strField & "] = """ & strFilter & """"

If all of the fields that could be selected are not text, then you are going to have to figure out a way to determine the datatype of the field before creating strApply.  You could use a function like the following:

Public Function FieldType(TableName As String, Fieldname As String) As Integer

    Dim strSQL As String
    Dim rs As DAO.Recordset
   
    strSQL = "SELECT [" & Fieldname & "] FROM [" & TableName & "] WHERE False"
    strSQL = Replace(Replace(strSQL, "[[", "["), "]]", "]")
   
    Set rs = CurrentDb.OpenRecordset(strSQL, , dbfailonerror)
   
    FieldType = rs.Fields(0).Type
    rs.Close
    Set rs = Nothing
   
End Function

And then do something like:

intFieldType = FieldType(strReport, strField)
SELECT Case intFieldType
    Case dbBigInt, dbBoolean, dbByte, cbCurrency, dbDecimal, dbDouble, dbFload, dbInteger, dbLong, dbNumeric
        strApply = "[" & strField & "] = " & strFilter
    Case dbChar, cbText, dbMemo
        strApply = "[" & strField & "] = """ & strFilter & """"
    Case dbDate, dbTime
        strApply = "[" & strField & "] = #" & strFilter & "#"
    Case Else
        msgbox "Unexpected data type = " & intFieldType
        'This one should include all values
        strApply = ""
End Select

me.subReport.Report.Filter = strApply
me.subReport.Report.FilterOn = (strApply <> "")
Random Solutions  
 
programming4us programming4us