Can someone explain to me why the below code doesn't work properly.
It doesn't create a spreasheet...sometimes. If i set a breakpoint and work through the code step by step in debug mode then the spreadsheet gets created perfectly. But the next day, the same code (without breakpoints or anything) doesn't create the file at all!!? This is with Excel 2002? does it matter?
Is there any other more stable way to export the code?
CODE:
Dim projtype As String
Dim qry As QueryDef
Dim avgDay As Integer
Dim rs As Recordset
Dim sql1, sql2, dtString As String
Dim dt As Date
Dim sheet As Object
Set db = CurrentDb()
On Error Resume Next
DoCmd.DeleteObject acQuery, "qryXL"
dt = Date
dtString = Format(dt, "mm"
& "/" & Format(dt, "dd"
& "/" & Format(dt, "yy"
sql1 = "SELECT [Date Received], [Approval Received], [Design_Time_Calculation] FROM [Tool Order Log] WHERE [Die No] <> 0 AND [Tool ordered] = #" & dtString & "#"
sql2 = "SELECT [Tool ordered], [Section No], [Die No], [Press], [Apertures], [Weld plate], [Expansion], [Die], [Porthole], [Backer], [Bolster], [Designer].[Designer], [tblToolmaker].[Toolmaker], [Date due], [Held / Comments], [Backers], [Die Holder], [Bolsters], [Bolster Insert], [Cep / Exp], [Date Approved], [Layout Drawing Status], [CQI Design Status], [Special Requirements], [Speed], [Billet Temperature], [Design_Time_Calculation] FROM [Tool Order Log], [Designer], [tblToolmaker] WHERE [Designer].ID=[Tool Order Log].Designer AND [tblToolmaker].ID=[Tool Order Log].[Toolmaker] AND [Die No] <> 0 AND [Tool ordered] = #" & dtString & "#"
Set rs = db.OpenRecordset(sql1, dbOpenDynaset)
If rs.EOF = False And rs.BOF = False Then
With rs
Do Until .EOF
.Edit
'avgDay = IIf((DateDiff("ww", rs("[Approval received]"
, rs("[Date Received]"
, 2) < 1) And (DateDiff("ww", rs("[Approval received]"
, rs("[Date Received]"
, 2) > -1), rs("[Date Received]"
- rs("[Approval received]"
, (rs("[Date Received]"
- rs("[Approval received]"
) - DateDiff("ww", rs("[Approval received]"
, rs("[Date Received]"
, 2) * 2)
!Design_Time_Calculation = avgDay
.Update
.MoveNext
Loop
End With
'DoCmd.RunMacro "Tooling Requisition"
Else: MsgBox "empty"
End If
Set sheet = GetObject("c:\tolog\TOL.xls"
sheet.Application.DisplayAlerts = False
sheet.worksheets.Add
sheet.worksheets("qryXL"
.Delete
sheet.Close vbYes
Set qry = db.CreateQueryDef("qryXL", sql2)
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel97, "qryXL", "c:\tolog\TOL.xls", True
Set sheet = GetObject("c:\tolog\TOL.xls"
sheet.Application.DisplayAlerts = False
sheet.worksheets("Sheet1"
.Delete
sheet.Windows("TOL.xls"
.Visible = True
sheet.Close vbYes
It doesn't create a spreasheet...sometimes. If i set a breakpoint and work through the code step by step in debug mode then the spreadsheet gets created perfectly. But the next day, the same code (without breakpoints or anything) doesn't create the file at all!!? This is with Excel 2002? does it matter?
Is there any other more stable way to export the code?
CODE:
Dim projtype As String
Dim qry As QueryDef
Dim avgDay As Integer
Dim rs As Recordset
Dim sql1, sql2, dtString As String
Dim dt As Date
Dim sheet As Object
Set db = CurrentDb()
On Error Resume Next
DoCmd.DeleteObject acQuery, "qryXL"
dt = Date
dtString = Format(dt, "mm"
sql1 = "SELECT [Date Received], [Approval Received], [Design_Time_Calculation] FROM [Tool Order Log] WHERE [Die No] <> 0 AND [Tool ordered] = #" & dtString & "#"
sql2 = "SELECT [Tool ordered], [Section No], [Die No], [Press], [Apertures], [Weld plate], [Expansion], [Die], [Porthole], [Backer], [Bolster], [Designer].[Designer], [tblToolmaker].[Toolmaker], [Date due], [Held / Comments], [Backers], [Die Holder], [Bolsters], [Bolster Insert], [Cep / Exp], [Date Approved], [Layout Drawing Status], [CQI Design Status], [Special Requirements], [Speed], [Billet Temperature], [Design_Time_Calculation] FROM [Tool Order Log], [Designer], [tblToolmaker] WHERE [Designer].ID=[Tool Order Log].Designer AND [tblToolmaker].ID=[Tool Order Log].[Toolmaker] AND [Die No] <> 0 AND [Tool ordered] = #" & dtString & "#"
Set rs = db.OpenRecordset(sql1, dbOpenDynaset)
If rs.EOF = False And rs.BOF = False Then
With rs
Do Until .EOF
.Edit
'avgDay = IIf((DateDiff("ww", rs("[Approval received]"
!Design_Time_Calculation = avgDay
.Update
.MoveNext
Loop
End With
'DoCmd.RunMacro "Tooling Requisition"
Else: MsgBox "empty"
End If
Set sheet = GetObject("c:\tolog\TOL.xls"
sheet.Application.DisplayAlerts = False
sheet.worksheets.Add
sheet.worksheets("qryXL"
sheet.Close vbYes
Set qry = db.CreateQueryDef("qryXL", sql2)
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel97, "qryXL", "c:\tolog\TOL.xls", True
Set sheet = GetObject("c:\tolog\TOL.xls"
sheet.Application.DisplayAlerts = False
sheet.worksheets("Sheet1"
sheet.Windows("TOL.xls"
sheet.Close vbYes