Ado ext for ddl and security (zz)
- //Reference Microsoft ADO Ext x.x for DDL and Security
- ADOX.Catalog cat = new ADOX.Catalog();
- ADOX.Table tbl = new ADOX.Table();
- String linkToDB = "Z:\\Docs\\Test.accdb";
- String linkInDB = "Z:\\Docs\\Test2.accdb";
- String cn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + linkInDB;
- cat.let_ActiveConnection(cn);
- tbl.ParentCatalog = cat;
- tbl.Name = "LinkTableDB";
- tbl.Properties["Jet OLEDB:Remote Table Name"].Value ="Table1";
- tbl.Properties["Jet OLEDB:Link Datasource"].Value = linkToDB;
- tbl.Properties["Jet OLEDB:Link Provider String"].Value ="MS Access";
- tbl.Properties["Jet OLEDB:Create Link"].Value =true;
- cat.Tables.Append(tbl);
Sub CreateLinkedJetTable() Dim cat As ADOX.Catalog Dim tbl As ADOX.Table Set cat = New ADOX.Catalog ' Open the catalog. cat.ActiveConnection = CurrentProject.Connection Set tbl = New ADOX.Table ' Create the new table. tbl.Name = "Linked_Employees" Set tbl.ParentCatalog = cat ' Set the properties to create the link. tbl.Properties("Jet OLEDB:Link Datasource") = "C:\Program Files\Microsoft Office\Office\Samples\northwind.mdb" tbl.Properties("Jet OLEDB:Remote Table Name") = "Employees" tbl.Properties("Jet OLEDB:Create Link") = True ' To link a table with a database password set the Link Provider String ' tbl.Properties("Jet OLEDB:Link Provider String") = "MS Access;PWD=Admin;" ' Append the table to the tables collection. cat.Tables.Append tbl Set cat = Nothing End Sub Sub RefreshLinks() Dim cat As ADOX.Catalog Dim tbl As ADOX.Table Set cat = New ADOX.Catalog ' Open the catalog. cat.ActiveConnection = CurrentProject.Connection Set tbl = New ADOX.Table For Each tbl In cat.Tables ' Verify that the table is a linked table. If tbl.Type = "LINK" Then tbl.Properties("Jet OLEDB:Link Datasource") = "C:\Program Files\Microsoft Office\Office\Samples\northwind.mdb" ' To refresh a linked table with a database password set the Link Provider String 'tbl.Properties("Jet OLEDB:Link Provider String") = "MS Access;PWD=Admin;" End If Next End Sub
For one of my contracts I decided to split the MS Access into two database one to collect the data and one for reporting. The reporting database would linked in all the tables from the data database. This way I can modify the reports email the client the report database and do not need to port any data over to the newer database or provide scripts to update the database.
The problem is that the linked table source database is full hardcoded path. To keep it easy for the clients I needed a way to have the program updated the linked table source database path from the program.
I googled around but was not able to find a solution but did find some leads which led to the following code snippet. The code snippet reset the linked tables source database path.
//z 2012-08-16 16:26:24 IS2120@CSDN.T2815507050[T7,L256,R11,V329]
- ADODB.Connection Con = new ADODB.Connection();
- ADOX.Catalog Cat = new ADOX.Catalog();
- Con.Open(connectionString, null, null, 0);
- Cat.ActiveConnection = Con;
- Cat.Tables[LinkedTableName].Properties["Jet OLEDB:Link Datasource"].Value = LinkedDatabaseLocation;
- Con.Close();
VBA
- 'Reference Microsoft ADO Ext x.x for DDL and Security
- Dim cn 'As ADODB.Connection
- Dim ct 'As ADOX.Catalog
- Dim tbl 'As ADOX.Table
- Dim strLinkXL 'As String
- Dim strLinkMDB 'As String
- Dim strMDB 'As String
- strLinkXL = "C:\Docs\LTD.xls"
- strLinkMDB = "C:\Docs\db1.mdb"
- strMDB = "C:\Docs\LTD.mdb"
- 'Create Link...
- Set cn = CreateObject("ADODB.Connection")
- cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
- "Data Source=" & strMDB & ";" & _
- "Persist Security Info=False"
- Set ct = CreateObject("ADOX.Catalog")
- Set ct.ActiveConnection = cn
- Set tbl = CreateObject("ADOX.Table")
- Set tbl.ParentCatalog = ct
- '1. Link MDB
- With tbl
- 'What the link table will be called
- .Name = "LinkTableMDB"
- 'Name of the table to link
- .properties("Jet OLEDB:Remote Table Name") = "Table1"
- .properties("Jet OLEDB:Link Datasource") = strLinkMDB
- .properties("Jet OLEDB:Link Provider String") = "MS Access"
- .properties("Jet OLEDB:Create Link") = True
- End With
- 'Append the table to the tables collection
- ct.Tables.Append tbl
- Set tbl = Nothing
- '2. Link Excel using named range
- Set tbl = CreateObject("ADOX.Table")
- Set tbl.ParentCatalog = ct
- With tbl
- .Name = "LinkTableXLRange"
- .properties("Jet OLEDB:Link Provider String") = "Excel 8.0;DATABASE=" & strLinkXL & ";HDR=Yes"
- 'The named range
- .properties("Jet OLEDB:Remote Table Name") = "Data_Range"
- .properties("Jet OLEDB:Create Link") = True
- End With
- 'Append the table to the tables collection
- ct.Tables.Append tbl
- Set tbl = Nothing
- '3. Link Excel by sheet name
- Set tbl = CreateObject("ADOX.Table")
- Set tbl.ParentCatalog = ct
- With tbl
- .Name = "LinkTableXLSheet"
- .properties("Jet OLEDB:Link Provider String") = "Excel 8.0;DATABASE=" & strLinkXL & ";HDR=Yes"
- 'Note the use of $, it is necessary
- .properties("Jet OLEDB:Remote Table Name") = "Sheet2$"
- .properties("Jet OLEDB:Create Link") = True
- End With
- 'Append the table to the tables collection
- ct.Tables.Append tbl
- Set tbl = Nothing
@IS2120#CNBLOGS.T2169364049[T1,L65,R1,V259]:备忘
$ € ₤ ₭ ₪ ₩ ₮ ₦ ₱ ฿ ₡ ₫ ﷼ ¥ ﷼ ₫ ₡ ฿ ₱ ₦ ₮ ₩ ₪ ₭ ₤ € $