Consulting

Results 1 to 7 of 7

Thread: VBA to Change Connection String of all query's in a workbook

  1. #1

    VBA to Change Connection String of all query's in a workbook

    Just like the title states what would be VBA to change a connection string of all the query's in a workbook?

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    That depends on whether the strings are in code or cells.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    I am going to go with cells. They are access query's that are imported into the spreadsheet.

  4. #4
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Select a Cell with a connection string to be replaced. Press Ctrl+H, then click the Advanced button.
    Attached Images Attached Images
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  5. #5
    SOmething like this:

    [VBA]Sub ChangeAllConnections(sNewConn As String)
    Dim oLo As ListObject
    Dim oSh As Worksheet
    Dim oQt As QueryTable
    For Each oSh In Worksheets
    'connections can be in a listobject (table)
    For Each oLo In oSh.ListObjects
    oLo.QueryTable.Connection = sNewConn
    Next
    'but also outside a listobject (querues created in Excel 2003 or before; web queries)
    For Each oQt In oSh.QueryTables
    oQt.Connection = sNewConn
    Next
    Next
    End Sub
    [/VBA]
    Regards,

    Jan Karel Pieterse
    Excel MVP jkp-ads.com

  6. #6
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    or ?
    [VBA]
    Sub M_snb()
    For Each cn In ThisWorkbook.Connections
    cn.OLEDBConnection.Connection = Replace(cn.OLEDBConnection.Connection, "G:\OF\", "G:\")
    Next
    End Sub
    [/VBA]

  7. #7
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    That assumes they are all OLEDB and not ODBC, of course.
    Be as you wish to seem

Posting Permissions

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