copy recordset to Excel Workbook

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hiya

Access/Excel 97
Say I have this recordset from a query

Dim dbs As Database, rst As Recordse
Set dbs = CurrentD
Set rst = dbs.OpenRecordset("qryExternal Referral Report", dbOpenDynaset

Is there a way that I can copy the whole recordset to desktop/myworkbook.xls on sheet 'Sheet 1' and from cell A7? I'd like to copy the whole lot across in one go - exactly as if I were opening the Access query in datasheet view, copying the lot to clipboard, going into myworkbook, selecting cell A7 and going to edit>paste

Anyone know how this can be done

Thanks loads
Baz
 
I have done something similar but for Access/Excel 2000
maybe helps you
the code is an adaptation from Alison Balter's book:
....... MORE CODE ......
'Tambien para el Send to Excel
Function CreateRecordset(rstData As ADODB.Recordset,
strTableName As String)
On Error GoTo CreateRecordset_Err
rstData.CursorType = adOpenStatic
rstData.Open strTableName, Options:=adCmdTable
CreateRecordset = True
CreateRecordset_Exit:
Exit Function
CreateRecordset_Err:
MsgBox "Error " & Err.Number & ": " & Err.Description
Resume CreateRecordset_Exit
End Function
Sub SendToExcel(strQryName As String, intNo As Integer)
On Error GoTo Err_SendToExcel

Dim rstData As ADODB.Recordset
Dim rng As Excel.Range
Dim objWS As Excel.Worksheet
Dim strPath As String
Dim strSQL As String
Dim strFiltro As String

Set rstData = New ADODB.Recordset
rstData.ActiveConnection = CurrentProject.Connection
strPath = "I:\01\Drives\PIBDE General\Access
Dbases\Service\Reports\ServiceMeasurements.xls"
strSQL = "GenDataID Like " & Me.cboService

If (intNo = 1 Or intNo = 2 Or intNo = 3 Or intNo = 4 Or
intNo = 5) Then

If CreateRecordset(rstData, strQryName) Then
rstData.Filter = strSQL
If rstData.RecordCount = 0 Then ' el recordset esta vacio
MsgBox "There is no data to send!!", vbCritical, "Warning"
GoTo Salir_Sub
End If ' rstData count
If CreateExcelObj() Then
gobjExcel.Workbooks.Open strPath
gobjExcel.Visible = True
Set objWS = gobjExcel.ActiveSheet
' HERE IS WHERE YOU SEND THE RECORDSET TO EXCEL
objWS.Range("A2").CopyFromRecordset rstData
'format data

............... MORE CODE...........

gr
-----Original Message-----
Hiya,

Access/Excel 97.
Say I have this recordset from a query:

Dim dbs As Database, rst As Recordset
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset("qryExternal Referral Report", dbOpenDynaset)

Is there a way that I can copy the whole recordset to
desktop/myworkbook.xls on sheet 'Sheet 1' and from cell
A7? I'd like to copy the whole lot across in one go -
exactly as if I were opening the Access query in datasheet
view, copying the lot to clipboard, going into myworkbook,
selecting cell A7 and going to edit>paste.
 
You can write code in access that outputs your query or table to an excel
spreadsheet. It looks something like this:

DoCmd.OutputTo acOutputTable, "Objectname", "MicrosoftExcel(*.xls)",
"C:\nameOfSpreadsheet.xls", False, ""

The above is for outputting a table. If you want to output a query you
change the syntax 'acOutputTable' to 'acOutputQuery'

Hope this helps.

Baz said:
Hiya,

Access/Excel 97.
Say I have this recordset from a query:

Dim dbs As Database, rst As Recordset
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset("qryExternal Referral Report", dbOpenDynaset)

Is there a way that I can copy the whole recordset to
desktop/myworkbook.xls on sheet 'Sheet 1' and from cell A7? I'd like to copy
the whole lot across in one go - exactly as if I were opening the Access
query in datasheet view, copying the lot to clipboard, going into
myworkbook, selecting cell A7 and going to edit>paste.
 
Thanks GR,

I think you might have left out some referenced functions that I need to understand what is going on - eg CreateExcelObj()?. I don't get all the goobj references too? Further help required please...

How would I open an Excel workbook in the first place from within Access97?

Baz
 
Thanks, had a look at this and transferspreadsheet method. But need to send it to a particular sheet and a particular cell.
 
sorry, here's the rest of the code. I remind you that I'm
working with Access 2000 I don't know if these works with
97

----- start code -------------
Option Compare Database
Option Explicit

'Variables globales
Public gobjExcel As Excel.Application

Function CreateExcelObj() As Boolean
'Launch excel
On Error GoTo CreateExcelObj_Err
CreateExcelObj = False
Set gobjExcel = New Excel.Application
CreateExcelObj = True

CreateExcelObj_Exit:
Exit Function

CreateExcelObj_Err:
MsgBox "Couldn't Launch Excel!!",
vbCritical, "Warning"
CreateExcelObj = False
Resume CreateExcelObj_Exit
End Function

Function CreateRecordset(rstData As ADODB.Recordset,
strTableName As String)
On Error GoTo CreateRecordset_Err
rstData.CursorType = adOpenStatic
rstData.Open strTableName, Options:=adCmdTable
CreateRecordset = True
CreateRecordset_Exit:
Exit Function
CreateRecordset_Err:
MsgBox "Error " & Err.Number & ": " &
Err.Description
Resume CreateRecordset_Exit
End Function

////// here is part it's quite big, so i'm sending you
only a small part (the rest my confuse you and is useless
to your post) you only need to fix the if-else-end if and
with/end with's as well ///////////////////////

Sub SendToExcel(strQryName As String, intNo As Integer)
On Error GoTo Err_SendToExcel

Dim rstData As ADODB.Recordset
Dim rng As Excel.Range
Dim objWS As Excel.Worksheet
Dim strPath As String
Dim strSQL As String
Dim strFiltro As String

Set rstData = New ADODB.Recordset
rstData.ActiveConnection = CurrentProject.Connection
strPath = "I:\01\Drives\PIBDE General\Access
Dbases\Service\Reports\ServiceMeasurements.xls"
strSQL = "GenDataID Like " & Me.cboService

If (intNo = 1 Or intNo = 2 Or intNo = 3 Or intNo = 4 Or
intNo = 5) Then

If CreateRecordset(rstData, strQryName) Then
rstData.Filter = strSQL
If rstData.RecordCount = 0 Then ' el
recordset esta vacio
MsgBox "There is no data to
send!!", vbCritical, "Warning"
GoTo Salir_Sub
End If ' rstData count
If CreateExcelObj() Then
gobjExcel.Workbooks.Open
strPath
gobjExcel.Visible = True
Set objWS =
gobjExcel.ActiveSheet
objWS.Range
("A2").CopyFromRecordset rstData
'Dar formato
With gobjExcel
.Range("A1").Select
.ActiveCell.CurrentRegi
on.Select
Set rng = .Selection
rng.NumberFormat = "#,
##0.000"

'Crear la grafica
If (intNo = 1) Then
'Nominal Data
chart
.Columns
("A:B").Delete
.Columns
("E").Delete
.Range
("A1").Value = "MEASURE"
.Range
("B1").Value = "START 130% TORQUE"
.Range
("C1").Value = "RATED SPEED"
.Range
("D1").Value = "MAX. SPEED"
.Rows
(1).Font.Bold = True
.Rows
(1).Font.Color = 10040115

rng.EntireColumn.AutoFit
.Range
(rng.Address).Select

Set rng
= .Selection
.Charts.Add
'Chart Format
With
gobjExcel.ActiveChart
.PlotBy
= xlColumns
.Locati
on xlLocationAsNewSheet
.HasTit
le = True

With .ChartTitle

.Characters.Text = "Nominal Data"

.Font.FontStyle = "Tahoma"

.Font.Bold = True

.Font.Size = 16
End
With

With .ChartArea

.Border.Weight = 1

.Shadow = True
End
With
.Legend
..Font.Size = 8
.Legend
..Position = xlLegendPositionBottom
'
Rotate the x-axis labels to a 45-degree angle
.Axes
(xlCategory).TickLabels.Orientation = 45
' the
legend
.HasLeg
end = True
End With

////// the code above creates the recordset, filters it,
sends it to Excel to a specific cell, make a chart in a
new worksheet, gives a name to the Data sheet (where the
recordset was sent) and also to the Chart sheet /////

Now i'm leaving from office, but if you need any further
info please repost and gladly I will answer you tomorrow.

By the way I send a post in Queries newsgroup (jan 19
4:34am), maybe you can help me :-)

