Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 612
  • Last Modified:

Need help w/CopyFromRecordset method

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
MICHAED
Asked:
MICHAED
  • 11
  • 7
  • 5
1 Solution
 
shanesuebsahakarnCommented:
Just use TransferSpreadsheet like so:

DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "tblTEST", " C:\Documents and Settings\3765\Desktop\TEST.xls", False, "Sheet1!A4:A4"
0
 
shanesuebsahakarnCommented:
Whoops, tblTEST above should be qselTEST of course.
0
 
MICHAEDAuthor Commented:
Hi Shane,

I tried the above, however, I get "run-time error 3436 -- failure creating file."
0
Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

 
tbsgadiCommented:
Try
DoCmd.TransferSpreadsheet acExport, 8,  "qselTEST", "C:\Documents and Settings\3765\Desktop\TEST.xls", False, "Sheet1!A4:A4"
0
 
MICHAEDAuthor Commented:
I get run-time error 3010 -- table 'sheet1$A4:A4' already exists.
0
 
tbsgadiCommented:
Try
DoCmd.TransferSpreadsheet acExport, 8,  "qselTEST", "C:\Documents and Settings\3765\Desktop\TEST.xls", True

This will put the names in aswell.
0
 
MICHAEDAuthor Commented:
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
 
shanesuebsahakarnCommented:
Try omitting the second parameter entirely:
DoCmd.TransferSpreadsheet acExport, , "qselTEST", " C:\Documents and Settings\3765\Desktop\TEST.xls", False, "Sheet1!A4:A4"
0
 
tbsgadiCommented:
Sorry you can use the "Sheet1!A4:A4" only for importing from Excel not Exporting.
0
 
MICHAEDAuthor Commented:
>>>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
 
shanesuebsahakarnCommented:
I tried it and it worked ok for me...(Access 2002). What happens if you put the document in another folder?
0
 
MICHAEDAuthor Commented:
>>>What happens if you put the document in another folder?<<<

I get the same error message.
0
 
tbsgadiCommented:
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
 
MICHAEDAuthor Commented:
>>>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
 
shanesuebsahakarnCommented:
> If you enter a range, the export will fail.

It works fine in Access 2002, regardless of what the help file says.
0
 
tbsgadiCommented:
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
 
tbsgadiCommented:
0
 
MICHAEDAuthor Commented:
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
 
MICHAEDAuthor Commented:
tbsgadi,

I will try to adapt the code from the link you provided and let you know if it works for me.
0
 
MICHAEDAuthor Commented:
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
 
MICHAEDAuthor Commented:
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
 
MICHAEDAuthor Commented:
Never mind....I will award points and post as a separate question.

Ed
0
 
tbsgadiCommented:
Hi thanks,
I'm not sure of the exact syntax but you want to add a line something like
 .Workbooks.Close
Good Luck!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

  • 11
  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now