Solved

Need help w/CopyFromRecordset method

Posted on 2004-10-18
23
602 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
Comment Utility
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
Comment Utility
Whoops, tblTEST above should be qselTEST of course.
0
 

Author Comment

by:MICHAED
Comment Utility
Hi Shane,

I tried the above, however, I get "run-time error 3436 -- failure creating file."
0
 
LVL 46

Expert Comment

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

Author Comment

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

Expert Comment

by:tbsgadi
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Sorry you can use the "Sheet1!A4:A4" only for importing from Excel not Exporting.
0
 

Author Comment

by:MICHAED
Comment Utility
>>>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
Comment Utility
I tried it and it worked ok for me...(Access 2002). What happens if you put the document in another folder?
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

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

I get the same error message.
0
 
LVL 46

Expert Comment

by:tbsgadi
Comment Utility
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
Comment Utility
>>>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
Comment Utility
> 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
Comment Utility
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
Comment Utility
0
 

Author Comment

by:MICHAED
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Never mind....I will award points and post as a separate question.

Ed
0
 
LVL 46

Expert Comment

by:tbsgadi
Comment Utility
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

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

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…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now