設為首頁收藏本站Access中國

Office中國論壇/Access中國論壇

 找回密碼
 注冊

QQ登錄

只需一步,快速開始

如何用代碼鏈接帶有密碼的數(shù)據(jù)庫中的表

1970-1-1 08:00| 發(fā)布者: 朱亦文| 查看: 3249| 評論: 0

將下列代碼粘貼到一個新模塊中,并通過使用adoLinkTable函數(shù)來實現(xiàn)。

'=======================================================================
' 函    數(shù):adoLinkTable(strMdbPath, strPWD, strLinkName, strTblName)
' 參數(shù)說明:strMdbPath 為要鏈接的數(shù)據(jù)庫的路徑
'           strPWD 為打開數(shù)據(jù)庫的密碼
'           strLinkName 為鏈接表名稱
'           strTblName 為鏈接的表的名稱
' 返    回:True 成功,F(xiàn)alse 失敗
' 調(diào)用舉例:
'     OK = adoLinkTable("c:\123.mdb", "123", "訂單", "訂單")
' 作者:朱亦文
' 日期:2002.11.21
'=======================================================================

Public Function adoLinkTable(ByVal strMdbPath As String, _
                            ByVal strPWD As String, _
                            ByVal strLinkName As String, _
                            ByVal strTblName As String) As Boolean

    On Error Resume Goto errh

    Dim catDB As ADOX.Catalog
    Dim tblLink As ADOX.Table

    Set catDB = New ADOX.Catalog
    catDB.ActiveConnection = CurrentProject.Connection

    ' 建立一個新的表對象
    Set tblLink = New ADOX.Table

    With tblLink
        ' 鏈接表名稱
        .Name = strLinkName
        Set .ParentCatalog = catDB

        .Properties("Jet OLEDB:Create Link") = True
        ' 數(shù)據(jù)庫的路徑和名稱
        .Properties("Jet OLEDB:Link Datasource") = strMdbPath
        ' 提供者及密碼
        .Properties("Jet OLEDB:Link Provider String") = "MS Access;PWD=" & strPWD & ";"
        ' 原數(shù)據(jù)庫中的表
        .Properties("Jet OLEDB:Remote Table Name") = strTblName
    End With

    catDB.Tables.Append tblLink        ' 添加到庫中
    Set tblLink = Nothing

    adoLinkTable = True
    Exit Function

errh:
    adoLinkTable = False
End Function

注:在VBA編輯器中引用"Microsoft ADO Ext. 2.5 for DDL and Security"以及"Microsoft ActiveX Data Objects 2.1/2.5/2.6/2.7 Library"

最新評論

QQ|站長郵箱|小黑屋|手機版|Office中國/Access中國 ( 粵ICP備10043721號-1 )  

GMT+8, 2025-7-17 05:49 , Processed in 0.088622 second(s), 16 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

返回頂部