+ Reply to Thread
Results 1 to 8 of 8

Coding of a query using QueryTable

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Manchester England
    MS-Off Ver
    Excel 2010
    Posts
    992

    Coding of a query using QueryTable

    I am trying to download data from Sage Line100 using vba. If I set about this using MSQuery I can down load the data I want but I have not been able to code this.
    My best attempt so far is
    Sub DownLoad()
          SQLStr = "SELECT SELECT SALES_LEDGER.ACCOUNT_NUMBER, SALES_TRANSACTIONS.SALES_CONTROL_VALUE "
          SQLStr = SQLStr & "FROM ACCOUNTING_SYSTEM.SALES_LEDGER SALES_LEDGER, ACCOUNTING_SYSTEM.SALES_TRANSACTIONS SALES_TRANSACTIONS "
          SQLStr = SQLStr & "WHERE SALES_LEDGER.THIS_RECORD = SALES_TRANSACTIONS.PARENT_RECORD "
          SQLStr = SQLStr & "AND ((SALES_TRANSACTIONS.TRANSACTION_DATE>={d '2017-01-01'}))"
       
        NewSQL "DownLoad", SQLStr
          
    End Sub
    Sub NewSQL(ShName, SQLStr)
    Sheets(ShName).Visible = True
    Const SAGECONN1 As String = "ODBC;DSN=SAGE LINE 100;UID=test;;"
    Dim qtSage As QueryTable
    Dim rngDest As Range
    'Dim strSQL As String
    
           Set rngDest = Sheets(ShName).Range("A1")
            Set qtSage = Worksheets(ShName).QueryTables.Add(SAGECONN1, rngDest, strSQL)
              qtSage.RefreshStyle = xlOverwiteCells
              Sheets(ShName).Range("A1").ListObject.QueryTable.Refresh BackgroundQuery:=False ********
           
    End Sub
    This ends with the error message "Object variable or With block not set" at the line marked********
    I have not been able to resolve this and would welcome some help
    John

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Coding of a query using QueryTable

    What happens if you remove that's line?
    If posting code please use code tags, see here.

  3. #3
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: Coding of a query using QueryTable

    Try below (replace the line marked with ********)
    qtSage.Refresh BackgroundQuery:=False
    "Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something."
    ― Robert A. Heinlein

  4. #4
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Manchester England
    MS-Off Ver
    Excel 2010
    Posts
    992

    Re: Coding of a query using QueryTable

    Norie andCK76
    Thanks for your interest. If I remove the asterisked line then nothing happens and no data is returned.
    If I replace the line as suggested by CK76 then I get the error message "Incomplete Data Source"
    John

  5. #5
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Manchester England
    MS-Off Ver
    Excel 2010
    Posts
    992

    Re: Coding of a query using QueryTable

    I have changed the code to
    Sub NewSQL(ShName, SQLStr)
    Sheets(ShName).Visible = True
    'Const SAGECONN1 As String = "ODBC;DSN=SAGE LINE 100;UID=test;;"
    Const SAGECONN1 As String = "ODBC;DSN=SageLine100;UID = test;;"
    Dim qtSage As QueryTable
    Dim rngDest As Range
    'Dim strSQL As String
    
           Set rngDest = Sheets(ShName).Range("A1")
            Set qtSage = Worksheets(ShName).QueryTables.Add(SAGECONN1, rngDest, SQLStr)
              qtSage.RefreshStyle = xlOverwriteCells
              qtSage.Refresh BackgroundQuery:=False
    End Sub
    This includes the correction of 2 typos.(SQLStr and xloverwrite) and now leads to the error message "Authorisation Failure"
    I have often used this kind of code to get data from Sage Line 100,and cannot see why its not working now.
    John

  6. #6
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: Coding of a query using QueryTable

    From the error, it's likely user permission level. Never dealt with Sage Line 100, so can't say for sure, but it's usually combination of User permission table setting and INI setting.

  7. #7
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Manchester England
    MS-Off Ver
    Excel 2010
    Posts
    992

    Re: Coding of a query using QueryTable

    I am going to have to admit that I have discovered that the cause of my problem was another typo in SQLStr. (SELECT SELECT)
    Correcting that allowed the expected data to download.
    Many apologies for wasting everyone's time. I have marked this thread as solved. The next one is about the coding of parameters and I have checked that I have not made the same crass mistake.
    John

  8. #8
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Coding of a query using QueryTable

    John

    I think we should have seen that too.

+ 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. How to test Querytable connection before actually executing SQL query
    By jackyan in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-15-2013, 03:31 PM
  2. [SOLVED] Problem with Querytable destination, using Web Query with multiple URLs
    By rdkyote in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-03-2013, 03:18 PM
  3. QueryTable Leaking Memory despite deletion of QueryTable
    By rbateman in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-11-2013, 03:20 AM
  4. QueryTable Leaking Memory despite deletion of QueryTable
    By rbateman in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-10-2013, 02:26 PM
  5. Query & Coding issue
    By abenny in forum Access Programming / VBA / Macros
    Replies: 2
    Last Post: 01-31-2011, 01:13 PM
  6. Refresh Query by Name, not by Sheet().Range().QueryTable
    By Whizbang in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-17-2009, 04:59 PM
  7. [SOLVED] Problem:Query Cell contain Formula from QueryTable is always get old value
    By Resant in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-03-2006, 03:30 AM
  8. Excel Web Query using a QueryTable via HTTPS??
    By Bing in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-02-2005, 11:06 AM

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