Sub OpenADPWindowsOrSQLServer(srvname As String, dbname As String, _
prpath As String, prname As String, _
suid As String, pwd As String, bolWindowsLogin As Boolean)
Dim bolLeaveOpen As Boolean
Dim strPrFilePath As String
Dim sConnectionString As String
'' 是否保持现有打开的程序?
If MsgBox("在该过程中是否关闭打开的 ADP?", vbYesNo) = vbYes Then
bolLeaveOpen = True
End If
'' 新建 Access 会话实例 (使用 .9 : Access 2000, .10 : Access 2002)
Set appAccess = CreateObject("Access.Application.9")
'' 使用登录名和口令打开 ADP 并使其可视
strPrFilePath = prpath & prname
appAccess.OpenAccessProject strPrFilePath
appAccess.Visible = True
'' 指定 ADP 使用的新的 Windwos 或 SQL Server 登录方式
If bolWindowsLogin Then
appAccess.CurrentProject.OpenConnection _
"PROVIDER=SQLOLEDB.1;INTEGRATED SECURITY=SSPI;" & _
"PERSIST SECURITY INFO=FALSE;INITIAL CATALOG=" & _
dbname & ";DATA SOURCE=" & srvname
Else
sConnectionString = "PROVIDER=SQLOLEDB.1;INITIAL CATALOG=" & _
dbname & ";DATA SOURCE=" & srvname
appAccess.CurrentProject.OpenConnection _
sConnectionString, _
suid, pwd
End If
'' 按上面提出的要求关闭 Access 会话实例
If bolLeaveOpen = False Then
appAccess.CloseCurrentDatabase
Set appAccess = Nothing
End If
End Sub