Working with Spreadsheets

With Microsoft Access and installable ISAM drivers, you can link or open spreadsheets and manipulate information in them as if they were database tables. The procedures for linking spreadsheets and opening them directly are very similar to the procedures for linking and opening external databases. The following sections explain how to use DAO to access data in Microsoft Excel.

Note   The procedure for accessing the data in Lotus 1-2-3 spreadsheet files (versions WK1, WK3, or WKS) is similar to the procedure used to access data in Microsoft Excel. For information on special considerations for using data in Lotus 1-2-3 spreadsheets, search the Help index for “Lotus 1-2-3.”

Working with Microsoft Excel Worksheets and Workbooks

The Microsoft Excel installable ISAM supports the following single-sheet worksheet and multiple-sheet workbook versions of Microsoft Excel: Microsoft Excel versions 3.0 and 4.0 for single-sheet worksheets, and Microsoft Excel versions 5.0, 7.0, and 8.0 for multiple-sheet workbooks. There are a few operations that you cannot perform on Microsoft Excel worksheets or workbooks through the Microsoft Excel installable ISAM:

Specifying Sheets and Ranges in Microsoft Excel Worksheet and Workbook Files

You can specify a subset of the available data when you first link or open a Microsoft Excel worksheet or workbook in Visual Basic code. In worksheet files, you can link or open the entire sheet, a named range of cells, or an unnamed range of cells. In a workbook file, you can link or open a single worksheet, a named range anywhere in the workbook, or an unnamed range in a single worksheet.

When you link data from a Microsoft Excel worksheet, you must set the Connect property to a connection string that includes the DATABASE argument; you must also set the SourceTableName property to the name of the worksheet or range of cells you are accessing. When you open a worksheet or range of cells directly, you set the connection string in the dbname argument of the OpenDatabase method and you specify the name of the worksheet or range of cells in the source argument of the OpenRecordset method. The following table lists the conventions for these settings.

To link or open this object In this version of Microsoft Excel Use this syntax
Entire sheet in a worksheet file 3.0 and 4.0 Set the DATABASE or dbname argument to specify the fully-qualified path to the worksheet file; set the SourceTableName property or the source argument to specify the sheet as filename#xls, where filename is the name of the worksheet.
Entire worksheet in a workbook file 5.0, 7.0, and 8.0 Set the DATABASE or dbname argument to specify the fully-qualified path to the workbook file, including the workbook file name; set the SourceTableName property or the source argument to specify the sheet as sheetname$, where sheetname is the name of the worksheet.

Important You must follow the worksheet name with a dollar sign ($).

Named range of cells in a worksheet or workbook file 3.0, 4.0, 5.0, 7.0, and 8.0 Set the DATABASE or dbname argument to specify the fully-qualified path to the worksheet or workbook file, including the worksheet or workbook file name; set the SourceTableName property or the source argument to specify the named range as NamedRange, where NamedRange is the name you assigned to the range in Microsoft Excel.

Important You must name the range in Microsoft Excel before attempting to link or open it.

Unnamed range of cells in a worksheet file 3.0 and 4.0 Set the DATABASE or dbname argument to specify the fully-qualified path to the worksheet file, including the worksheet file name; set the SourceTableName property or the source argument to specify the range as FirstCellInRange:LastCellInRange. For example, to access cells A1 through Z256, you would set the SourceTableName property or the source argument to A1:Z256.
Unnamed range of cells in a single worksheet in a workbook file 5.0, 7.0, and 8.0 Set the DATABASE or dbname argument to specify the fully-qualified path to the workbook file, including the workbook file name; set the SourceTableName property or the source argument to specify the sheet you want to link or open as sheetname$ and the range as FirstCellInRange:LastCellInRange. For example, to access cells A1 through Z256 in a worksheet called Sales, you would set the SourceTableName property or the source argument to Sales$A1:Z256.

Note   You cannot specify a value in a range that exceeds the maximum number of rows, columns, or sheets for the worksheet or workbook. For more information on these values, see your Microsoft Excel documentation.

Linking Microsoft Excel Worksheets

To link data from a Microsoft Excel worksheet to your Microsoft Access database, use the CurrentDb function to return an object variable that represents the current Microsoft Access database. Then create a TableDef object and set the connection string and source table name: set the TableDef object’s Connect property to specify the source database type and the full path to the Microsoft Excel worksheet or workbook, and set its SourceTableName property to the name of the worksheet or range of cells.

The following example links a Microsoft Excel version 5.0 worksheet named January Sales in a workbook named Q1Sales.xls located in the Sales folder to a Microsoft Access database:

Sub LinkExcelSheet()
	Dim dbs As Database
	Dim tdf As TableDef
	Dim rstSales As Recordset

	' Open the Microsoft Access database.
	Set dbs = CurrentDb

	' Create a TableDef object.
	Set tdf = dbs.CreateTableDef("Linked Microsoft Excel Worksheet")

	' Set the connection string to specify the source database type and the
	' path to the workbook that contains the data you want to link.
	tdf.Connect = "Excel 5.0;DATABASE=C:\Sales\Q1Sales.xls"

	' Set the SourceTableName property to the worksheet you want to access.
	tdf.SourceTableName = "January Sales$"

	' Append the TableDef object to the TableDefs collection to create a link.
	dbs.TableDefs.Append tdf

	' Create a Recordset object from the linked Microsoft Excel worksheet.
	Set rstSales = dbs.OpenRecordset("Linked Microsoft Excel Worksheet")
