By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,183 Members | 1,386 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,183 IT Pros & Developers. It's quick & easy.

Access Table Export to specific excel row

Nathan H
100+
P: 104
I am curious if this is possible to do. I have a case scenario where I need to export data from an access table into an excel spreadsheet, and have that data start on row 5. Can't do it from Excel with a query, as the machine has Access 2007 runtime db and Excel 2003 (which does not recognize the new access extension).

Thanks

Nathan
Dec 13 '07 #1
Share this Question
Share on Google+
10 Replies


ADezii
Expert 5K+
P: 8,692
I am curious if this is possible to do. I have a case scenario where I need to export data from an access table into an excel spreadsheet, and have that data start on row 5. Can't do it from Excel with a query, as the machine has Access 2007 runtime db and Excel 2003 (which does not recognize the new access extension).

Thanks

Nathan
I did it from Excel, but I totally forgot about it recognizing the new 2007 extension. It could be done within Access by using Automation code with similarities to what I am showing below, but it would be more complex.
Expand|Select|Wrap|Line Numbers
  1. Private Sub CommandButton1_Click()
  2. Dim MyDB As DAO.Database, MyRS As DAO.Recordset
  3. Dim wk As Workspace, intCols As Integer, intRow As Integer
  4.  
  5. ' Create Microsoft Jet Workspace object.
  6. Set wk = CreateWorkspace("", "admin", "", dbUseJet)
  7.  
  8. ' Open Database object from saved Microsoft Jet database for exclusive use.
  9. Set MyDB = wk.OpenDatabase("C:\Test\Test.mdb", True)
  10.  
  11. ' Open Recordset based on the Employees Table
  12. Set MyRS = MyDB.OpenRecordset("qryEmployees", dbOpenSnapshot)
  13.  
  14. 'Field Names on Row 5 - then make Bold
  15. For intCols = 0 To MyRS.Fields.Count - 1
  16.   Cells(5, intCols + 1).Value = MyRS.Fields(intCols).Name
  17. Next
  18. Range("A5:Z5").Select: Selection.Font.Bold = True
  19.  
  20. 'Bring the data in starting from Row 6
  21. Worksheets("Sheet1").Activate
  22. Worksheets("Sheet1").Range("A6").CopyFromRecordset MyRS
  23.  
  24. MyRS.Close
  25. Set MyRS = Nothing
  26. End Sub
Dec 13 '07 #2

FishVal
Expert 2.5K+
P: 2,653
I'm very unsure CopyFromRecordset method ever works with DAO.Recordset. Try ADODB.Recordset instead if it doesn't.

Regards,
Fish
Dec 13 '07 #3

Nathan H
100+
P: 104
I'm very unsure CopyFromRecordset method ever works with DAO.Recordset. Try ADODB.Recordset instead if it doesn't.

Regards,
Fish
I found a very simple work around. I export the data to a generic excel spreadsheet, and then have the Template Excel file query the generic, rather than go directly to the database. It adds a step and an extra spreadsheet, but was fairly quick and painless...

Thanks for your input
Dec 13 '07 #4

ADezii
Expert 5K+
P: 8,692
I'm very unsure CopyFromRecordset method ever works with DAO.Recordset. Try ADODB.Recordset instead if it doesn't.

Regards,
Fish
Hello FishVal, the code in Post #2 has been tested and is functional.
Dec 13 '07 #5

FishVal
Expert 2.5K+
P: 2,653
Hello FishVal, the code in Post #2 has been tested and is functional.
Ok. The following code fails with
403: Class does not support Automation or does not support expected interface

XP SP2, Office 2003
Expand|Select|Wrap|Line Numbers
  1. Public Sub ExportDAOfromWS()
  2.  
  3.     Dim appExcel As Excel.Application
  4.     Dim rs As DAO.Recordset
  5.     Dim ws As DAO.Workspace
  6.     Dim db As DAO.Database
  7.  
  8.     Set appExcel = CreateObject("Excel.Application")
  9.     Set ws = CreateWorkspace("", "admin", "", dbUseJet)
  10.     Set db = OpenDatabase("C:\db2.mdb", True)
  11.     Set rs = db.OpenRecordset("t1", dbOpenSnapshot)
  12.  
  13.     With appExcel
  14.         .UserControl = True
  15.         .Visible = True
  16.         With .Workbooks.Add.Worksheets(1)
  17.             .Activate
  18.             .Range("A1").CopyFromRecordset rs      '<------- Fails here
  19.         End With
  20.     End With
  21.  
  22.     rs.Close
  23.  
  24.     Set rs = Nothing
  25.     Set db = Nothing
  26.     Set ws = Nothing
  27.     Set appExcel = Nothing
  28.  
  29. End Sub
  30.  
