+ Reply to Thread
Results 1 to 4 of 4

Unexpected results from MSQuery

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

    Unexpected results from MSQuery

    I am using MSQuery to download selected data from our accounts package into Excel
    The SQL statement is :-
    SELECT SALES_LEDGER.ACCOUNT_NAME, SALES_LEDGER.VAT_CODE, SALES_TRANSACTIONS.TRANSACTION_TYPE, SALES_TRANSACTIONS.GOODS_AMOUNT, SALES_TRANSACTIONS.TRANSACTION_DATE
    FROM ACCOUNTING_SYSTEM.SALES_LEDGER SALES_LEDGER, ACCOUNTING_SYSTEM.SALES_TRANSACTIONS SALES_TRANSACTIONS
    WHERE SALES_LEDGER.THIS_RECORD = SALES_TRANSACTIONS.PARENT_RECORD AND ((SALES_TRANSACTIONS.TRANSACTION_TYPE=5) AND (SALES_LEDGER.VAT_CODE=4) OR (SALES_TRANSACTIONS.TRANSACTION_TYPE=4) AND (SALES_LEDGER.VAT_CODE=4))

    My problem is that the OR statement is not completed. If I swap the order of the WHERE statement then the OR part is still not executed.
    I am not sure if I am using the right forum, if I am not then please point me right, if I am can any one help with the SQL statement
    j_Southern

  2. #2
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Unexpected results from MSQuery

    I am not certain what the criteria should be but I think probably:

    SELECT SALES_LEDGER.ACCOUNT_NAME, SALES_LEDGER.VAT_CODE, SALES_TRANSACTIONS.TRANSACTION_TYPE, SALES_TRANSACTIONS.GOODS_AMOUNT, SALES_TRANSACTIONS.TRANSACTION_DATE
    FROM ACCOUNTING_SYSTEM.SALES_LEDGER SALES_LEDGER, ACCOUNTING_SYSTEM.SALES_TRANSACTIONS SALES_TRANSACTIONS
    WHERE SALES_LEDGER.THIS_RECORD = SALES_TRANSACTIONS.PARENT_RECORD AND ((SALES_TRANSACTIONS.TRANSACTION_TYPE=5) OR (SALES_TRANSACTIONS.TRANSACTION_TYPE=4)) AND SALES_LEDGER.VAT_CODE=4
    • Please remember to mark threads Solved with Thread Tools link at top of page.
    • Please use code tags when posting code: [code]Place your code here[/code]
    • Please read Forum Rules

  3. #3
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Unexpected results from MSQuery

    You should probably be posting your question here.

    Anyway, let's look at your problem from a logical standpoint.

    Your WHERE clause
    Please Login or Register  to view this content.
    What I usually do is, I split "AND" conditions by row, and "OR" conditions in the same row. So it would look like this for you
    Please Login or Register  to view this content.
    When split like this, it is visually easy to spot what went wrong.

    I assume you want it to be a TRANSACTION_TYPE of "4" or "5" and it must have VAT_CODE of "4"?
    So it should look like this:
    Please Login or Register  to view this content.
    And when you want to combine it back to a single line, always bracket "OR" conditions (as shown above).

    Please Login or Register  to view this content.
    多么想要告诉你 我好喜欢你

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

    Re: Unexpected results from MSQuery

    Thanks for both your help. I have benn able to rewrite the query in a way that works and stays stable.
    John

+ 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. LINEST function unexpected results
    By guffaw320 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 06-05-2013, 02:46 PM
  2. Unexpected results from CurrentRegion
    By ajgully in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-29-2011, 02:55 PM
  3. VLOOKUP with unexpected N/A results
    By cgjones in forum Excel General
    Replies: 2
    Last Post: 03-15-2011, 04:39 PM
  4. Unexpected HLOOKUP results
    By scaffdog845 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-06-2009, 03:01 PM
  5. Unexpected Results with CurrentRegion
    By avveerkar in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-06-2007, 09:57 PM

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