• Can’t change reference to msquery source (XP)

    Home » Forums » AskWoody support » Productivity software by function » MS Excel and spreadsheet help » Can’t change reference to msquery source (XP)

    Author
    Topic
    #401066

    I created a query to import data from an external database file (the file is local on my pc). After creating the query, I moved the file to a different directory. How do I get Excel or MsQuery to use the database file in the new location as the new source when updating?

    Viewing 2 reply threads
    Author
    Replies
    • #787093

      1. Interactively, it is convoluted and illogical (anyone know of a better method?)

      Open the workbook.
      Click somewhere in the imported data.
      Click “Edit Query” on the External Data toolbar, or select Data | Import External Data | Edit Query…
      You’ll get an error message “Could not find file …”
      Click OK.
      You’ll get a login screen. Click on Database…
      Browse to the new location of the database, select it and click OK twice.
      You’ll get another error message “This query can not be edited…”. Click OK.
      You’ll get the error message “Could not find file …” again (!), then Microsoft Query opens.
      Click the SQL button, or select View | SQL.
      Edit the location of the database in the SQL string manually, then click OK.
      Select File | Return data to Excel.
      Phew!

      2. In VBA:

      Dim strOldPath As String
      Dim strNewPath As String
      strOldPath = “C:Excel”
      strNewPath = “C:Databases”
      With ActiveSheet.QueryTables(1)
      .CommandText = Replace(.CommandText, strOldPath, strNewPath)
      .Connection = Replace(.Connection, strOldPath, strNewPath)
      End With

      • #787102

        Thank You Hans

        I couldn’t get the first part to work but the VBA worked like a bomb.

        Why did MS made it so difficult?

      • #787103

        Thank You Hans

        I couldn’t get the first part to work but the VBA worked like a bomb.

        Why did MS made it so difficult?

      • #808281

        Ah, the beauty of the Search function! My situation was similar, but for multiple MSQuery query tables on multiple worksheets. I surrounded the code from Hans with the below and it worked wonderfully — cheers! cheers

        Dim strOldPath As String
        Dim strNewPath As String
        strOldPath = "C:Excel"
        strNewPath = "C:Databases"
        
        Dim wSht As Worksheet
        Dim allwShts As Sheets
        Set allwShts = Worksheets
        Dim strName As String
        
        'Loop through each worksheet
        For Each wSht In allwShts
        wSht.Activate
        
            For Each QueryTable In ActiveSheet.QueryTables
                
                strName = QueryTable.Name
                With ActiveSheet.QueryTables(strName)
                    .CommandText = Replace(.CommandText, strOldPath, strNewPath)
                    .Connection = Replace(.Connection, strOldPath, strNewPath)
                End With
                MsgBox strName & "Complete"
                
            Next
        
        Next wSht
        
        MsgBox "Every MSQuery has been changed.", vbInformation
      • #808282

        Ah, the beauty of the Search function! My situation was similar, but for multiple MSQuery query tables on multiple worksheets. I surrounded the code from Hans with the below and it worked wonderfully — cheers! cheers

        Dim strOldPath As String
        Dim strNewPath As String
        strOldPath = "C:Excel"
        strNewPath = "C:Databases"
        
        Dim wSht As Worksheet
        Dim allwShts As Sheets
        Set allwShts = Worksheets
        Dim strName As String
        
        'Loop through each worksheet
        For Each wSht In allwShts
        wSht.Activate
        
            For Each QueryTable In ActiveSheet.QueryTables
                
                strName = QueryTable.Name
                With ActiveSheet.QueryTables(strName)
                    .CommandText = Replace(.CommandText, strOldPath, strNewPath)
                    .Connection = Replace(.Connection, strOldPath, strNewPath)
                End With
                MsgBox strName & "Complete"
                
            Next
        
        Next wSht
        
        MsgBox "Every MSQuery has been changed.", vbInformation
    • #787094

      1. Interactively, it is convoluted and illogical (anyone know of a better method?)

      Open the workbook.
      Click somewhere in the imported data.
      Click “Edit Query” on the External Data toolbar, or select Data | Import External Data | Edit Query…
      You’ll get an error message “Could not find file …”
      Click OK.
      You’ll get a login screen. Click on Database…
      Browse to the new location of the database, select it and click OK twice.
      You’ll get another error message “This query can not be edited…”. Click OK.
      You’ll get the error message “Could not find file …” again (!), then Microsoft Query opens.
      Click the SQL button, or select View | SQL.
      Edit the location of the database in the SQL string manually, then click OK.
      Select File | Return data to Excel.
      Phew!

      2. In VBA:

      Dim strOldPath As String
      Dim strNewPath As String
      strOldPath = “C:Excel”
      strNewPath = “C:Databases”
      With ActiveSheet.QueryTables(1)
      .CommandText = Replace(.CommandText, strOldPath, strNewPath)
      .Connection = Replace(.Connection, strOldPath, strNewPath)
      End With

    • #787097

      You can also use my flexfind (see my website below) to S&R in the query definition.
      Another option is to try this little utility…

      • #787164

        I forgot to add that the macro in the utility can be started from the Tools menu (Tools, Query Manager).

      • #787165

        I forgot to add that the macro in the utility can be started from the Tools menu (Tools, Query Manager).

    Viewing 2 reply threads
    Reply To: Can’t change reference to msquery source (XP)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: