Solved

Need help w/CopyFromRecordset method

Posted on 2004-10-18
23
605 Views
Last Modified: 2010-08-05
Hi,

I have checked the forum for some examples of using the CopyFromRecordset method, however I am still not certain how to implement it, so I built a test database w/the hopes that one of you folks could display the code for it -- as well as comments of what the code is doing.  
I'd like to transfer the data from my query (Access 2002) into an existing / formatted excel spreadsheet (Excel 2002) -- here is what I have:


tblTEST
Name
Address
Zip

qselTEST
SELECT tblTEST.Name, tblTEST.Address, tblTEST.Zip
FROM tblTEST;

frmTEST (with command button -- cmdTEST)

An Excel spreadsheet:
Location: C:\Documents and Settings\3765\Desktop\TEST.xls
The spreadsheet contains 1 worksheet named "Sheet1"  
Cell A3 contains "Name"   (width 25)
Cell B3 contains "Address"  (width 50)
Cell C3 contains "Zip"  (width 10)
So the data would begin at cell A4...

Thanks!
Ed
0
Comment
Question by:MICHAED
  • 11
  • 7
  • 5
23 Comments
 
LVL 41

Expert Comment

by:shanesuebsahakarn
ID: 12342156
Just use TransferSpreadsheet like so:

DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "tblTEST", " C:\Documents and Settings\3765\Desktop\TEST.xls", False, "Sheet1!A4:A4"
0
 
LVL 41

Expert Comment

by:shanesuebsahakarn
ID: 12342163
Whoops, tblTEST above should be qselTEST of course.
0
 

Author Comment

by:MICHAED
ID: 12346573
Hi Shane,

I tried the above, however, I get "run-time error 3436 -- failure creating file."
0
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
LVL 46

Expert Comment

by:tbsgadi
ID: 12346700
Try
DoCmd.TransferSpreadsheet acExport, 8,  "qselTEST", "C:\Documents and Settings\3765\Desktop\TEST.xls", False, "Sheet1!A4:A4"
0
 

Author Comment

by:MICHAED
ID: 12346747
I get run-time error 3010 -- table 'sheet1$A4:A4' already exists.
0
 
LVL 46

Expert Comment

by:tbsgadi
ID: 12346894
Try
DoCmd.TransferSpreadsheet acExport, 8,  "qselTEST", "C:\Documents and Settings\3765\Desktop\TEST.xls", True

This will put the names in aswell.
0
 

Author Comment

by:MICHAED
ID: 12347311
tbsgadi,

I tried it, however, the data exports into the spreadsheet & creates another worksheet.  I already have a blank formatted worksheet (similar to a template), and I want the data to export into the existing spreadsheet/worksheet beginning with cell A4.
0
 
LVL 41

Expert Comment

by:shanesuebsahakarn
ID: 12348210
Try omitting the second parameter entirely:
DoCmd.TransferSpreadsheet acExport, , "qselTEST", " C:\Documents and Settings\3765\Desktop\TEST.xls", False, "Sheet1!A4:A4"
0
 
LVL 46

Expert Comment

by:tbsgadi
ID: 12348277
Sorry you can use the "Sheet1!A4:A4" only for importing from Excel not Exporting.
0
 

Author Comment

by:MICHAED
ID: 12348310
>>>Try omitting the second parameter entirely:<<<
DoCmd.TransferSpreadsheet acExport, , "qselTEST", " C:\Documents and Settings\3765\Desktop\TEST.xls", False, "Sheet1!A4:A4"


Hmmm.... I tried it, but I still get run-time error number 3436 -- "faliure creating file."
0
 
LVL 41

Expert Comment

by:shanesuebsahakarn
ID: 12348404
I tried it and it worked ok for me...(Access 2002). What happens if you put the document in another folder?
0
 

Author Comment

by:MICHAED
ID: 12348472
>>>What happens if you put the document in another folder?<<<

I get the same error message.
0
 
LVL 46

Expert Comment

by:tbsgadi
ID: 12348487
From Access2002 help

Range   Optional Variant. A string expression that's a valid range of cells or the name of a range in the spreadsheet. This argument applies only to importing. Leave this argument blank to import the entire spreadsheet. When you export to a spreadsheet, you must leave this argument blank. If you enter a range, the export will fail.

0
 

Author Comment

by:MICHAED
ID: 12348531
>>>If you enter a range, the export will fail<<<

That's why I was trying to use the CopyFromRecordset method...I'm trying to export into an Excel "template" pre-formatted with Row headings & the like.
0
 
LVL 41

Expert Comment

by:shanesuebsahakarn
ID: 12348574
> If you enter a range, the export will fail.

It works fine in Access 2002, regardless of what the help file says.
0
 
LVL 46

Expert Comment

by:tbsgadi
ID: 12348645
I found this code on the web you could try adapting it


Option Explicit
Sub aa()
Dim dbconnstring As String
Dim DBConn As ADODB.Connection
Dim RS As ADODB.Recordset
Dim sSql As String
Dim ws As Worksheet
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim ws3 As Worksheet
Dim I As Long
    Set ws = Workbooks("demoload.xls").Worksheets(1)
    Set ws1 = Workbooks("demoload.xls").Worksheets(2)
    Set ws2 = Workbooks("demoload.xls").Worksheets(3)
    Set ws3 = Workbooks("demoload.xls").Worksheets(4)
    Set DBConn = New ADODB.Connection
    Set RS = New Recordset
    dbconnstring = "dsn=LocalServer;uid=usexr;pwd=pwdx;database=databasex;"
    DBConn.CursorLocation = adUseClient
    DBConn.Open dbconnstring
   
  sSql = "select cast(pais as char) from democount"
 
  RS.Open sSql, DBConn, adOpenStatic, adLockOptimistic

ws.Range("a100").CopyFromRecordset RS
ws1.Range("a100").CopyFromRecordset RS
ws2.Range("a1000").CopyFromRecordset RS
ws3.Range("a1000").CopyFromRecordset RS

End Sub
0
 
LVL 46

Accepted Solution

by:
tbsgadi earned 500 total points
ID: 12348681
0
 

Author Comment

by:MICHAED
ID: 12348692
tbsgadi,

That's what I was originally asking for -- there are similar examples on this forum. However, I am uncertain how to adapt it to my criteria, that's why I posted all the items above.  I was hoping someone could "plug-in" my items into the code.
0
 

Author Comment

by:MICHAED
ID: 12348711
tbsgadi,

I will try to adapt the code from the link you provided and let you know if it works for me.
0
 

Author Comment

by:MICHAED
ID: 12349200
Ok...I am utilizing the following:

Sub sCopyRSToNamedRange()
'Copy records to a named range
'on an existing worksheet on a
'workbook
'
Dim objXL As Excel.Application
Dim objWkb As Excel.Workbook
Dim objSht As Excel.Worksheet
Dim db As Database
Dim rs As Recordset
Const conMAX_ROWS = 20000
Const conSHT_NAME = "Sheet1"
Const conWKB_NAME = "C:\Documents and Settings\3765\Desktop\TEST.xls"
Const conRANGE = "A4:C6"

  Set db = CurrentDb
  Set objXL = New Excel.Application
  Set rs = db.OpenRecordset("qselTEST", dbOpenSnapshot)
  With objXL
    .Visible = True
    Set objWkb = .Workbooks.Open(conWKB_NAME)
    On Error Resume Next
    Set objSht = objWkb.Worksheets(conSHT_NAME)
    If Not Err.Number = 0 Then
      Set objSht = objWkb.Worksheets.Add
      objSht.Name = conSHT_NAME
    End If
    Err.Clear
    On Error GoTo 0
    objSht.Range(conRANGE).CopyFromRecordset rs
  End With
  Set objSht = Nothing
  Set objWkb = Nothing
  Set objXL = Nothing
  Set rs = Nothing
  Set db = Nothing
End Sub


However, I am getting a "Type Mismatch (run-time 13) error on this line:
Set rs = db.OpenRecordset("tblTEST", dbOpenSnapshot)

Ed
0
 

Author Comment

by:MICHAED
ID: 12350781
tbsgadi,
I used the code from the link, and it solved my problem.  However, I replaced:

Dim db As Database
Dim rs As Recordset

with:
Dim db As DAO.Database
Dim rs As DAO.Recordset



So, Now I have the following:

Sub sCopyRSToNamedRange()
'Copy records to a named range on an existing worksheet on a workbook

Dim objXL As Excel.Application
Dim objWkb As Excel.Workbook
Dim objSht As Excel.Worksheet
Dim db As DAO.Database
Dim rs As DAO.Recordset
Const conMAX_ROWS = 20000
Const conSHT_NAME = "SHEET1"
Const conWKB_NAME = "C:\Documents and Settings\3765\Desktop\TEST.XLS"
Const conRANGE = "A4:C6"

  Set db = CurrentDb
  Set objXL = New Excel.Application
  Set rs = db.OpenRecordset("qselTEST", dbOpenSnapshot)
  With objXL
    .Visible = True
    Set objWkb = .Workbooks.Open(conWKB_NAME)
    On Error Resume Next
    Set objSht = objWkb.Worksheets(conSHT_NAME)
    If Not Err.Number = 0 Then
      Set objSht = objWkb.Worksheets.Add
      objSht.Name = conSHT_NAME
    End If
    Err.Clear
    On Error GoTo 0
    objSht.Range(conRANGE).CopyFromRecordset rs
  End With
  Set objSht = Nothing
  Set objWkb = Nothing
  Set objXL = Nothing
  Set rs = Nothing
  Set db = Nothing
End Sub
(Credit: The above code was originally written by Dev Ashish)


Everything works well, however, the Excel spreadsheet stays open, I would like to have it populated & CLOSED.  How can I do this?

Thanks,
Ed



0
 

Author Comment

by:MICHAED
ID: 12350830
Never mind....I will award points and post as a separate question.

Ed
0
 
LVL 46

Expert Comment

by:tbsgadi
ID: 12350923
Hi thanks,
I'm not sure of the exact syntax but you want to add a line something like
 .Workbooks.Close
Good Luck!
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question