we are in the process of converting an ADP project into ACCDB as ADP's are no longer supported in Office 2013 version.
I have the below code to change the application connection in 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
But , I am not sure how to change the current project connection in ACCDB ?
I am using linked tables with SQL Server as backend
Here is what i have tried :
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
I am getting an error ,change the current project connection. The connection string is fine and it is connecting sucessfully to the database.
The only thing is how to set that connection to Application.CurrentProject inorder to use it acrosss the application.
Any help much appreciated..
As far as I know, in a standard Access database application (not an ADP) you can manipulate your connection(s) in only these ways:
I don't think a non-ADP application has a built-in global ADO connection like ADP so you have to use the methods listed above.