Hi,
I am in need of assistance. I have created the following code to export data from an access project to Excel. After running the code and creating the Excel file and then closing the form that calls the code, I am no longer able to re-open the form. Some of the code that is used to load defaults on the form is no longer recognized. The strange part is that other users that have not run the code below, are able to open the form w/o issues. Is the code below doing something to the form or to Access itself? It happens on versions 2003, 2007 and 2010.
Public Sub XLS_Export(strID As String)
On Error GoTo Routine2
Dim objXL As Excel.Application
Dim wbk As Excel.Workbook
Dim wks As Excel.Worksheet
Dim rng As Range
Dim strFullPath As String
Dim rst As ADODB.Recordset
Dim strSQL As String
Dim varDt As Variant
DoCmd.Hourglass True
varDt = Format(Date, "yyyymmdd")
strFullPath = CurrentProject.Path & "\Summary_" & varDt & ".xls"
strSQL = "SELECT * "
strSQL = strSQL & "FROM sql_view WHERE id = '" & strID & "';"
' Set to Break on all Errors
Application.SetOption "Error Trapping", 0
Set rst = New ADODB.Recordset
'Assign to recordset
rst.Open strSQL, Application.CurrentProject.Connection, adOpenStatic, adLockReadOnly
'Check for Results
If Not rst.EOF = True Then
' Create the Excel Applicaiton, Workbook and Worksheet and Database object
Set objXL = CreateObject("Excel.Application")
'Open Excel
With objXL
Set wbk = .Workbooks.Add
Set wks = wbk.Worksheets(1)
wks.NAME = "Summary"
With wks
On Error Resume Next
.Range("A2").CopyFromRecordset rst
.Range("A1:I1").Value = Array("ColumnA", "ColumnB", "ColumnC", "ColumnD", "ColumnE", "ColumnF", "ColumnG", "ColumnH", "ColumnI")
.Range("A1:I1").Font.Bold = True
End With
.ActiveWorkbook.SaveAs strFullPath
.Visible = True
End With
End If
Routine1:
' Cleanup all objects (resume next on errors)
On Error Resume Next
Set wks = Nothing
Set wbk = Nothing
Set objXL = Nothing
rst.Close
Set rst = Nothing
DoCmd.Hourglass False
Exit Sub
Routine2:
MsgBox err.Number & " - " & err.Description, vbCritical, "basUtils.XLS_Export"
DoCmd.Hourglass False
Resume Routine1
End Sub
I am in need of assistance. I have created the following code to export data from an access project to Excel. After running the code and creating the Excel file and then closing the form that calls the code, I am no longer able to re-open the form. Some of the code that is used to load defaults on the form is no longer recognized. The strange part is that other users that have not run the code below, are able to open the form w/o issues. Is the code below doing something to the form or to Access itself? It happens on versions 2003, 2007 and 2010.
Public Sub XLS_Export(strID As String)
On Error GoTo Routine2
Dim objXL As Excel.Application
Dim wbk As Excel.Workbook
Dim wks As Excel.Worksheet
Dim rng As Range
Dim strFullPath As String
Dim rst As ADODB.Recordset
Dim strSQL As String
Dim varDt As Variant
DoCmd.Hourglass True
varDt = Format(Date, "yyyymmdd")
strFullPath = CurrentProject.Path & "\Summary_" & varDt & ".xls"
strSQL = "SELECT * "
strSQL = strSQL & "FROM sql_view WHERE id = '" & strID & "';"
' Set to Break on all Errors
Application.SetOption "Error Trapping", 0
Set rst = New ADODB.Recordset
'Assign to recordset
rst.Open strSQL, Application.CurrentProject.Connection, adOpenStatic, adLockReadOnly
'Check for Results
If Not rst.EOF = True Then
' Create the Excel Applicaiton, Workbook and Worksheet and Database object
Set objXL = CreateObject("Excel.Application")
'Open Excel
With objXL
Set wbk = .Workbooks.Add
Set wks = wbk.Worksheets(1)
wks.NAME = "Summary"
With wks
On Error Resume Next
.Range("A2").CopyFromRecordset rst
.Range("A1:I1").Value = Array("ColumnA", "ColumnB", "ColumnC", "ColumnD", "ColumnE", "ColumnF", "ColumnG", "ColumnH", "ColumnI")
.Range("A1:I1").Font.Bold = True
End With
.ActiveWorkbook.SaveAs strFullPath
.Visible = True
End With
End If
Routine1:
' Cleanup all objects (resume next on errors)
On Error Resume Next
Set wks = Nothing
Set wbk = Nothing
Set objXL = Nothing
rst.Close
Set rst = Nothing
DoCmd.Hourglass False
Exit Sub
Routine2:
MsgBox err.Number & " - " & err.Description, vbCritical, "basUtils.XLS_Export"
DoCmd.Hourglass False
Resume Routine1
End Sub