将 Excel 工作表链接到 MS Access 中的表 - 使用 VBScript

发布于 2024-09-15 18:34:19 字数 215 浏览 3 评论 0原文

我正在尝试在 MS Access 中创建链接到 Excel 工作表的链接表。我想通过 VBscripting 来完成此操作。

我的场景是我将有一个 Excel 工作表,该工作表会经常更新。但我的脚本从 MSAccess 中的表中获取值,该表应该是 Excel 工作表(链接表)的副本。

所以我想知道 VBscript 中是否有任何代码可以创建一个到 Excel Sheet 的链接表。

I am trying to create a Linked table in MS Access linked to Excel sheet. I want to do this through VBscripting.

My Scenario is I will have a excel sheet which will be updated very often. But my script picks up the values from the table in MSAccess which should be a replica of the Excel sheet (Linked table).

So I want to know if there is any code in VBscript wherein I can create a Linked table to Excel Sheet.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

染年凉城似染瑾 2024-09-22 18:34:19

这是一些示例脚本。

   Dim cn ''As ADODB.Connection
   Dim ct ''As ADOX.Catalog
   Dim tbl ''As ADOX.Table

   Dim strLinkXL ''As String
   Dim strMDB ''As String

   strLinkXL = "C:\Docs\LTD.xls"
   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


   ''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

   ''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

来自:http://wiki.lessthandot.com/index.php/Linking_Tables_via_Jet_and_ADO

Here is some sample script.

   Dim cn ''As ADODB.Connection
   Dim ct ''As ADOX.Catalog
   Dim tbl ''As ADOX.Table

   Dim strLinkXL ''As String
   Dim strMDB ''As String

   strLinkXL = "C:\Docs\LTD.xls"
   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


   ''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

   ''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

From: http://wiki.lessthandot.com/index.php/Linking_Tables_via_Jet_and_ADO

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文