如何在VB6中运行Access 2007模块?

时间:2010-05-21 22:13:10

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

我在Access 2007中创建了一个更新链接表的模块,但是我想从vb6运行这个模块。我已经尝试过Microsoft的这段代码,但它没有用。

      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

1 个答案:

答案 0 :(得分:0)

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

将每个sub更改为Public Function或创建一个同时调用子示例

的函数
public function Refresh()
  RefreshLinked
end Function`

然后创建一个Marco并在Action中查找Run代码,并查找该函数,一切运行良好

谢谢大家