ADODB.Recordset works just fine.

Oops! Just have noticed. Your code runs in Excel application.
Dec 13 '07 #6

ADezii
Expert 5K+
P: 8,692
Ok. The following code fails with
403: Class does not support Automation or does not support expected interface

XP SP2, Office 2003
Expand|Select|Wrap|Line Numbers
  1. Public Sub ExportDAOfromWS()
  2.  
  3.     Dim appExcel As Excel.Application
  4.     Dim rs As DAO.Recordset
  5.     Dim ws As DAO.Workspace
  6.     Dim db As DAO.Database
  7.  
  8.     Set appExcel = CreateObject("Excel.Application")
  9.     Set ws = CreateWorkspace("", "admin", "", dbUseJet)
  10.     Set db = OpenDatabase("C:\db2.mdb", True)
  11.     Set rs = db.OpenRecordset("t1", dbOpenSnapshot)
  12.  
  13.     With appExcel
  14.         .UserControl = True
  15.         .Visible = True
  16.         With .Workbooks.Add.Worksheets(1)
  17.             .Activate
  18.             .Range("A1").CopyFromRecordset rs      '<------- Fails here
  19.         End With
  20.     End With
  21.  
  22.     rs.Close
  23.  
  24.     Set rs = Nothing
  25.     Set db = Nothing
  26.     Set ws = Nothing
  27.     Set appExcel = Nothing
  28.  
  29. End Sub
  30.  
ADODB.Recordset works just fine.

Oops! Just have noticed. Your code runs in Excel application.
If the t1 Recordset contains an OLE Object Field, you may also encounter the same Error.
Dec 14 '07 #7

FishVal
Expert 2.5K+
P: 2,653
If the t1 Recordset contains an OLE Object Field, you may also encounter the same Error.
[t1] contains numerical, text and boolean fields.
Interesting that CopyFromRecordset method works with DAO.Recordset when code runs in Excel but fails in Access.
Problem with Excel objects automation?
Dec 14 '07 #8

ADezii
Expert 5K+
P: 8,692
[t1] contains numerical, text and boolean fields.
Interesting that CopyFromRecordset method works with DAO.Recordset when code runs in Excel but fails in Access.
Problem with Excel objects automation?
Interesting that CopyFromRecordset method works with DAO.Recordset when code runs in Excel but fails in Access.
Hello FishVal, just thought you wanted to know that the code in the previous Post works flawlessly. If you like, I can send you the Test DB that I used. It just didn't seem right that it would fail within Access.
Dec 14 '07 #9

FishVal
Expert 2.5K+
P: 2,653
Hello FishVal, just thought you wanted to know that the code in the previous Post works flawlessly. If you like, I can send you the Test DB that I used. It just didn't seem right that it would fail within Access.
Oh, I know the code works in Excel via opening Access database in DAO.Workspace.
It fails when CopyFromRecordset method is invoked via automation in Access module with DAO.Recordset as argument. Again, replacing DAO.Recordset with ADODB.Recordset makes it run normally.
I can send you xls and mdb with the code illustrating this phenomenon if you want.
Dec 14 '07 #10

ADezii
Expert 5K+
P: 8,692
Oh, I know the code works in Excel via opening Access database in DAO.Workspace.
It fails when CopyFromRecordset method is invoked via automation in Access module with DAO.Recordset as argument. Again, replacing DAO.Recordset with ADODB.Recordset makes it run normally.
I can send you xls and mdb with the code illustrating this phenomenon if you want.
You really peaked my curiosity on this one, if you could send me the .xls and .mdb, I would really appreciate it. I'll send you mt E-Mail Address in a PM. Thanks, FishVal.
Dec 14 '07 #11

Post your reply

Sign in to post your reply or Sign up for a free account.