Applies To Connection object, Database object, QueryDef object, TableDef object.
Description
Sets or returns a value that provides information about the source of an open connection, an open database, a database used in a pass-through query, or a linked table. For Database objects, new Connection objects, linked tables, and TableDef objects not yet appended to a collection, this property setting is read/write. For QueryDef objects and base tables, this property is read-only.
Syntax object.Connect = databasetype;parameters; The Connect property syntax has these parts.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 |
Remarks If the specifier is only "ODBC;", the ODBC driver displays a dialog box listing all registered ODBC data source names so that the user can select a database.
If a password is required but not provided in the Connect property setting, a login dialog box is displayed the first time a table is accessed by the ODBC driver and again if the connection is closed and reopened. For data in Microsoft Exchange, the required MAPILEVEL key should be set to a fully-resolved folder path (for example, "Mailbox - Pat SmithIAlpha/Today"). The path does not include the name of the folder that will be opened as a table; that folder's name should instead be specified as the name argument to the CreateTable method. The TABLETYPE key should be set to "0" to open a folder (default) or "1" to open an address book. The PROFILE key defaults to the profile currently in use. For base tables in a Microsoft Jet database (.mdb), the Connect property setting is a zero-length string (" "). You can set the Connect property for a Database object by providing a source argument to the OpenDatabase method. You can check the setting to determine the type, path, user ID, password, or ODBC data source of the database. On a QueryDef object in a Microsoft Jet workspace, you can use the Connect property with the ReturnsRecords property to create an ODBC SQL pass-through query. The databasetype of the connection string is "ODBC;", and the remainder of the string contains information specific to the ODBC driver used to access the remote data. For more information, see the documentation for the specific driver. NotesSub 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