+ Reply to Thread
Results 1 to 2 of 2

Excel query update

Hybrid View

  1. #1
    Registered User
    Join Date
    11-22-2017
    Location
    Glasgow
    MS-Off Ver
    2007
    Posts
    1

    Excel query update

    I have an Excel spreadsheet that contains several hundred rows and about 50 columns.
    Some of these columns are yes/no type.
    I have created a query to extract a subset of data based on the value of one of the yes/no columns.
    So far so good - everything works and I see the desired subset in another worksheet.
    If I move the workbook to a different folder the query does not work as the data paths in the Connection still point to the old location.
    I can manually edit the Connection to point to the correct location but I will have 20 or more subsets to extract and the workbook location will change again.
    I want to use vba to change the Connection to point to the current file location and refresh the subset on the click of a button.
    I have tried using relative file locations but that doesn't work.
    I worked out the vba to make the changes but the changes are not being saved.
    Have I missed something or am I doing something wrong or stupid?
    This is the vba, the two msgbox lines return the same string:
    Sub UpdateSubSet()
    
        Dim strPath As String
        Dim strFile As String
        Dim strConnect() As String
        
        strPath = ThisWorkbook.Path
        strFile = ThisWorkbook.Name
        strConnect = Split("ODBC;DSN=Excel Files;" _
                            & "DBQ=" & strPath & "\" & strFile & ";" _
                            & "DefaultDir=" & strPath & ";" _
                            & "DriverId=1046;" _
                            & "MaxBufferSize=2048;" _
                            & "PageTimeout=5;", ";")
                
        MsgBox ThisWorkbook.Connections("qryGetSubSet").ODBCConnection.Connection
        
        ThisWorkbook.Connections("qryGetSubSet").ODBCConnection.Connection = strConnect
        
        MsgBox ThisWorkbook.Connections("qryGetSubSet").ODBCConnection.Connection
          
        ThisWorkbook.RefreshAll
    
    End Sub

  2. #2
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,957

    Re: Excel query update

    I've done something similar with ODBC connections usually to hide or insert a password. I can't say that I understand what you are doing with your split command. The following is partial "air code" done from memory.

    The way I do it is
    Dim ConnectString as string
    
    ConnectString = ThisWorkbook.Connections("qryGetSubSet").ODBCConnection.Connection
    strConnect  = Split(ConnectString, ";")
    strConnect (2) = "DBQ=" & strPath & "\" & strFile
    ThisWorkbook.Connections("qryGetSubSet").ODBCConnection.Connection) = join(srtconnect,";")
    I'm not sure which element DBQ= is associated with. Get a printout of the connection string - what is shown under Data-> Connections-> Properties is missing the leading field. So the 2 might be 1 or whatever.
    One spreadsheet to rule them all. One spreadsheet to find them. One spreadsheet to bring them all and at corporate, bind them.

    A picture is worth a thousand words, but a sample spreadsheet is more likely to be worked on.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Update Query Url: Excel 2016 Query
    By igoodable in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-11-2017, 12:45 PM
  2. Update query via Excel VBA - incorrect Sql statement
    By Hendrik Soede in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-14-2013, 07:03 AM
  3. Running Access Update query from Excel
    By vish2025 in forum Access Tables & Databases
    Replies: 2
    Last Post: 10-26-2010, 08:53 AM
  4. Update Excel query value from Sheet
    By jribeiro in forum Excel General
    Replies: 2
    Last Post: 08-05-2009, 06:58 PM
  5. [SOLVED] Running a SQL Update Query in Excel VBA
    By KC in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-27-2006, 06:35 PM
  6. [SOLVED] How do I update a ODBC query in Excel using pivotTables in VBA?
    By FCS in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-10-2005, 12:55 PM
  7. [SOLVED] Update Query and Local Tables in Excel
    By Kevin in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-25-2005, 06:06 PM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1