Solved

How do I vba copy from 2 Access recordsets and paste on 2 different Excel sheets in the same open workbook?

Posted on 2015-02-10
2
187 Views
Last Modified: 2016-02-10
I don't think this is a tough one; probably not declaring the target sheets right.  Here's my code.  It works, generally, but pastes the same rsResults onto the "Tracking" sheet.  I need rsResults to go on one sheet and rsResults2 to go on another.
Public Sub ProcessSheets()

    Dim sPath As String, xlFile As String
    sPath = "C:\Users\jw51640\Desktop\Pick\Refresh\"
    xlFile = Dir(sPath & "*.xlsm")
    Dim XL As Excel.Application, wbTarget As Workbook
    Dim qdfResults As QueryDef
    Dim rsResults As DAO.Recordset
    Dim qdfResults2 As QueryDef
    Dim rsResults2 As DAO.Recordset
    
    Set XL = New Excel.Application
    Set wbTarget = XL.Workbooks.Open("C:\Users\jw51640\Desktop\Pick\PickListTemplate.xlsm")
    Set qdfResults = CurrentDb.QueryDefs("PickListDataQ")
    Set rsResults = qdfResults.OpenRecordset()
    Set qdfResults2 = CurrentDb.QueryDefs("TrackingQ")
    Set rsResults2 = qdfResults.OpenRecordset()
    
    Debug.Print xlFile
'Import and delete the data file
    DoCmd.SetWarnings False
    DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "ProcessingT", sPath & xlFile, True, "PickList!A2:U5000"
    DoCmd.OpenQuery "DeleteProcessingTBlanksQ", , acReadOnly
    Kill sPath & xlFile
   xlFile = Dir()
'Process
    DoCmd.SetWarnings False
'Updates Archetypes with matching PID
    DoCmd.OpenQuery "UpdatePicksQ", , acReadOnly
    DoCmd.OpenQuery "AppendNewPicksQ", , acReadOnly
    Call Snapshot
    
    XL.Visible = True
    wbTarget.Sheets("PickList").Range("A3").CopyFromRecordSet rsResults
    Set rsResults = Nothing
    XL.Run "SetPickListRowHeight"
    wbTarget.Sheets("Tracking").Range("A1").CopyFromRecordSet rsResults2 'this is where the bug is.
    Set rsResults2 = Nothing
    'XL.Run "SaveSendResults"
    'Set wbTarget = Nothing

'ClearProcessSheets
    DoCmd.RunSQL "DELETE * FROM ProcessingT"
    DoCmd.SetWarnings True
Set db = Nothing

Open in new window

0
Comment
Question by:Jay Williams
[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
2 Comments
 
LVL 50

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 40600508
Hi,

pls correct

    Set rsResults2 = qdfResults.OpenRecordset()

to
    Set rsResults2 = qdfResults2.OpenRecordset()

Regards
0
 

Author Closing Comment

by:Jay Williams
ID: 40600517
That's the ticket!  Thanks!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

740 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