简体   繁体   中英

Excel 2013 - 1004 Run-time error Refresh Query Table

Have been troubleshooting this one for weeks and so far, no resolutions have worked. I have tried many solutions, including:

  • Option Explicit
  • removing BackgroundQuery = False
  • AutoFilterMode = False

What is strange in my case is, on two computers running the same version of Excel and using the same version of ODBC driver, on Computer 1, all queries work and on Computer 2, it gives the run-time error. The issue is, soon Computer 1 will be gone so I need to get Computer 2 working.

The other thing that is really odd is that Computer 2 does successfully connect and retrieve info in the same subroutine! So query 1 works but query 2 gives the runtime error.

Running on Computer 2 in same Excel file, here is query 1:

SELECT  COUNT(ADM_APP_INFO.APP_NUM)
FROM 
 SCHEMA.TABLE TABLE
WHERE (TABLE.QUEUE  = 'QNAME' AND TABLE.PARAM = 'D' 
AND TABLE.DATE_ENT >= '1/1/2016')

which works and pulls info. Query 2, which has a non-zero count is:

SELECT DISTINCT COUNT(ACCT_NUM) FROM SCHEMA.DAILY_RUN WHERE DAILY_RUN.ACCT_NUM_FLAG = 0 FETCH FIRST 10 ROWS ONLY

Yet it does work when on Computer 2 when query 2 has no WHERE clause. Both of these queries work on Computer 1 but Query 2 with WHERE clause gives the error on Computer 2.

I even copy the working Excel file on Computer 1 to Computer 2 and get the error only on Computer 2. These points suggest there isn't Any suggestions?

For reference, here is the With block I am using:

With Worksheets("Template").QueryTables.Add(Connection:="ODBC;DSN=Connect32;UID=UserName;;HOST=111.111.11.111;PORT=xxxx;SUBSYS=xxxx;DBTY=DB2;PLAN=xxxx ", _
    Destination:=Range("D5"))
    .CommandText = Range("Query!A1:A1").Value
    '.Name = "Query from MS Access Database"
    .FieldNames = False
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .BackgroundQuery = True
    .RefreshStyle = xlOverwriteCells
    .SavePassword = True
    .SaveData = True
    .AdjustColumnWidth = False
    .RefreshPeriod = 0
    .PreserveColumnInfo = True
    .Refresh BackgroundQuery:=False
End With

OK, so it looks like I got a birthday present as one of my colleagues was able to resolve this.

Turns out my ODBC driver, as some of you may have suspected, was not configured the exact same way on both systems. In my case, I needed to add the database name under the AlternateID setting under the Advanced tab.

Even fully qualifying the table names in the queries doesn't help without this step. Hope this helps others out there facing this seemingly bizarre issue!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM