Article ID: 132970
Article Last Modified on 10/11/2006
Microsoft Excel 7.0
C:\Program Files\Common Files\Microsoft Shared\MSQuery
Microsoft Excel 97
C:\Program Files\Microsoft Office\Office
A1: Order_ID B1: Custmr_ID C1: Employ_ID D1: Order_Date E1: Order_Amt A2: 88000 B2: WALNG C2: '111 D2: 1/1/97 E2: 111.00 A3: 88001 B3: HIGHG C3: '333 D3: 1/2/97 E3: 222.00NOTE: Include the apostrophe before the numbers in cells C2 and C3 as shown to convert the values to text.
Press ALT+F11 to activate the Visual Basic Editor.
Click Module on the Insert menu.
Click References on the Tools menu.
Select Microsoft DAO 3.5 Object Library and click OK.
Click Module on the Insert menu.
Click References on the Tools menu.
Select the Microsoft DAO 3.0 Object Library and click OK.
Sub JoinTables()
Dim db As database
Dim rs As recordset
Dim OrdersTable As tabledef, EmpTable As tabledef
'Create a temporary Jet database called Temp.MDB
Set db = createdatabase("C:\My Documents\Temp.mdb", dbLangGeneral)
'Attach the Excel table "Orders" from the file Orders.xls to the
'database
Set OrdersTable = db.CreateTableDef("Orders")
OrdersTable.Connect = "Excel 5.0;DATABASE=C:\My Documents\ORDERS.XLS"
OrdersTable.SourceTableName = "Orders"
db.TableDefs.Append OrdersTable
'Attach the dBASE IV table "Employee" to the database
'** Note: You may need to change the path to the sample dBASE
' files for your installation of Excel.
Set EmpTable = db.CreateTableDef("Employee")
EmpTable.Connect = _
"dBASE IV;DATABASE=C:\Program Files\Microsoft Office\Office"
EmpTable.SourceTableName = "Employee"
db.TableDefs.Append EmpTable
'Create the recordset -- Return the Order_ID from the Orders
'table and
'The First_name and Last_Name from the Employee table where the
'Employ_ID in the Employee table matches the Employ_ID in the Orders
'table
Set rs = db.OpenRecordset("SELECT orders.ORDER_ID, " & _
"employee.FIRST_NAME, employee.LAST_NAME FROM employee, orders " & _
"WHERE employee.EMPLOY_ID = orders.EMPLOY_ID", dbOpenDynaset)
'Copy the recordset to Sheet1!A1
Sheets("Sheet1").Range("A1").CopyFromRecordset rs
'Close the database and delete the database file Temp.mdb
db.Close
Kill "c:\my documents\temp.mdb"
End SubAdditional query words: 8.00 97 XL
Keywords: kbdtacode kbhowto kbprogramming KB132970