2010-05-21 127 views
2

我在access 2007中創建了一個模塊來更新鏈接表,但我想從vb6運行這個模塊。我曾嘗試從微軟這個代碼,但它沒有奏效。如何在VB6中運行Access 2007模塊?

 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 
+0

它是如何工作的?沒有這個,任何答案都只是一個猜測。 – Corin 2010-05-21 22:18:38

+0

你有沒有名爲「RefreshLinks」的宏? – 2010-05-22 17:16:49

+0

@ David-W-Fenton經過很長一段時間的搜索之後,我發現我沒有marco,爲了得到marco,我不得不調用這個問題解決的功能 – Mahmoud 2010-05-22 20:37:09

回答

0

這裏的錯誤是,所有的功能都潛艇,使宏觀來看,我不得不要麼做以下

的C一個焊割每個subPublic Function或創建一個調用兩個子例如

public function Refresh() 
    RefreshLinked 
end Function` 

一個函數,然後創建一個Marco和行動尋找運行的代碼,並期待在功能,一切都很好做工精細

謝謝人