End Sub

The following code shows the connection information you would use to link a range of cells named ThirdQuarter in a Microsoft Excel version 8.0 worksheet called Sales.xls which is located on C:\Excel\Examples to a Microsoft Access database.

tdfThirdQtr.Connect = "Excel 8.0; DATABASE=C:\Excel\Examples\Sales.xls"
tdfThirdQtr.SourceTableName = "ThirdQuarter"

Tip Instead of specifying the connection string and source table name by setting properties of the TableDef object, you can specify them by using the connect and source arguments of the CreateTableDef method. You can also use the Microsoft Access user interface to link external data. For more information on using the CreateTableDef method or the user interface to link external data, see “Linking External Tables” earlier in this chapter.

Opening Microsoft Excel Worksheets

To open a Microsoft Excel worksheet directly from your Microsoft Access database, create a Database object and set the arguments of the OpenDatabase method to specify the full path to the worksheet or workbook, whether to open the worksheet or workbook exclusively, whether to open it with read/write or read-only permissions, and the source database type. Specify the name of the worksheet or range of cells as the source argument of the OpenRecordset method.

The following example opens a Microsoft Excel version 4.0 worksheet named Data.xls directly from a Microsoft Access database, creates a Recordset object from the worksheet, and counts the number of records that appear in the Recordset object.

Sub OpenExcel4Sheet()
	Dim dbs As Database
	Dim rst As Recordset
	Dim intNumRecords As Integer

	' Open the Microsoft Excel worksheet named Data.xls without exclusive access
	' and with read/write permissions. 
	Set dbs = OpenDatabase("C:\Excel\Data.xls",False, False, "Excel 4.0;HDR=No;")

	' Create a Recordset object from the worksheet Data.xls. The number sign
	' character (#) tells the installable ISAM that the code is referencing
	' the entire worksheet.
	Set rst = dbs.OpenRecordset("Data#xls")

	' Use the MoveLast method to populate the Recordset; use the RecordCount 
	' property to count the records and display a message that gives the results.
	rst.MoveLast
	intNumRecords = rst.RecordCount
	MsgBox "There are " & intNumRecords & " rows in this worksheet."
	rst.Close
End Sub

This next example opens a worksheet named SampleSheet in a Microsoft Excel version 7.0 workbook named Data.xls, creates a Recordset object from the worksheet, and counts the number of records that appear in the Recordset object.

Note   Use the source database type Excel 5.0 in the connection string for both Microsoft Excel version 5.0 and Microsoft Excel version 7.0. Excel 7.0 isn’t a valid source database type.

Sub OpenExcel7Sheet()
	Dim dbs As Database
	Dim rst As Recordset
	Dim intNumRecords As Integer

	' Open the Microsoft Excel workbook named Data.xls without exclusive access
	' and with read/write permissions.
	Set dbs = OpenDatabase("C:\Excel\Data.xls",False, False, "Excel 5.0;HDR=No;")

	' Create a Recordset from the worksheet. The dollar sign character following 
	' the sheet name tells the installable ISAM that the code is referencing the
	' entire sheet.
	Set rst = dbs.OpenRecordset("SampleSheet$")

	' Use the MoveLast method to populate the Recordset; use the RecordCount
	' property to count the records and display a message that gives the results.
	rst.MoveLast
	intNumRecords = rst.RecordCount
	MsgBox "There are " & intNumRecords & " rows in this worksheet."
	rst.Close
End Sub

Note Microsoft Excel version 8.0 supports the ability to create files that contain formatting information for both Microsoft Excel version 7.0 and Microsoft Excel version 8.0. You can create files that can be opened by either Microsoft Excel version 7.0 or Microsoft Excel version 8.0 without having to convert the file format. You can access these files only in read-only mode from Microsoft Access.

Using the HDR Argument to Suppress Headers

Both Microsoft Excel and Lotus 1-2-3 include the HDR argument in the connection string. The HDR argument determines whether the first row of the range to be opened should be treated as a record or as field names when the Microsoft Access table is created. The following table describes the settings for the HDR argument.

HDR argument setting Result
Yes Data in the first row of a spreadsheet range is treated as field names.
No Data in the first row of a spreadsheet range is treated as a record.

For Microsoft Excel worksheets and workbooks, and Lotus 1-2-3 spreadsheets, this behavior is managed by the FirstRowHasNames setting in the Windows Registry. You can find this setting in the \HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft \Jet \3.5\Engines\Lotus key for Lotus 1-2-3 and in the \HKEY_LOCAL_MACHINE \SOFTWARE\Microsoft\Jet\3.5\Engines\Excel key for Microsoft Excel. The default value of the FirstRowHasNames setting is 01 (Yes), which specifies that the first row in any range is treated as if it contains field name information.

You can override this Windows Registry setting on a range-by-range basis by setting the HDR argument in the connection string. For example, in the code in the preceding section, the HDR argument is set to No, which specifies that the first row of the worksheet is treated as a record in the table, not as a header containing field names. If the HDR argument were set to Yes in this example, the total records in the recordset wouldn’t include the first record, and the variable intNumRecords would be one less than its value when the HDR argument is set to No.