Microsoft KB Archive/213415

= XL2000: CopyFromRecordset Method Does Not Return Field Headings =

Article ID: 213415

Article Last Modified on 11/23/2006

-

APPLIES TO


 * Microsoft Excel 2000 Standard Edition

-



This article was previously published under Q213415







SUMMARY
In Microsoft Visual Basic for Applications, the CopyFromRecordset method is used to return data from a Data Access Object (DAO). It returns only data, and does not return the database's field headings.



MORE INFORMATION
The CopyFromRecordset method is used to return data from an open database to an active worksheet. The method does not have an argument that returns the field headings of the database. Additional code is needed to place the field headings on the worksheet. The following sample code returns the field headings as well as the data.

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact a Microsoft Certified Partner or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Partners, please visit the following Microsoft Web site:

https://partner.microsoft.com/global/30000104

For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:

http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS

NOTE: Before you type the following code in a Visual Basic for Applications module, you must create a reference to the Microsoft DAO Object Library. To do this, open a blank module, click References on the Tools menu, and click to select the Microsoft DAO 3.6 Object Library check box. Sub Returning_Field_Headers_Example Dim db As database Dim rs As Recordset Dim vaTmp As String Dim vaNew As Variant Dim dbLocation As String

' Opens the database and creates a record set ' dbLocation could be any directory where the Access files are ' stored.

dbLocation = "c:\Program Files\Microsoft " _ & "Office\Office\Samples\Northwind.mdb" ' Modify the drive and path for the sample database Northwind.mdb ' if different from the default. Set db = DBEngine.Workspaces(0).OpenDatabase(dbLocation) Set rs = db.OpenRecordset("Orders")

' This section fills in the field names from the Orders table. ReDim vaTmp(rs.Fields.Count) For x = 0 To rs.Fields.Count - 1 vaTmp(x + 1) = rs.Fields(x).Name

Next Sheets("Sheet1").Cells(1, 1).Resize(1, rs.Fields.Count) = vaTmp

' Retrieves the data to the sheet. The sheet should be called ' "Sheet1" numberOfRows = Sheets("Sheet1").Cells(2, 1).CopyFromRecordset(rs) Sheets("Sheet1").Activate End Sub

