I am creating spreadsheets from a recordset as shown below.
I have a function I am running, below, that opens the spreadsheet that was created, formats it, and then should close and allow the loop to continue.
The problem I am having is that code seems to run fine, but Access gets tied up and won't continue past the loop. It just freezes.
Code:
Do While Not rs1.EOF
sqlStr = "SELECT Transsend_M3_Var.APP_USERNAME, Transsend_M3_Var.Last_Name, Transsend_M3_Var.First_Name, Transsend_M3_Var.Job_Code, Transsend_M3_Var.Job_Title, Transsend_M3_Var.Dept_ID, Transsend_M3_Var.Dept_Title, Transsend_M3_Var.Roles " & _
"FROM Transsend_M3_Var " & _
"WHERE ((Transsend_M3_Var.Manager_ID)='" & rs1.Fields("Manager_ID") & "');"
Set rs2 = CurrentDb.OpenRecordset(sqlStr)
If (rs2.RecordCount = 0) Then
MsgBox ("No records found on Transsend_M3_Var")
Exit Do
Else
rs2.MoveLast
rs2.MoveFirst
Dim rsExport As DAO.QueryDef
Set rsExport = CurrentDb.CreateQueryDef("myExportQueryDef", sqlStr)
fileName = filePath & rs1.Fields("Manager_ID") & ".xlsx"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "myExportQueryDef", fileName, True
Tools.formatWorkbooks (fileName)
CurrentDb.QueryDefs.Delete rsExport.Name
End If
rs1.MoveNext
Loop
I have a function I am running, below, that opens the spreadsheet that was created, formats it, and then should close and allow the loop to continue.
Code:
Public Function formatWorkbooks(fileName As String)
Dim xlApp As Excel.Application, xlWork As Excel.Workbook, xlSheet1 As Excel.Worksheet
Set xlApp = New Excel.Application
'xlApp.Visible = False
Set xlWork = xlApp.Workbooks.Open(fileName)
'xlWork.Windows(1).Visible = False
Set xlSheet1 = xlWork.Worksheets(1)
With xlSheet1
.Range("I1").FormulaR1C1 = "APPROVE/DENY"
.Range("A1", "I1").Columns.AutoFit
.Range("A1", "I1").Characters.Font.Bold = True
End With
xlWork.Close True
Set xlWork = Nothing
xlApp.Quit
Set xlApp = Nothing
End Function
The problem I am having is that code seems to run fine, but Access gets tied up and won't continue past the loop. It just freezes.