Part | Description |
object | An object expression that evaluates to an object in the Applies To list. |
databasetype | Optional. A String that specifies a database type. For Microsoft Jet databases, exclude this argument; if you specify parameters, use a semicolon (;) as a placeholder. |
parameters | Optional. A String that specifies additional parameters to pass to ODBC or installable ISAM drivers. Use semicolons to separate parameters. |
Database type | Specifier | Example |
Microsoft Jet Database | [database]; | drive:\path\filename.mdb |
dBASE III | dBASE III; | drive:\path |
dBASE IV | dBASE IV; | drive:\path |
dBASE 5 | dBASE 5.0; | drive:\path |
Paradox 3.x | Paradox 3.x; | drive:\path |
Paradox 4.x | Paradox 4.x; | drive:\path |
Paradox 5.x | Paradox 5.x; | drive:\path |
FoxPro 2.0 | FoxPro 2.0; | drive:\path |
FoxPro 2.5 | FoxPro 2.5; | drive:\path |
FoxPro 2.6 | FoxPro 2.6; | drive:\path |
Excel 3.0 | Excel 3.0; | drive:\path\filename.xls |
Excel 4.0 | Excel 4.0; | drive:\path\filename.xls |
Excel 5.0 or Excel 95 | Excel 5.0; | drive:\path\filename.xls |
Excel 97 | Excel 97; | drive:\path\filename.xls |
HTML Import | HTML Import; | drive:\path\filename |
HTML Export | HTML Export; | drive:\path |
Text | Text; | drive:\path |
ODBC | ODBC; DATABASE=database; UID=user; PWD=password; DSN= datasourcename; [LOGINTIMEOUT=seconds;] | None |
Exchange | Exchange; MAPILEVEL=folderpath; [TABLETYPE={ 0 | 1 }];[PROFILE=profile;] [PWD=password;] [DATABASE=database;] | drive:\path\filename.mdb |
Sub ConnectX()
Dim dbsTemp As Database
Dim strMenu As String
Dim strInput As String
' Open a Microsoft Jet database to which you will link
' a table.
Set dbsTemp = OpenDatabase("DB1.mdb")
' Build menu text.
strMenu = "Enter number for data source:" & vbCr
strMenu = strMenu & _
" 1. Microsoft Jet database" & vbCr
strMenu = strMenu & _
" 2. Microsoft FoxPro 3.0 table" & vbCr
strMenu = strMenu & _
" 3. dBASE table" & vbCr
strMenu = strMenu & _
" 4. Paradox table" & vbCr
strMenu = strMenu & _
" M. (see choices 5-9)"
' Get user's choice.
strInput = InputBox(strMenu)
If UCase(strInput) = "M" Then
' Build menu text.
strMenu = "Enter number for data source:" & vbCr
strMenu = strMenu & _
" 5. Microsoft Excel spreadsheet" & vbCr
strMenu = strMenu & _
" 6. Lotus spreadsheet" & vbCr
strMenu = strMenu & _
" 7. Comma-delimited text (CSV)" & vbCr
strMenu = strMenu & _
" 8. HTML table" & vbCr
strMenu = strMenu & _
" 9. Microsoft Exchange folder"
' Get user's choice.
strInput = InputBox(strMenu)
End If
' Call the ConnectOutput procedure. The third argument
' will be used as the Connect string, and the fourth
' argument will be used as the SourceTableName.
Select Case Val(strInput)
Case 1
ConnectOutput dbsTemp, _
"JetTable", _
";DATABASE=C:\My Documents\Northwind.mdb", _
"Employees"
Case 2
ConnectOutput dbsTemp, _
"FoxProTable", _
"FoxPro 3.0;DATABASE=C:\FoxPro30\Samples", _
"Q1Sales"
Case 3
ConnectOutput dbsTemp, _
"dBASETable", _
"dBase IV;DATABASE=C:\dBASE\Samples", _
"Accounts"
Case 4
ConnectOutput dbsTemp, _
"ParadoxTable", _
"Paradox 3.X;DATABASE=C:\Paradox\Samples", _
"Accounts"
Case 5
ConnectOutput dbsTemp, _
"ExcelTable", _
"Excel 5.0;" & _
"DATABASE=C:\Excel\Samples\Q1Sales.xls", _
"January Sales"
Case 6
ConnectOutput dbsTemp, _
"LotusTable", _
"Lotus WK3;" & _
"DATABASE=C:\Lotus\Samples\Sales.xls", _
"THIRDQTR"
Case 7
ConnectOutput dbsTemp, _
"CSVTable", _
"Text;DATABASE=C:\Samples", _
"Sample.txt"
Case 8
ConnectOutput dbsTemp, _
"HTMLTable", _
"HTML Import;DATABASE=http://" & _
"www.server1.com/samples/page1.html", _
"Q1SalesData"
Case 9
ConnectOutput dbsTemp, _
"ExchangeTable", _
"Exchange 4.0;MAPILEVEL=" & _
"Mailbox - Michelle Wortman (Exchange)" & _
"|People\Important;", _
"Jerry Wheeler"
End Select
dbsTemp.Close
End Sub
Sub ConnectOutput(dbsTemp As Database, _
strTable As String, strConnect As String, _
strSourceTable As String)
Dim tdfLinked As TableDef
Dim rstLinked As Recordset
Dim intTemp As Integer
' Create a new TableDef, set its Connect and
' SourceTableName properties based on the passed
' arguments, and append it to the TableDefs collection.
Set tdfLinked = dbsTemp.CreateTableDef(strTable)
tdfLinked.Connect = strConnect
tdfLinked.SourceTableName = strSourceTable
dbsTemp.TableDefs.Append tdfLinked
Set rstLinked = dbsTemp.OpenRecordset(strTable)
Debug.Print "Data from linked table:"
' Display the first three records of the linked table.
intTemp = 1
With rstLinked
Do While Not .EOF And intTemp <= 3
Debug.Print , .Fields(0), .Fields(1)
intTemp = intTemp + 1
.MoveNext
Loop
If Not .EOF Then Debug.Print , "[additional records]"
.Close
End With
' Delete the linked table because this is a demonstration.
dbsTemp.TableDefs.Delete strTable
End Sub
Example (Microsoft Access)
The following example creates a TableDef object in the specified database. The procedure then sets its Connect and SourceTableName properties and appends the object to the TableDefs collection.
Sub ConnectSource()
Dim dbs As Database, tdf As TableDef
' Return reference to current database.
Set dbs = CurrentDb
' Create new TableDef object.
Set tdf = dbs.CreateTableDef("PDXAuthor")
' Attach Paradox table Author in database C:\PDX\Publish.
tdf.Connect = "Paradox 4.X;Database=C:\PDX\Publish"
tdf.SourceTableName = "Author"
dbs.TableDefs.Append tdf
Set dbs = Nothing
End Sub
Example (Microsoft Excel)
This example attaches the table Product.dbf (a dBASE IV table located in the C:\Program Files\Common Files\Microsoft Shared\MSquery folder) to the Nwindex.mdb database. (In Windows NT, Product.dbf is located in the C:\Windows\Msapps\Msquery folder.)
To create the Nwindex.mdb database, run the Microsoft Excel example for the CreateDatabase method.
Dim nWindEx As Database, tDef As TableDef
Dim dataSource As String
dataSource = _
"dBASE IV;DATABASE=C:\Program Files\Common Files\Microsoft _
Shared\MSquery"
Set nWindEx = Workspaces(0).OpenDatabase(Application.Path _
& "\NWINDEX.MDB")
Set tDef = nWindEx.CreateTableDef("Product")
tDef.Connect = dataSource
tDef.SourceTableName = "Product"
nWindEx.TableDefs.Append tDef
nWindEx.Close