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

stephenlecomptejr
stephenlecomptejr used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

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

Author

Commented:
sorry don't know how to delete question.  But there is the answer for me above.  My bad!!
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

Dale FyeOwner, Dev-Soln LLC
Most Valuable Expert 2014
Top Expert 2010

Commented:
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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial