Go Premium for a chance to win a PS4. Enter to Win

x
?
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
Medium Priority
?
208 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 53

Accepted Solution

by:
Rgonzo1971 earned 2000 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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

885 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