我们正在将 ADP 项目转换为 ACCDB,因为 Office 2013 版本不再支持 ADP。
我有以下代码来更改ADP中的应用程序连接:
Function ChangeADPConnection(strServerName As String, strDBName As _
String, Optional strUN As String, Optional strPW As String) As Boolean
Dim strConnect As String
On Error GoTo EH:
Application.CurrentProject.CloseConnection
'The Provider, Data Source, and Initial Catalog arguments are required.
strConnect = "Provider=SQLOLEDB.1" & _
";Data Source=" & strServerName & _
";Initial Catalog=" & strDBName
If strUN <> "" Then
strConnect = strConnect & ";user id=" & strUN
If strPW <> "" Then
strConnect = strConnect & ";password=" & strPW
End If
Else 'Try to use integrated security if no username is supplied.
strConnect = strConnect & ";integrated security=SSPI"
End If
Application.CurrentProject.OpenConnection strConnect
ChangeADPConnection = True
Exit Function
EH:
MsgBox Err.Number & ": " & Err.Description, vbCritical, "Connection Error"
ChangeADPConnection = False
End Function
但是,我不确定如何更改 ACCDB 中的当前项目连接?
我正在使用带有 SQL Server 的链接表作为后端
这是我尝试过的:
Public Function ChangeACCDBConnection(strServerName As String, strDBName As _
String, Optional strUN As String, Optional strPW As String) As Boolean
Dim strConnect As String
Dim cnn As ADODB.Connection
Application.CurrentProject.Connection.Close
On Error GoTo EH:
strConnect = "XXXXX"
Set Con = New ADODB.Connection
Con.ConnectionString = strConnect
CurrentProject.OpenConnection strConnect // Error here
ChangeConnection = True
Exit Function
EH:
MsgBox Err.Number & ": " & Err.Description, vbCritical, "Connection Error"
ChangeConnection = False
End Function
我收到一个错误,更改当前项目连接。连接字符串很好,它已成功连接到数据库。
唯一的事情是如何将该连接设置为 Application.CurrentProject 以便在应用程序中使用它。
非常感谢任何帮助..