Question : I am having a glitch with a form and combo boxes

I keep having a strange glitch ever since I  changed my form around. My original database was all window based. One of these windows was for filtering a pre-made report. The report is filtered by 3 combo boxes. The first populates the second, which in turn populates the third and applies the filter.

My new setup is more user friendly and I have only a single form and all the other forms are now opened s subforms within the main form. Well ever since I changed the design and edited some of the paths for the codes, my combo boxes reset themselves after you select from the third and final box. For instance, if you first navigate to the report generator form and select the report, what to filter, and the filter it will apply the filter and then clear all the boxes. Then, if you try to apply a different filter, after selecting the 3rd item the 3 combo boxes reset to the last choices you make. It will continue to do this all the time. Below is the original code before I redesigned it and the next is the code I currently use.
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:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
Option Explicit

Public Function FieldType(TableName As String, Fieldname As String) As Integer
    Dim strSQL As String, rs As DAO.Recordset
    
    'Assign variables
        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

Private Sub btnMainMenu_Click()
On Error GoTo btnMainMenu_Click_Err

    DoCmd.OpenForm "Main"
    DoCmd.Close acForm, "ReportGen"

btnMainMenu_Click_Exit:
    Exit Sub

btnMainMenu_Click_Err:
    MsgBox Error$
    Resume btnMainMenu_Click_Exit

End Sub

Private Sub btnMainMenu_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Me.btnMainMenu.Visible = False
Me.btnMainMenu2.Visible = True
End Sub

Private Sub btnMainMenu_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Me.btnMainMenu2.Visible = False
Me.btnMainMenu.Visible = True
End Sub

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 strFilter As String, strApply As String, strReport As String, strField As String
    Dim intFieldType As String
    'Assign variables
        strReport = Me.cboReport.Value
        strField = Me.cboField.Value
        strFilter = Me.cboFilter.Value
        intFieldType = FieldType(strReport, strField)
    
    'Apply selected filter to subReport
        Select Case intFieldType
            Case dbBigInt, dbBoolean, dbByte, dbCurrency, dbDecimal, dbDouble, dbFloat, dbInteger, dbLong, dbNumeric
                 strApply = "[" & strField & "] = " & strFilter
            Case dbChar, dbText, dbMemo
                strApply = "[" & strField & "] = """ & strFilter & """"
            Case dbDate, dbTime
                strApply = "[" & strField & "] = #" & strFilter & "#"
            Case Else
                MsgBox "Unexpected data type = " & intFieldType
                strApply = ""
        End Select

        Me.subReport.Report.Filter = strApply
        Me.subReport.Report.FilterOn = (strApply <> "")

End Sub
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:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
Option Explicit

Public Function FieldType(TableName As String, Fieldname As String) As Integer
    Dim strSQL As String, rs As DAO.Recordset
    
    'Assign variables
        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

Private Sub cboReport_Change()
Dim strReport As String, strOpenReport As String
    'Assign variables
strReport = "tbl" & Me.cboReport.Value
strOpenReport = "Report.rpt" & 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 = ""
        Forms!frmMain.subMain.Form.subReport.SourceObject = strOpenReport

End Sub

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

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

Private Sub cboFilter_Change()
    Dim strFilter As String, strApply As String, strField As String, strReport As String, intFieldType As String
    
    'Assign variables
        strReport = "tbl" & Me.cboReport.Value
        strField = Me.cboField.Value
        strFilter = Me.cboFilter.Value
        intFieldType = FieldType(strReport, strField)
    
    'Apply selected filter to subReport
        Select Case intFieldType
            Case dbBigInt, dbBoolean, dbByte, dbCurrency, dbDecimal, dbDouble, dbFloat, dbInteger, dbLong, dbNumeric
                 strApply = "[" & strField & "] = " & strFilter
            Case dbChar, dbText, dbMemo
                strApply = "[" & strField & "] = """ & strFilter & """"
            Case dbDate, dbTime
                strApply = "[" & strField & "] = #" & strFilter & "#"
            Case Else
                MsgBox "Unexpected data type = " & intFieldType
                strApply = ""
        End Select
        Forms!frmMain.subMain.Form.subReport.Report.Filter = strApply
        Forms!frmMain.subMain.Form.subReport.Report.FilterOn = (strApply <> "")
End Sub

Private Sub Form_Load()
Forms!frmMain.lblSubHeader.Caption = "Report Generator"
End Sub

Answer : I am having a glitch with a form and combo boxes

1. What is the need for the Main form anyway?
You can dump it, and change all the references to it to:
    [subReport].Report.use this
2. Move your combobox codes to the "After_Update" events

I have attached a repaired sample.

Notes:
This "Report in a Form" functionality is new in Acc2010, it will throw errors like the one I got,
Even if opened in Acc07 (which is supposedly compatible)
It will not work at all in older versions.

If you filter, then open your report from a *separate* menu, (the standard way this is done, for full compatibility) this will all work fine.
K.I.S.S
;-)

In other words, I tend to stay away from using new "Gee Whiz" functionality until I understand fully how it work.

Finally, this system may end up being difficult to manage (one menu, multiple reports)
As you may be requested to expand for more fields, and more complex filtering,
Again, this is not the standard way of doing this.
But I will concede that you are the boss in this case.
;-)

But yes, this was an interesting exercise.

;-)

JeffCoachman


Random Solutions  
 
programming4us programming4us