thx.



-----Original Message-----
Thanks GR,

I think you might have left out some referenced functions
that I need to understand what is going on - eg
CreateExcelObj()?. I don't get all the goobj references
too? Further help required please...
 
Just a note, in my code I'm filtering the recordset, this
is very inefficient, but is what i need. But maybe in your
case this is not necessary so I suggest you if you need to
filter to do this directly on your query.
-----Original Message-----
sorry, here's the rest of the code. I remind you that I'm
working with Access 2000 I don't know if these works with
97

----- start code -------------
Option Compare Database
Option Explicit

'Variables globales
Public gobjExcel As Excel.Application

Function CreateExcelObj() As Boolean
'Launch excel
On Error GoTo CreateExcelObj_Err
CreateExcelObj = False
Set gobjExcel = New Excel.Application
CreateExcelObj = True

CreateExcelObj_Exit:
Exit Function

CreateExcelObj_Err:
MsgBox "Couldn't Launch Excel!!",
vbCritical, "Warning"
CreateExcelObj = False
Resume CreateExcelObj_Exit
End Function

Function CreateRecordset(rstData As ADODB.Recordset,
strTableName As String)
On Error GoTo CreateRecordset_Err
rstData.CursorType = adOpenStatic
rstData.Open strTableName, Options:=adCmdTable
CreateRecordset = True
CreateRecordset_Exit:
Exit Function
CreateRecordset_Err:
MsgBox "Error " & Err.Number & ": " &
Err.Description
Resume CreateRecordset_Exit
End Function

