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