Microsoft KB Archive/141686

= How to Use DAO to Append the Records in Two Tables =

Article ID: 141686

Article Last Modified on 8/17/2005

-

APPLIES TO


 * Microsoft Excel 97 Standard Edition
 * Microsoft Excel 95 Standard Edition

-



This article was previously published under Q141686



SUMMARY
In the versions of Microsoft Excel listed at the beginning of this article, you can use DAO (Data Access Objects) to manipulate data in external databases. This article provides a macro example that demonstrates how you can append the records from two different tables using DAO commands.



MORE INFORMATION
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers 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 requirements. WARNING: Your use or modification of the SQL statement provided in this article is at your own risk. Microsoft provides this SQL statement "as is" without warranty of any kind, either expressed or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose. Microsoft does not support modifications of the SQL statement to suit specific customer requirements.

You can merge the results of two or more queries, tables, and SELECT statements in a single UNION operation. The following example demonstrates how you can use the UNION operation to append two tables:

Sub AppendTables Dim db As Database Dim Results As Recordset Dim i As Integer 'Open the dBASE IV database Set db = OpenDatabase("c:\my documents", False, False, "dBASE IV;") 'Create a recordset that is a union of the January table 'and the February Table Set Results = _ db.OpenRecordset("Select * from January" & _       "UNION Select * from February") 'Return the column headers to the first row of Sheet1 For i = 0 To Results.Fields.Count - 1 Sheets("Sheet1").Range("A1").Offset(, i) = Results.Fields(i).Name Next 'Return the recordset results to Sheet1 Sheets("Sheet1").Range("a2").CopyFromRecordset Results db.Close End Sub If the January and February tables used in the AppendTables macro appear as in the following tables   January.DBF                         February.DBF DATE     QTY     AMOUNT            DATE      QTY     AMOUNT 1/3/95    1      66.99             2/6/95     6      44.91   1/5/95     6      59.11             2/9/95     7      19.13   1/8/95     4       2.80             2/10/95    2      20.56   1/8/95     3      11.12             2/10/95    3      69.94   1/9/95     2      49.71             2/11/95    8      71.67   1/15/95    3      59.76             2/19/95    1      93.61   1/23/95    7      93.32             2/24/95    4      57.76   1/23/95    8      90.96             2/26/95    3      98.13 the results returned to Sheet1 using the macro are as follows   DATE      QTY     AMOUNT 1/3/95    1      66.99   1/5/95     6      59.11   1/8/95     4       2.80   1/8/95     3      11.12   1/9/95     2      49.71   1/15/95    3      59.76   1/23/95    7      93.32   1/23/95    8      90.96   2/6/95     6      44.91   2/9/95     7      19.13   2/10/95    2      20.56   2/10/95    3      69.94   2/11/95    8      71.67   2/19/95    1      93.61   2/24/95    4      57.76   2/26/95    3      98.13 To use the Data Access Object commands in a Microsoft Excel Visual Basic for Applications macro, the DAO library must be referenced. To reference the DAO library use the following steps:
 * 1) Activate the module sheet containing the macro.
 * 2) On the Tools menu, click References.
 * 3) In Microsoft Excel 97, select the Microsoft DAO 3.5 Object Library check box or in Microsoft Excel version 7.0, select the Microsoft DAO 3.0 Object Library check box, and then click OK.