////// here is part it's quite big, so i'm sending you
only a small part (the rest my confuse you and is useless
to your post) you only need to fix the if-else-end if and
with/end with's as well ///////////////////////

Sub SendToExcel(strQryName As String, intNo As Integer)
On Error GoTo Err_SendToExcel

Dim rstData As ADODB.Recordset
Dim rng As Excel.Range
Dim objWS As Excel.Worksheet
Dim strPath As String
Dim strSQL As String
Dim strFiltro As String

Set rstData = New ADODB.Recordset
rstData.ActiveConnection = CurrentProject.Connection
strPath = "I:\01\Drives\PIBDE General\Access
Dbases\Service\Reports\ServiceMeasurements.xls"
strSQL = "GenDataID Like " & Me.cboService

If (intNo = 1 Or intNo = 2 Or intNo = 3 Or intNo = 4 Or
intNo = 5) Then

If CreateRecordset(rstData, strQryName) Then
rstData.Filter = strSQL
If rstData.RecordCount = 0 Then ' el
recordset esta vacio
MsgBox "There is no data to
send!!", vbCritical, "Warning"
GoTo Salir_Sub
End If ' rstData count
If CreateExcelObj() Then
gobjExcel.Workbooks.Open
strPath
gobjExcel.Visible = True
Set objWS =
gobjExcel.ActiveSheet
objWS.Range
("A2").CopyFromRecordset rstData
'Dar formato
With gobjExcel
.Range("A1").Select
.ActiveCell.CurrentReg i
on.Select
Set rng = .Selection
rng.NumberFormat = "#,
##0.000"

'Crear la grafica
If (intNo = 1) Then
'Nominal Data
chart
.Columns
("A:B").Delete
.Columns
("E").Delete
.Range
("A1").Value = "MEASURE"
.Range
("B1").Value = "START 130% TORQUE"
.Range
("C1").Value = "RATED SPEED"
.Range
("D1").Value = "MAX. SPEED"
.Rows
(1).Font.Bold = True
.Rows
(1).Font.Color = 10040115

rng.EntireColumn.AutoFit
.Range
(rng.Address).Select

Set rng
= .Selection
.Charts.Add
'Chart Format
With
gobjExcel.ActiveChart
.PlotB y
= xlColumns
.Locat i
on xlLocationAsNewSheet
.HasTi t
le = True

With .ChartTitle
.Characters.Text = "Nominal Data"
.Font.FontStyle = "Tahoma"
.Font.Bold = True
.Font.Size = 16
End
With

With .ChartArea
 
Is there a way that I can copy the whole recordset to
desktop/myworkbook.xls on sheet 'Sheet 1' and from cell A7? I'd like
to copy the whole lot across in one go - exactly as if I were opening
the Access query in datasheet view, copying the lot to clipboard,
going into myworkbook, selecting cell A7 and going to edit>paste.

Is there any reason for not doing this all within Excel? It will be faster,
easier to debug, and I have a feeling that Excel VBA has a GetRows method
that performs exactly this job... Look up VBA help in Excel for more
details.

HTH

Tim F
 
Back
Top