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
178 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
2 Comments
 
LVL 48

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
Modern/Metro styled message box and input box that directly can replace MsgBox() and InputBox()in Microsoft Access 2013 and later. Also included is a preconfigured error box to be used in error handling.
Familiarize people with the process of utilizing SQL Server functions 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 Ac…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

757 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

17 Experts available now in Live!

Get 1:1 Help Now