Article ID: 173748
Article Last Modified on 1/20/2007
A1: First B1: Last C1: Middle.
A2: Adam B2: Smith C2: A.
A3: Bob B3: Jones C3: B.
Sub XLLink(strNewAccTable as string, strXLFileName as String, _
strImportSheet as String)
' Variables:
' strNewAccTable - the name of your new linked table.
' strXLFileName - the path and name of your Excel file. This
' should be in the form "C:\MyDir\MyFile.xls."
' strImportSheet - the name of the sheet you want to link.
' All these variable are strings, and should be supplied to the
' subroutine enclosed in quotation marks.
On Error GoTo XLError
Dim db As DATABASE
Dim td As TableDef
Set db = CurrentDb
' Create a new TableDef using the passed name.
Set td = db.CreateTableDef(strNewAccTable)
' Set the ConnectString property to the Excel file to link.
' In Microsoft Access 7.0, the ConnectString needs to reflect the
' version of Excel. Remove the apostrophe from the Excel 5.0
' line and comment out the Excel 8.0 line when working with
' Excel 5.0/95.
' td.Connect = "Excel 5.0;DATABASE=" & strXLFileName & ";"
td.Connect = "Excel 8.0;DATABASE=" & strXLFileName & ";"
td.SourceTableName = strImportSheet & "$"
' Append the new TableDef to the TableDefs collection.
db.TableDefs.Append td
Exit_XLLink:
Exit Sub
XLError:
MsgBox Err.Number & " " & Err.Description
Resume Exit_XLLink
End Sub
XLLink "New Link", "C:\My Documents\LinkTest.xls", "Sheet1"
163435 VBA: Programming Resources for Visual Basic for Applications
Additional query words: wordcon inf vba
Keywords: kbhowto kbprogramming KB173748