会员登录 - 用户注册 - 网站地图 Office中国(office-cn.net),专业Office论坛
当前位置:主页 > 技巧 > Access技巧 > 数据库系统 > 正文

如何用代码链接带有密码的数据库中的表

时间:2008-10-21 16:17 来源:access faq 作者:朱亦文 阅读:
将下列代码粘贴到一个新模块中,并通过使用adoLinkTable函数来实现。

'=======================================================================
' 函    数:adoLinkTable(strMdbPath, strPWD, strLinkName, strTblName)
' 参数说明:strMdbPath 为要链接的数据库的路径
'           strPWD 为打开数据库的密码
'           strLinkName 为链接表名称
'           strTblName 为链接的表的名称
' 返    回:True 成功,False 失败
' 调用举例:
'     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
        ' 数据库的路径和名称
        .Properties("Jet OLEDB:Link Datasource") = strMdbPath
        ' 提供者及密码
        .Properties("Jet OLEDB:Link Provider String") = "MS access;PWD=" & strPWD & ";"
        ' 原数据库中的表
        .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"

(责任编辑:admin)

顶一下
(0)
0%
踩一下
(0)
0%
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价: