Solved

Need help w/CopyFromRecordset method

Posted on 2004-10-18
23
607 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

726 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