Public Sub WriteDataToSheet()
Dim oCn As New ADODB.Connection
Dim oRs As New ADODB.Recordset
Dim ws As Worksheet
Dim xlApp As Excel.Application
Dim arrRecords() As Variant 'Create an array to load the record set
Dim lRecNo As Long
' Open connection to the database
oCn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & sDBFile & ";"
With oRs
.Open SQL_Products, oCn, adOpenStatic, adLockReadOnly
arrRecords() = .GetRows()
.Close
Set oRs = Nothing
End With
oCn.Close
Set oCn = Nothing
' Note that the connection is already closed before the process starts
'do manipulations
For lRecNo = 0 To UBound(arrRecords, 2)
' MyNz function on field PRODescription, this is field(2) in ADO and add PRODCode (field(4)
arrRecords(2, lRecNo) = [B]fnMyNz[/B](arrRecords(2, lRecNo)) & arrRecords(4, lRecNo)
' add 10 pct to field PRODPrice, this is field(3) in ADO
arrRecords(3, lRecNo) = [B]fnAdd10pct[/B](arrRecords(3, lRecNo))
Next lRecNo
'write to sheet
Set ws = ActiveWorkbook.Sheets("Products")
Set xlApp = ThisWorkbook.Application
With ws
'Resize range, remember that the array is zero based, so add 1 to max
'Transpose array
.Cells(1, 1).Resize(UBound(arrRecords, 2) + 1, UBound(arrRecords, 1) + 1).Value = _
xlApp.WorksheetFunction.Transpose(arrRecords)
End With
Set ws = Nothing
Set xlApp = Nothing
Erase arrRecords()
End Sub