Consulting

Results 1 to 8 of 8

Thread: VBA Change ODBC Connection String (Excel)

  1. #1
    VBAX Newbie
    Joined
    Feb 2014
    Posts
    4
    Location

    VBA Change ODBC Connection String (Excel)

    Hi I have a spreadsheet which looks at an ODBC link. The connection string works fine which is

    DSN=xxxx;UID=XX;PWD=xxx;APP=Microsoft Office 2010;WSID=xxx;DATABASE=test;

    The x i have hidden the names. My problem is i want to change the name of the database which is test in this example to another database name eg live.

    Manually i can do this, but i want the user to select the database they want, once they have selected the database the test will change to the database the user has decided.

    I can change the string in code but when i run this it does not change this.

    Can anyone please guide me in the right direction please.

    Thanks

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    What database is it? Doesn't the DSN point at the database, maybe you need multiple DSN.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Newbie
    Joined
    Feb 2014
    Posts
    4
    Location
    Quote Originally Posted by xld View Post
    What database is it? Doesn't the DSN point at the database, maybe you need multiple DSN.
    Hi its a SQL database

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I assume that you mean SQL Server? Did you see my other comment?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    VBAX Newbie
    Joined
    Feb 2014
    Posts
    4
    Location
    Quote Originally Posted by xld View Post
    I assume that you mean SQL Server? Did you see my other comment?
    Yes its SQL Server, in total there could be 15 databases, which the user could connect. The DSN does point to the right database.

    I have tried this code, by changing the connectionstring database to test2 but nothing has been changed.

    Dim con As New ADODB.Connection
    con.ConnectionString = "DSN=xxxx;UID=XX;PWD=xxx;APP=Microsoft Office 2010;WSID=xxx;DATABASE=test2;"


    Thanks

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    ARe you sure that the DSN does not use a specific database name, it is referrring to the server?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    VBAX Newbie
    Joined
    Feb 2014
    Posts
    4
    Location
    Hi the DSN is looking at the server.

    I found this code which works (see below) from another forum by Jerry Sullivan, where you can change the database. It only works once because it has constant the string stays the same, i tried changing the code to having the database as a variable but this did not work. As i have 15 database with the same structure on the same server, i want to swap between database to find my results, only thing which needs changing is the database name.

    I have pasted the code

    Sub SwitchODBCSource()
        Dim conn As WorkbookConnection
        Dim sOldConnection As String, sNewConnection As String
        
        Const sOldPath As String = "DSN=xxxx;UID=XX;PWD=xxx;APP=Microsoft Office 2010;WSID=xxx;DATABASE=test2; '--omit trailing backslashes to change DefaultDir
        Const sNewPath As String = "DSN=xxxx;UID=XX;PWD=xxx;APP=Microsoft Office 2010;WSID=xxx;DATABASE=test2;"
        
        For Each conn In ActiveWorkbook.Connections
            With conn
                If .Type = xlConnectionTypeODBC Then
                    sOldConnection = .ODBCConnection.Connection
                    If InStr(1, sOldConnection, sOldPath) > 0 Then
                        sNewConnection = Replace(sOldConnection, _
                                sOldPath, sNewPath, Compare:=vbTextCompare)
                        .ODBCConnection.Connection = sNewConnection
                        .Refresh '--optional to refresh now
                    End If
                End If
              End With
        Next conn
        
        Set conn = Nothing
    
    End Sub
    Last edited by Bob Phillips; 02-23-2014 at 07:05 AM. Reason: Added VBA tags

  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I have never used this technique in anger, but it seems to me that you should be able to connect, then change the connection database to your target db.

    I created a DSN that was pointing at AdventureWorksDW2008R2, then ran the following code to point it at one of my dbs, seems to work fine. See if you can adapt this.

    Dim conn As Object 'ADODB.Connection
    Dim RS As Object 'ADODB.RecordSet
    Dim data As Variant
         
        Const connPath As String = "DSN=myDSN"
        
        Set conn = CreateObject("ADODB.Connection")
        conn.Open connPath
    
        Set RS = CreateObject("ADODB.Recordset")
        
        Set RS = conn.Execute("SELECT * FROM DimProduct")
        data = RS.GetRows()
        MsgBox data(1, 0)
    
        conn.DefaultDatabase = "BARS"
        
        Set RS = conn.Execute("SELECT * FROM [User]")
        data = RS.GetRows()
        MsgBox data(1, 0) & " " & data(2, 0)
         
        Set conn = Nothing
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •