ivan624888
New Member
- Joined
- Oct 4, 2019
- Messages
- 5
WHEN click a button in Access, which suppose to generate an excel report normally. But this time it shows error 7: Out of memory
Appreciate your suggestions.
Code:
Private Sub btn_Click()
On Error GoTo ErrCheck
Dim strPeriod As String
Dim intFY As Integer
Dim strFMth As String
Dim cnt As New ADODB.Connection
Dim rst As ADODB.Recordset
Dim xlApp As Object
Dim xlWb As Object
Dim xlWs As Object
Dim fldCount As Integer
Dim recCount As Long
Dim iCol As Integer
' Open connection to the database
cnt.Open CONNECT_STRING_ADO
Dim intFYear As Integer
'Prompt user for fyear:
DoCmd.OpenForm "frmFYearSelect", acNormal, , , acFormEdit, acDialog
If DLookup("status_ok", "tblTemp_FYearSelect") = True Then
intFYear = DLookup("FYear", "tblTemp_FYearSelect")
Else
GoTo ExitRoutine
End If
' Define the recordset:
Set rst = fnDisconnectedRS("SELECT * FROM udv_rpt_extract_depr_current_KERRY WHERE fyear = " & intFYear & " ORDER BY asset_number, cost_centre, gl_number")
' Copy field names to the first row of the worksheet
' Create an instance of Excel and add a workbook
Set xlApp = CreateObject("Excel.Application")
Set xlWb = xlApp.Workbooks.Add
Set xlWs = xlWb.Worksheets("Sheet1")
fldCount = rst.Fields.Count
For iCol = 1 To fldCount
xlWs.Cells(1, iCol).Value = rst.Fields(iCol - 1).Name
Next
' Copy the recordset to the worksheet, starting in cell A2
xlWs.Cells(2, 1).CopyFromRecordset rst
' Display Excel and give user control of Excel's lifetime
xlApp.Visible = True
xlApp.UserControl = True
' Auto-fit the column widths and row heights
xlApp.Selection.CurrentRegion.Columns.AutoFit
xlApp.Selection.CurrentRegion.Rows.AutoFit
'=========================
rst.Close
cnt.Close
Set rst = Nothing
Set cnt = Nothing
' Release Excel references
Set xlWs = Nothing
Set xlWb = Nothing
Set xlApp = Nothing
ExitRoutine:
Exit Sub
ErrCheck:
MsgBox err.Number & "->" & err.description
Resume ExitRoutine
End Sub
Appreciate your suggestions.
Last edited by a moderator: