I'm trying to get the data from a pass through query into a recordset to then put it into Excel.
It seems to fail on the querydefs part. I know this code has worked before but the connection is now through ODBC on this database and it seems to be throwing everything?
Option Compare Database
Private Sub cmd_Export_Click()
'MsgBox (Me.Form.RecordSource)
'MsgBox (Forms!TabsReport!Top10Debtors_BU.Form.RecordSource)
Dim iCount As Integer
Dim strCell As String
'iCount = DCount("[Symonds Div Level Description]", Form.RecordSource)
Application.SetOption "Show Status Bar", True
StatusBar = SysCmd(acSysCmdSetStatus, "Formatting export file... please wait.")
'--------------------------------------
'Main recordset with level codes
'--------------------------------------
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim rs As DAO.Recordset
Dim rst As DAO.Recordset
Dim rstReceipts As DAO.Recordset
Set db = CurrentDb
'Set qdf = db.QueryDefs("LedgerReportByController")
'For Each prm In qdf.Parameters
' prm.Value = Eval(prm.Name)
'Next prm
Set rs = db.OpenRecordset("LedgerReportByController", dbOpenSnapshot)
'Start a new workbook in Excel
Dim oApp As New Excel.Application
Dim oBook As Excel.workBook
Dim oSheet As Excel.workSheet
'uncomment these 2 lines for debugging
'oApp.Visible = True
'oApp.UserControl = True
Set oBook = oApp.Workbooks.Add
Set oSheet = oBook.Worksheets(1)
'Add the field names in row 1
Dim i As Integer
Dim iNumCols As Integer
iNumCols = rs.Fields.Count
For i = 1 To iNumCols
oSheet.Cells(1, i).Value = rs.Fields(i - 1).Name
Next
'Add the data starting at cell A2
oSheet.Range("A2").CopyFromRecordset rs
'------------------------------------------
'Format the sheet
'------------------------------------------
With oSheet
'put cursor into first cell
.Range("A1").Select
End With
'--------------------------------------
'Tidy up
'--------------------------------------
oApp.Visible = True
oApp.UserControl = True
'Close the Database and Recordset
rs.Close
rst.Close
rstReceipts.Close
db.Close
qdf.Close
Set rs = Nothing
Set rst = Nothing
Set rstReceipts = Nothing
Set qdf = Nothing
Set db = Nothing
Set oBook = Nothing
Set oSheet = Nothing
Set oApp = Nothing
vStatusBar = SysCmd(acSysCmdClearStatus)
End Sub
It seems to fail on the querydefs part. I know this code has worked before but the connection is now through ODBC on this database and it seems to be throwing everything?
Option Compare Database
Private Sub cmd_Export_Click()
'MsgBox (Me.Form.RecordSource)
'MsgBox (Forms!TabsReport!Top10Debtors_BU.Form.RecordSource)
Dim iCount As Integer
Dim strCell As String
'iCount = DCount("[Symonds Div Level Description]", Form.RecordSource)
Application.SetOption "Show Status Bar", True
StatusBar = SysCmd(acSysCmdSetStatus, "Formatting export file... please wait.")
'--------------------------------------
'Main recordset with level codes
'--------------------------------------
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim rs As DAO.Recordset
Dim rst As DAO.Recordset
Dim rstReceipts As DAO.Recordset
Set db = CurrentDb
'Set qdf = db.QueryDefs("LedgerReportByController")
'For Each prm In qdf.Parameters
' prm.Value = Eval(prm.Name)
'Next prm
Set rs = db.OpenRecordset("LedgerReportByController", dbOpenSnapshot)
'Start a new workbook in Excel
Dim oApp As New Excel.Application
Dim oBook As Excel.workBook
Dim oSheet As Excel.workSheet
'uncomment these 2 lines for debugging
'oApp.Visible = True
'oApp.UserControl = True
Set oBook = oApp.Workbooks.Add
Set oSheet = oBook.Worksheets(1)
'Add the field names in row 1
Dim i As Integer
Dim iNumCols As Integer
iNumCols = rs.Fields.Count
For i = 1 To iNumCols
oSheet.Cells(1, i).Value = rs.Fields(i - 1).Name
Next
'Add the data starting at cell A2
oSheet.Range("A2").CopyFromRecordset rs
'------------------------------------------
'Format the sheet
'------------------------------------------
With oSheet
'put cursor into first cell
.Range("A1").Select
End With
'--------------------------------------
'Tidy up
'--------------------------------------
oApp.Visible = True
oApp.UserControl = True
'Close the Database and Recordset
rs.Close
rst.Close
rstReceipts.Close
db.Close
qdf.Close
Set rs = Nothing
Set rst = Nothing
Set rstReceipts = Nothing
Set qdf = Nothing
Set db = Nothing
Set oBook = Nothing
Set oSheet = Nothing
Set oApp = Nothing
vStatusBar = SysCmd(acSysCmdClearStatus)
End Sub