ItIsHardToProgram
Technical User
Hey everyone! I programmed a VBA "manual" loop to store some specific query related data in an excel spreadsheet, where a specific criteria matches.
It is very dirty, and before I run it and try it out, I want to make sure this is the best way to do what I want to do. I seriously doubt it, and I want some expert to maybe "clean up" or at least point me in a better direction. If my process is sound and works, then cheers.
Here is what I am doing:
I am not sure this will perform as I want, but I want to make sure my cpu won't go in an undeniable OverCPUusage.
How long will this process take? Am I seeing it wrong? I've put alot of thought into this, and I am not sure how it will perform. This is run from Access.
Please help me tackle this problem!
Cheers!
Julien
"Knowing that you know is the greatest sign of stupidity, knowing that you are ignorant is the best proof of intelligence.
It is very dirty, and before I run it and try it out, I want to make sure this is the best way to do what I want to do. I seriously doubt it, and I want some expert to maybe "clean up" or at least point me in a better direction. If my process is sound and works, then cheers.
Here is what I am doing:
Code:
Function TransfertExcelAutomation(strSQL As String, _
sEmplacement As String)
On Error GoTo err_Handler
'Excel object variables
Dim appExcel As Excel.Application
Dim wbk As Excel.Workbook
Dim wks As Excel.Worksheet
Dim sTemplate As String
Dim sTempFile As String
Dim sOutput As String
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim sSQL As String
Dim IRecords As Long
Dim iRow As Integer
Dim iCol As Integer
Dim iFld As Integer
Dim ProjectID As String
Dim i As Integer
Const cTabOne As Byte = 1
Const cTabTwo As Byte = 2
Const cStartRow As Byte = 3
Const cStartColumn As Byte = 1
iRow = 4
DoCmd.Hourglass True
'Set to break on all errors
Application.SetOption "Error Trapping", 0
'Start with clean file built from template file
sTemplate = sEmplacement & "\TestTemps.xls"
sOutput = sEmplacement & "\TestTemps2.xls"
If Dir(sOutput) <> "" Then Kill sOutput
FileCopy sTemplate, sOutput
'Create the Excel Application, Workbook and Worksheet and Database object
Set appExcel = New Excel.Application
appExcel.Visible = True
Set wbk = appExcel.Workbooks.Open(sOutput)
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset(strSQL, dbOpenSnapshot)
If Not rst.BOF Then rst.MoveFirst
'For this template, the data must be placed in the appropriate cells of the spreadsheet
'Do While ProjectID <> ""
i = 1
ManualLoop:
Select Case i
Case 1
FirstProcedure:
Do While rst.EOF = False
ProjectID = wbk.Worksheets("BrunoSommaire").Cells(iRow, 1)
If ProjectID = rst.Fields("IDProjet") Then
wbk.Worksheets("SOMMAIRE").Cells(iRow, 9).Value = rst.Fields("Honoraire utilisé")
End If
rst.MoveNext
Loop
iRow = iRow + 1
If ProjectID = wbk.Worksheets("BrunoSommaire").Cells(iRow, 1) = "" Then
GoTo EndOfProcedure
End If
GoTo FirstProcedure
Case 2
SecondProcedure:
Do While rst.EOF = False
ProjectID = wbk.Worksheets("NancySommaire").Cells(iRow, 1)
If ProjectID = rst.Fields("IDProjet") Then
wbk.Worksheets("SOMMAIRE").Cells(iRow, 9).Value = rst.Fields("Honoraire utilisé")
End If
rst.MoveNext
Loop
iRow = iRow + 1
If ProjectID = wbk.Worksheets("NancySommaire").Cells(iRow, 1) = "" Then
GoTo EndOfProcedure
End If
GoTo SecondProcedure
Case 3
ThirdProcedure:
Do While rst.EOF = False
ProjectID = wbk.Worksheets("JefSommaire").Cells(iRow, 1)
If ProjectID = rst.Fields("IDProjet") Then
wbk.Worksheets("SOMMAIRE").Cells(iRow, 9).Value = rst.Fields("Honoraire utilisé")
End If
rst.MoveNext
Loop
iRow = iRow + 1
If ProjectID = wbk.Worksheets("JefSommaire").Cells(iRow, 1) = "" Then
GoTo EndOfProcedure
End If
GoTo ThirdProcedure
Case 4
FourthProcedure:
Do While rst.EOF = False
ProjectID = wbk.Worksheets("MartinSommaire").Cells(iRow, 1)
If ProjectID = rst.Fields("IDProjet") Then
wbk.Worksheets("SOMMAIRE").Cells(iRow, 9).Value = rst.Fields("Honoraire utilisé")
End If
rst.MoveNext
Loop
iRow = iRow + 1
If ProjectID = wbk.Worksheets("MartinSommaire").Cells(iRow, 1) = "" Then
GoTo EndOfProcedure
End If
GoTo FourthProcedure
Case 5
FifthProcedure:
Do While rst.EOF = False
ProjectID = wbk.Worksheets("MaSommaire").Cells(iRow, 1)
If ProjectID = rst.Fields("IDProjet") Then
wbk.Worksheets("SOMMAIRE").Cells(iRow, 9).Value = rst.Fields("Honoraire utilisé")
End If
rst.MoveNext
Loop
iRow = iRow + 1
If ProjectID = wbk.Worksheets("MaSommaire").Cells(iRow, 1) = "" Then
GoTo EndOfProcedure
End If
GoTo FifthProcedure
Case 6
SixthProcedure:
Do While rst.EOF = False
ProjectID = wbk.Worksheets("JimSommaire").Cells(iRow, 1)
If ProjectID = rst.Fields("IDProjet") Then
wbk.Worksheets("SOMMAIRE").Cells(iRow, 9).Value = rst.Fields("Honoraire utilisé")
End If
rst.MoveNext
Loop
iRow = iRow + 1
If ProjectID = wbk.Worksheets("JimSommaire").Cells(iRow, 1) = "" Then
GoTo EndOfProcedure
End If
GoTo SixthProcedure
End Select
'Loop
EndOfManualLoop:
rst.Close
exit_Here:
'Cleanup all objects (resume next on errors)
Set wbk = Nothing
appExcel.Quit
Set appExcel = Nothing
Set rst = Nothing
Set dbs = Nothing
DoCmd.Hourglass False
i = 0
Exit Function
err_Handler:
ExportQuery = Err.Description
Resume exit_Here
End If
EndOfProcedure:
Select Case i
Case 1
i = 2
rst.MoveFirst
GoTo ManualLoop
Case 2
i = 3
GoTo ManualLoop
Case 3
i = 4
GoTo ManualLoop
Case 4
i = 5
GoTo ManualLoop
Case 5
i = 6
GoTo ManualLoop
Case 6
GoTo EndOfManualLoop
End Select
End Function
I am not sure this will perform as I want, but I want to make sure my cpu won't go in an undeniable OverCPUusage.
How long will this process take? Am I seeing it wrong? I've put alot of thought into this, and I am not sure how it will perform. This is run from Access.
Please help me tackle this problem!
Cheers!
Julien
"Knowing that you know is the greatest sign of stupidity, knowing that you are ignorant is the best proof of intelligence.