T
TS
hi all,
I want a button click event in a form to do the following:
Append the result of an access query to an excel file (precisely appending
one row of data coming from the access query to a specific worksheet in an
Excel workbook EVERY DAY) . The query is a crosstab query, so PLEASE DO NOT
SUGGEST an Append Query -I do not need that-. I wrote the following codes
that returned the error message "Select Method of Range Class Failed" when
the line <ActiveCell.Offset(1, 0).CopyFromRecordset rs> is reached. Can you
please tell me what's wrong with my syntax? If this code won't do what I want
to accomplish , can you please suggest a different syntax
Private Sub cmd_ChildrenCountCharts_Click()
Dim strxlfile As String
Dim xlapp As Excel.Application
Dim xlbook As Excel.Workbook
Dim xlsheet As Excel.Worksheet
Dim rs As Recordset
strxlfile = "M:excel files\qry_ProgramTotals.xls"
Set db = CurrentDb
Set rs = db.OpenRecordset("qry_ProgramTotals", dbOpenSnapshot)
Set xlapp = CreateObject("excel.application")
With xlapp
..Visible = True
..WindowState = xlMinimized
End With
Set xlbook = xlapp.Workbooks.Open(strxlfile)
Set xlsheet = xlbook.Worksheets("Programs Total")
xlsheet.Cells.Range("a1").Select
Do Until IsEmpty(ActiveCell)
ActiveCell.Offset(1, 0).Select
Loop
ActiveCell.Offset(1, 0).CopyFromRecordset rs
Set xlapp = Nothing
Set xlbook = Nothing
Set xlsheet = Nothing
rs.Close
Set rs = Nothing
End Sub
I want a button click event in a form to do the following:
Append the result of an access query to an excel file (precisely appending
one row of data coming from the access query to a specific worksheet in an
Excel workbook EVERY DAY) . The query is a crosstab query, so PLEASE DO NOT
SUGGEST an Append Query -I do not need that-. I wrote the following codes
that returned the error message "Select Method of Range Class Failed" when
the line <ActiveCell.Offset(1, 0).CopyFromRecordset rs> is reached. Can you
please tell me what's wrong with my syntax? If this code won't do what I want
to accomplish , can you please suggest a different syntax
Private Sub cmd_ChildrenCountCharts_Click()
Dim strxlfile As String
Dim xlapp As Excel.Application
Dim xlbook As Excel.Workbook
Dim xlsheet As Excel.Worksheet
Dim rs As Recordset
strxlfile = "M:excel files\qry_ProgramTotals.xls"
Set db = CurrentDb
Set rs = db.OpenRecordset("qry_ProgramTotals", dbOpenSnapshot)
Set xlapp = CreateObject("excel.application")
With xlapp
..Visible = True
..WindowState = xlMinimized
End With
Set xlbook = xlapp.Workbooks.Open(strxlfile)
Set xlsheet = xlbook.Worksheets("Programs Total")
xlsheet.Cells.Range("a1").Select
Do Until IsEmpty(ActiveCell)
ActiveCell.Offset(1, 0).Select
Loop
ActiveCell.Offset(1, 0).CopyFromRecordset rs
Set xlapp = Nothing
Set xlbook = Nothing
Set xlsheet = Nothing
rs.Close
Set rs = Nothing
End Sub