sql-server-2008ms-accessconnectionlinked-tables

How to set Application.CurrentProject.Connection in ACCDB project?


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..


Solution

  • As far as I know, in a standard Access database application (not an ADP) you can manipulate your connection(s) in only these ways:

    1. Delete linked tables and recreate those links using code. If you do this correctly you can link/relink to any valid data source. This has no affect though on any ADO connections. Linked tables actually use a combination of DAO and ODBC as the data access layers.
    2. Create an ADO connection object as a global variable in a code module. Write functions to change its connection when you want to switch data sources. (You could also wrap all this in a class.)

    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.