'''EXCEL CODE''''
'''SET REFERENCE TO Active X DAO Library
Option Explicit
Public Type RowColumn 'function requires a user defined type
row As String
col As String
End Type
Public Function GetRowColumn(strRowColumn) As RowColumn
Dim lngCount As Long
Dim lngChar As String
'function to get cell references out of column names where the column names
' are equivalent to A1 style cell references (Note:R1C1 references won't work with this)
For lngCount = 1 To Len(strRowColumn)
lngChar = Asc(Mid(strRowColumn, lngCount, 1))
If lngChar >= Asc("0") And lngChar <= Asc("9") Then
Exit For
End If
Next lngCount
'this extracts the column and row from the Field Name in the recordset.
GetRowColumn.col = Left(strRowColumn, lngCount - 1)
GetRowColumn.row = Right(strRowColumn, Len(strRowColumn) - lngCount + 1)
End Function
Sub GenerateAllReports()
'called from button click on Sheet1
'simply a sample. The procedure to pass a variable through the Generate report can
'be called from a form, and excel file, and access file with minor changes, or another
'program
'dimension variables
Dim rngNames As Range, c As Range
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.DisplayAlerts = False
On Error Goto EarlyOut
With ActiveSheet
Set rngNames = .Range("A2:A" & .Cells(.Rows.Count, 1).End(xlUp).row)
For Each c In rngNames
GenerateReport c
Next c
End With
Set rngNames = Nothing
Application.ScreenUpdating = True
Application.EnableEvents = True
MsgBox "Reports Completed!"
Exit Sub
EarlyOut:
MsgBox "Error: " & Err.Number & " " & Err.Description
Application.DisplayAlerts = True
Application.ScreenUpdating = True
Application.EnableEvents = True
Set rngNames = Nothing
End Sub
Public Sub GenerateReport(ByVal strCriteria As String)
'Dimension Variables NOTE: the criteria for this is passed in as a string,
' and can be passed any number of ways.
Dim objExcelApp As Excel.Application
Dim objWorkbook As Excel.Workbook
Dim objWorksheet As Excel.Worksheet
Dim objCN As ADODB.Connection
Dim objRS As ADODB.Recordset
Dim objField As ADODB.Field
Dim strSQL As String
Dim udtRowColumn As RowColumn
Dim strPath As String
Dim strConn As String
Dim strFileName As String
strPath = ThisWorkbook.Path 'sets the path of the file for use later
Set objExcelApp = New Excel.Application 'sets object as new instance of excel
Set objWorkbook = objExcelApp.Workbooks.Open(strPath & "\ReportTemplate\" & "Template.xls")
'above opens the file in the new instance
Set objWorksheet = objWorkbook.ActiveSheet 'sets the destination sheet
Set objCN = New ADODB.Connection 'sets a new connection in memory
'string connection here
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strPath & "\" & "GetColumnRow.mdb;Persist Security Info=False"
objCN.ConnectionString = strConn 'set the connection
objCN.Open 'open the connection
'remember to use the "column Name" to identify criteria field...
strSQL = "SELECT * FROM ReportQuery WHERE [A2] =" & Chr(34) & strCriteria & Chr(34)
'remember to format the criteria string properly to match the record source format..
'SQL statement. if already in Access it could be DoCmd.RunSQL
Set objRS = New ADODB.Recordset
objRS.Open strSQL, objCN, adOpenForwardOnly, adLockReadOnly, adCmdText
On Error Goto ErrHandler
For Each objField In objRS.Fields 'for each column in result set
If IsNumeric(Right(objField.Name, 1)) Then
udtRowColumn = GetRowColumn(objField.Name)
objWorksheet.Cells(udtRowColumn.row, udtRowColumn.col) = objField.Value
End If
'gets the cell reference from the column/feild name of the result set
Next objField 'next field
objRS.Close 'close recordset
Set objRS = Nothing 'clear the recordset from memory
objCN.Close 'cease the connection
Set objCN = Nothing 'clear the object
'save the report to a specified path with date formatted string
strFileName = strPath & "\Reports\" & strCriteria & "_" & Format(Now(), "mmddyy") & ".xls"
objWorkbook.SaveAs strFileName
objWorkbook.Close
'clear the excel variable objects
Set objWorksheet = Nothing
Set objWorkbook = Nothing
Set objExcelApp = Nothing
Set objCN = Nothing
Set objRS = Nothing
Set objField = Nothing
Exit Sub
ErrHandler:
'to clear the hidden objWorkbook from memory...and resume
objWorkbook.Close
Set objWorksheet = Nothing
Set objWorkbook = Nothing
Set objExcelApp = Nothing
Resume Next
End Sub
''''MICROSOFT ACCESS CODE'''
''SET REFERENCE TO MICROSOFT EXCEL OBJECT LIBRARY
Option Compare Database
Option Explicit
Public Type RowColumn 'function requires a user defined type
row As String
col As String
End Type
Public Function GetRowColumn(strRowColumn) As RowColumn
Dim lngCount As Long
Dim lngChar As String
'function to get cell references out of column names where the column names
' are equivalent to A1 style cell references (Note:R1C1 references won't work with this)
For lngCount = 1 To Len(strRowColumn)
lngChar = Asc(Mid(strRowColumn, lngCount, 1))
If lngChar >= Asc("0") And lngChar <= Asc("9") Then
Exit For
End If
Next lngCount
'this extracts the column and row from the Field Name in the recordset.
GetRowColumn.col = Left(strRowColumn, lngCount - 1)
GetRowColumn.row = Right(strRowColumn, Len(strRowColumn) - lngCount + 1)
End Function
Public Sub GenerateReport(ByVal strCriteria As String)
'Dimension Variables NOTE: the criteria for this is passed in as a string,
' and can be passed any number of ways.
Dim objExcelApp As Excel.Application
Dim objWorkbook As Excel.Workbook
Dim objWorksheet As Excel.Worksheet
Dim ObjRS1 As Recordset
Dim objField As Field
Dim strSQL As String
Dim udtRowColumn As RowColumn
Dim strPath As String
Dim strConn As String
Dim strFileName As String
strPath = CurrentProject.Path 'sets the path of the file for use later
Set objExcelApp = New Excel.Application 'sets object as new instance of excel
Set objWorkbook = objExcelApp.Workbooks.Open(strPath & "\ReportTemplate\" & "Template.xls")
'above opens the file in the new instance
Set objWorksheet = objWorkbook.ActiveSheet 'sets the destination sheet
'remember to use the "column Name" to identify criteria field...
strSQL = "SELECT * FROM ReportQuery WHERE [A2] =" & Chr(34) & strCriteria & Chr(34)
'remember to format the criteria string properly to match the record source format..
'SQL statement. if already in Access it could be DoCmd.RunSQL
Set ObjRS1 = New Recordset
ObjRS1.Open strSQL, CurrentProject.Connection, adOpenForwardOnly, adLockReadOnly, adCmdText
On Error Goto ErrHandler
For Each objField In ObjRS1.Fields 'for each column in result set
If IsNumeric(Right(objField.Name, 1)) Then
udtRowColumn = GetRowColumn(objField.Name)
objWorksheet.Cells(udtRowColumn.row, udtRowColumn.col) = objField.Value
End If
'gets the cell reference from the column/feild name of the result set
Next objField 'next field
ObjRS1.Close 'close recordset
Set ObjRS1 = Nothing 'clear the recordset from memory
'save the report to a specified path with date formatted string
strFileName = strPath & "\Reports\" & strCriteria & "_" & Format(Now(), "mmddyy") & ".xls"
objWorkbook.SaveAs strFileName
objWorkbook.Close
'clear the excel variable objects
Set objWorksheet = Nothing
Set objWorkbook = Nothing
Set objExcelApp = Nothing
Exit Sub
ErrHandler: 'if it errors, the ExcelApp remains in the background, but this clears it and resumes the code
objWorkbook.Close
Set objWorksheet = Nothing
Set objWorkbook = Nothing
Set objExcelApp = Nothing
Err.Clear
Resume Next
End Sub
Sub GenerateAllReports()
Dim objRS As ADODB.Recordset, strSQL As String
strSQL = "Select Employee_Name from Employee Group By Employee_Name"
Set objRS = New Recordset
objRS.Open strSQL, CurrentProject.Connection, adOpenDynamic, adLockReadOnly, 1
Do While Not objRS.EOF
GenerateReport objRS![Employee_Name]
objRS.MoveNext
Loop
Set objRS = Nothing
MsgBox "Reports Completed!"
End Sub
|