Need VBA syntax to import to a specific sheet a Microsoft Access VBA Recordset?

Need help with exporting recordset to correct worksheet using Access/ Excel VBA:
Please note the following code.

I'm trying to get the recordset on the first worksheet - Sheet1 and it does put the data in there
But I'm trying to skip over to Sheet 3 - and it won't go to the third sheet in the workbook - it instead still - STILL puts in in Sheet 1 over the previous recordset.



Private Sub bSendExcel_Click()

  Dim sFileName As String
  Dim objApp As Excel.Application
  Dim wb As Excel.Workbook
  Dim ws As Excel.Worksheet
  sFileName = txtPath
  
  'CLOSE OUT OF ALL EXCEL APPLICATIONS
  'if open - remind to save and close out

  
  Dim rs As DAO.Recordset
  Set objApp = CreateObject("Excel.Application")
  objApp.Visible = True
  Set wb = objApp.Workbooks.Open(sFileName, True, False)
  Set rs = CurrentDb.OpenRecordset("qMonthlyWorkloadbyAsset_Crosstab")
  Set ws = wb.Sheets(1)
  For Each ws In wb.Worksheets
      With ws
          .Activate
          .Cells(2, 1).CopyFromRecordset rs
      End With
  Next
 
  rs.Close
  Set rs = Nothing

'this is the second recordset... that still puts in Sheet 1 - even though I specify wb.Sheets(3)
  
'  Set rs = CurrentDb.OpenRecordset("_02_Final Hours Worked by Customer")
'  Set ws = wb.Sheets(3)
'  For Each ws In wb.Worksheets
'      With ws
'          .Activate
'          .Cells(2, 1).CopyFromRecordset rs
'      End With
'  Next
'
  wb.Save
  Set wb = Nothing
  objApp.Quit
  Set objApp = Nothing


End Sub

Open in new window

LVL 1
stephenlecomptejrAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

stephenlecomptejrAuthor Commented:
Private Sub bSendExcel_Click()

  Dim sFileName As String
  Dim objApp As Excel.Application
  Dim wb As Excel.Workbook
  Dim ws As Excel.Worksheet
  sFileName = txtPath
  
  'CLOSE OUT OF ALL EXCEL APPLICATIONS
  'if open - remind to save and close out
  
  Dim rs As DAO.Recordset
  Set objApp = CreateObject("Excel.Application")
  objApp.Visible = True
  Set wb = objApp.Workbooks.Open(sFileName, True, False)
  Set rs = CurrentDb.OpenRecordset("qMonthlyWorkloadbyAsset_Crosstab")
  Set ws = wb.Sheets(1)
  ws.Activate
  ws.Cells(2, 1).CopyFromRecordset rs
  rs.Close
  Set rs = Nothing

  Set rs = CurrentDb.OpenRecordset("_02_Final Hours Worked by Customer")
  Set ws = wb.Sheets(3)
  ws.Activate
  ws.Cells(2, 1).CopyFromRecordset rs
  wb.Save
  Set wb = Nothing
  objApp.Quit
  Set objApp = Nothing


End Sub

Open in new window

stephenlecomptejrAuthor Commented:
sorry don't know how to delete question.  But there is the answer for me above.  My bad!!
stephenlecomptejrAuthor Commented:
Private Sub bSendExcel_Click()

  Dim sFileName As String
  Dim objApp As Excel.Application
  Dim wb As Excel.Workbook
  Dim ws As Excel.Worksheet
  sFileName = txtPath
  
  'CLOSE OUT OF ALL EXCEL APPLICATIONS
  'if open - remind to save and close out
  
  Dim rs As DAO.Recordset
  Set objApp = CreateObject("Excel.Application")
  objApp.Visible = True
  Set wb = objApp.Workbooks.Open(sFileName, True, False)
  Set rs = CurrentDb.OpenRecordset("qMonthlyWorkloadbyAsset_Crosstab")
  Set ws = wb.Sheets(1)
  ws.Activate
  ws.Cells(2, 1).CopyFromRecordset rs
  rs.Close
  Set rs = Nothing

  Set rs = CurrentDb.OpenRecordset("_02_Final Hours Worked by Customer")
  Set ws = wb.Sheets(3)
  ws.Activate
  ws.Cells(2, 1).CopyFromRecordset rs
  wb.Save
  Set wb = Nothing
  objApp.Quit
  Set objApp = Nothing


End Sub

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dale FyeOwner, Dev-Soln LLCCommented:
I'm not sure how to delete a post any more either.

You should be able to select one of your posts as the solution.

Look at the post with the code that works, and in the upper right corner of that post you should see some blue text that says "select my solution" (or something like that).  After clicking on that, I believe you will see a button popup at the bottom of that post that allows you to select your post.

HTH
Dale
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.