Hi there
I have a report to create in Excel from scratch with data from Access, but its taking 9 minutes at the moment on a 466MHZ PC with 64MB RAM. I am using DLOOKUP to search for a value in one table, if its not found there, I DLOOKUP another table and if its still not found, I set the cell color to yellow in excel. Is it more efficient and faster to use .findfirst ? Any advice gratefully received.
My Code is below :
Private Sub s_RPT_Difference_CreateBodyDifference()
On Error GoTo err_s_RPT_Difference_CreateBodyDifference
'
' Create body in Excel spreadsheet
'
Dim xlApp As New Excel.Application, xlSheet As Excel.Worksheet
Dim RangeCurrent As Excel.Range, dbcurrent As Database
Dim rstWeekNumbers As Recordset, rstExcelRows As Recordset
Dim iColCount As Integer, iRowCount As Integer
' Open excel
Set xlApp = Excel.Application
xlApp.Workbooks.Open f_FILELOCATION_ReturnLocation("ANNUAL.XLS"
Set xlSheet = xlApp.Worksheets("Annual Diff"
Set RangeCurrent = xlSheet.Range("C5"
iColCount = 0
iRowCount = 0
' Open database
Set dbcurrent = CurrentDb()
Set rstWeekNumbers = dbcurrent.OpenRecordset("SELECT * FROM WEEK_NUMBERS WHERE CURRENT_YEAR = '2000' ORDER BY WEEK_END_DATE ASC;"
Set rstExcelRows = dbcurrent.OpenRecordset("SELECT * FROM EXCEL_ROWS ORDER BY BRANCH_NUMBER;", dbOpenDynaset)
' Loop through recordsets
With rstWeekNumbers
If Not .EOF Then .MoveLast
If Not .BOF Then .MoveFirst
If .RecordCount > 0 Then
' Copy over records
Do While Not .EOF
If Not rstExcelRows.EOF Then rstExcelRows.MoveLast
If Not rstExcelRows.BOF Then rstExcelRows.MoveFirst
' Write data
If rstExcelRows.RecordCount > 0 Then
Do While Not rstExcelRows.EOF
If IsNull(DLookup("BALANCE", "DIFFERENCES", "BRANCH_NUMBER = " & rstExcelRows!BRANCH_NUMBER & " AND WEEK_ENDING_DATE = #" & Format(!WEEK_END_DATE, "mm/dd/yyyy" & "#") Then
' Value not found in difference table, lookup difference derived.
If IsNull(DLookup("BALANCE", "DIFFERENCES_DERIVED", "BRANCH_NUMBER = " & rstExcelRows!BRANCH_NUMBER & " AND WEEK_ENDING_DATE = #" & Format(!WEEK_END_DATE, "mm/dd/yyyy" & "#") Then
' Value not found in difference derived table, paint cell yellow
RangeCurrent.Offset(iRowCount, iColCount).Interior.ColorIndex = 6
RangeCurrent.Offset(iRowCount, iColCount).Interior.Pattern = xlSolid
Else
' Value found in difference derived table, write to spreadshet.
RangeCurrent.Offset(iRowCount, iColCount).Value = DLookup("BALANCE", "DIFFERENCES_DERIVED", "BRANCH_NUMBER = " & rstExcelRows!BRANCH_NUMBER & " AND WEEK_ENDING_DATE = #" & Format(!WEEK_END_DATE, "mm/dd/yyyy" & "#"
End If
Else
' Value found in difference table, write to excel spreadsheet.
RangeCurrent.Offset(iRowCount, iColCount).Value = DLookup("BALANCE", "DIFFERENCES", "BRANCH_NUMBER = " & rstExcelRows!BRANCH_NUMBER & " AND WEEK_ENDING_DATE = #" & Format(!WEEK_END_DATE, "mm/dd/yyyy" & "#"
End If
iColCount = iColCount + 1
rstExcelRows.MoveNext
Loop
iColCount = 0
End If
iRowCount = iRowCount + 1
.MoveNext
Loop
End If
End With
' Close database objects
rstWeekNumbers.Close
rstExcelRows.Close
dbcurrent.Close
' Save excel
xlApp.ActiveWorkbook.Save
xlApp.Quit
' Destroy objects
Set rstExcelRows = Nothing
Set rstWeekNumbers = Nothing
Set dbcurrent = Nothing
Set RangeCurrent = Nothing
Set xlSheet = Nothing
Set xlApp = Nothing
exit_s_RPT_Difference_CreateBodyDifference:
Exit Sub
err_s_RPT_Difference_CreateBodyDifference:
Beep
MsgBox "An error has occured. Please contact IT with details below" & vbNewLine & _
vbNewLine & Err.Number & vbNewLine & Err.Description
Resume exit_s_RPT_Difference_CreateBodyDifference
End Sub
Missy Ed
Looking to exchange ideas and tips on VB and MS Access development as well as office 97 development.
I have a report to create in Excel from scratch with data from Access, but its taking 9 minutes at the moment on a 466MHZ PC with 64MB RAM. I am using DLOOKUP to search for a value in one table, if its not found there, I DLOOKUP another table and if its still not found, I set the cell color to yellow in excel. Is it more efficient and faster to use .findfirst ? Any advice gratefully received.
My Code is below :
Private Sub s_RPT_Difference_CreateBodyDifference()
On Error GoTo err_s_RPT_Difference_CreateBodyDifference
'
' Create body in Excel spreadsheet
'
Dim xlApp As New Excel.Application, xlSheet As Excel.Worksheet
Dim RangeCurrent As Excel.Range, dbcurrent As Database
Dim rstWeekNumbers As Recordset, rstExcelRows As Recordset
Dim iColCount As Integer, iRowCount As Integer
' Open excel
Set xlApp = Excel.Application
xlApp.Workbooks.Open f_FILELOCATION_ReturnLocation("ANNUAL.XLS"
Set xlSheet = xlApp.Worksheets("Annual Diff"
Set RangeCurrent = xlSheet.Range("C5"
iColCount = 0
iRowCount = 0
' Open database
Set dbcurrent = CurrentDb()
Set rstWeekNumbers = dbcurrent.OpenRecordset("SELECT * FROM WEEK_NUMBERS WHERE CURRENT_YEAR = '2000' ORDER BY WEEK_END_DATE ASC;"
Set rstExcelRows = dbcurrent.OpenRecordset("SELECT * FROM EXCEL_ROWS ORDER BY BRANCH_NUMBER;", dbOpenDynaset)
' Loop through recordsets
With rstWeekNumbers
If Not .EOF Then .MoveLast
If Not .BOF Then .MoveFirst
If .RecordCount > 0 Then
' Copy over records
Do While Not .EOF
If Not rstExcelRows.EOF Then rstExcelRows.MoveLast
If Not rstExcelRows.BOF Then rstExcelRows.MoveFirst
' Write data
If rstExcelRows.RecordCount > 0 Then
Do While Not rstExcelRows.EOF
If IsNull(DLookup("BALANCE", "DIFFERENCES", "BRANCH_NUMBER = " & rstExcelRows!BRANCH_NUMBER & " AND WEEK_ENDING_DATE = #" & Format(!WEEK_END_DATE, "mm/dd/yyyy" & "#") Then
' Value not found in difference table, lookup difference derived.
If IsNull(DLookup("BALANCE", "DIFFERENCES_DERIVED", "BRANCH_NUMBER = " & rstExcelRows!BRANCH_NUMBER & " AND WEEK_ENDING_DATE = #" & Format(!WEEK_END_DATE, "mm/dd/yyyy" & "#") Then
' Value not found in difference derived table, paint cell yellow
RangeCurrent.Offset(iRowCount, iColCount).Interior.ColorIndex = 6
RangeCurrent.Offset(iRowCount, iColCount).Interior.Pattern = xlSolid
Else
' Value found in difference derived table, write to spreadshet.
RangeCurrent.Offset(iRowCount, iColCount).Value = DLookup("BALANCE", "DIFFERENCES_DERIVED", "BRANCH_NUMBER = " & rstExcelRows!BRANCH_NUMBER & " AND WEEK_ENDING_DATE = #" & Format(!WEEK_END_DATE, "mm/dd/yyyy" & "#"
End If
Else
' Value found in difference table, write to excel spreadsheet.
RangeCurrent.Offset(iRowCount, iColCount).Value = DLookup("BALANCE", "DIFFERENCES", "BRANCH_NUMBER = " & rstExcelRows!BRANCH_NUMBER & " AND WEEK_ENDING_DATE = #" & Format(!WEEK_END_DATE, "mm/dd/yyyy" & "#"
End If
iColCount = iColCount + 1
rstExcelRows.MoveNext
Loop
iColCount = 0
End If
iRowCount = iRowCount + 1
.MoveNext
Loop
End If
End With
' Close database objects
rstWeekNumbers.Close
rstExcelRows.Close
dbcurrent.Close
' Save excel
xlApp.ActiveWorkbook.Save
xlApp.Quit
' Destroy objects
Set rstExcelRows = Nothing
Set rstWeekNumbers = Nothing
Set dbcurrent = Nothing
Set RangeCurrent = Nothing
Set xlSheet = Nothing
Set xlApp = Nothing
exit_s_RPT_Difference_CreateBodyDifference:
Exit Sub
err_s_RPT_Difference_CreateBodyDifference:
Beep
MsgBox "An error has occured. Please contact IT with details below" & vbNewLine & _
vbNewLine & Err.Number & vbNewLine & Err.Description
Resume exit_s_RPT_Difference_CreateBodyDifference
End Sub
Missy Ed
Looking to exchange ideas and tips on VB and MS Access development as well as office 97 development.