Consulting

Results 1 to 6 of 6

Thread: Replace Query Database

  1. #1
    VBAX Tutor jo15765's Avatar
    Joined
    Oct 2011
    Posts
    281
    Location

    Replace Query Database

    How in Excel 2007/Excel 2010 can you use VBA to replace the database of all queries on the worksheet?
    Go to Heaven for the climate, Hell for the company.
    ~~Mark Twain

  2. #2
    not enough information

  3. #3
    VBAX Tutor jo15765's Avatar
    Joined
    Oct 2011
    Posts
    281
    Location
    Quote Originally Posted by westconn1 View Post
    not enough information
    Let's say there is an imported Access Query into Excel and the query text is (SQL behind the query)
    [vba]
    Select * from qry_Uno
    FROM c:\AlphaDawg\12345.qry_Uno qry_Uno
    [/vba]

    How can I change the 12345 (database name) to 84321
    Go to Heaven for the climate, Hell for the company.
    ~~Mark Twain

  4. #4
    VBAX Expert
    Joined
    Oct 2012
    Posts
    726
    Location
    [vba]Sub test()

    Const oldpath As String = "C:\old"
    Const newpath As String = "C:\new"
    Const db As String = "\mydb.mdb"

    Dim c As WorkbookConnection
    For Each c In ActiveWorkbook.Connections
    Select Case c.Type
    Case 1
    c.OLEDBConnection.Connection = _
    "OLEDB;Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & newpath & db
    c.OLEDBConnection.SourceDataFile = newpath & db
    Case 2
    c.ODBCConnection.Connection = _
    Replace(c.ODBCConnection.Connection, oldpath, newpath)
    c.ODBCConnection.CommandText = _
    Replace(c.ODBCConnection.CommandText, oldpath, newpath)
    End Select

    c.Refresh
    Next
    End Sub
    [/vba]

  5. #5
    VBAX Tutor jo15765's Avatar
    Joined
    Oct 2011
    Posts
    281
    Location
    What are the 2 different cases account for?
    Go to Heaven for the climate, Hell for the company.
    ~~Mark Twain

  6. #6
    VBAX Expert
    Joined
    Oct 2012
    Posts
    726
    Location
    If I remember correctly, the first case is for tables/queries linked directly, the second is for queries written using the query builder thing.
    Judging by the sql you posted above you would be using the second case.

Posting Permissions

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