【问题标题】:How run an Access 2007 module in VB6?如何在 VB6 中运行 Access 2007 模块?
【发布时间】:2011-02-22 13:52:18
【问题描述】:

我在 access 2007 中创建了一个用于更新链接表的模块,但我想从 vb6 运行这个模块。我试过微软的这段代码,但没有用。

      Sub AccessTest1()
      Dim A As Object

      Set A = CreateObject("Access.Application")
      A.Visible = False

      A.OpenCurrentDatabase (App.Path & "/DataBase/acc.accdb")

      A.DoCmd.RunMacro "RefreshLinks"

   End Sub

我的目标是让我的程序将所有链接表更新为新链接,以防该程序已在其他计算机上使用

如果你想看一下模块程序,这里是:

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 = "Companies"
Set tbl.ParentCatalog = cat

' Set the properties to create the link.
tbl.Properties("Jet OLEDB:Link Datasource") = CurrentProject.Path & "/db3.mdb"
tbl.Properties("Jet OLEDB:Remote Table Name") = "Companies"
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") = CurrentProject.Path & "/db3.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

【问题讨论】:

  • 怎么没用?没有这个,任何答案都只是猜测。
  • 你有一个名为“RefreshLinks”的宏吗?
  • @David-W-Fenton 经过长时间的搜索,我发现我没有马可,为了得到马可,我不得不调用这个问题解决的函数

标签: ms-access vba vb6 ms-access-2007


【解决方案1】:

这里的错误是所有函数都是子函数,要使宏运行我必须执行以下操作之一

将每个 sub 更改为 Public Function 或创建一个调用两个子示例的函数

public function Refresh()
  RefreshLinked
end Function`

然后创建一个 Marco 并在 Action 中查找 Run 代码,查找该函数,一切正常

谢谢大家

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多