+ Reply to Thread
Results 1 to 7 of 7

Using OR in MSQuery

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

    Using OR in MSQuery

    When looking for data in our Sage accounts program I like to use MSQuery and Excel.
    Typically I will use something like this to limit the data to a given month.

    |Document _Type| Document_Date|
    | 4 or 5 | Between 01/01/2019 and 31/01/2019|
    and this will give me all documents of type 4 or 5 during month of January which I want.

    However if I perform the query a second time I only get the documents of type 4 and when I look at the query again it looks like_

    |Document_Type| Document _Date |
    | 4 | Between 01/01/2019 and 31/01/2019|
    OR | 5 | Between 01/01/2019 and 31/01/2019|
    and this only gives me the documents of type 4. for the month of January
    When I look at the SQL statements they are different
    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    I over come the difficulty by using the correct SQL statement in my code. It would be much easier just to refresh the query. How should I use MSQuery to give me the desired result and stop it changing itself to give the wrong result?
    John
    Last edited by alansidman; 04-25-2019 at 10:31 AM.

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

    Re: Using OR in MSQuery

    As there has been no response to my initial post I am trying to simplify it.

    My core problem is that when using MSQuery to get data from Sage Line 100 I get the result I need the first time it is run but it resets itself so I get the wrong result in subsequent runs.
    I see that the reason is that the SQL statement is changed from:-
    [1]
    Please Login or Register  to view this content.
    to:-
    [2]
    Please Login or Register  to view this content.
    The correct SQL statement is achieved in MSQuery by making the selection of :-

    TRANSACTION_TYPE (4 or 5) and TRANSACTIOIN_DATE(Between Date1 and Date2) using only 1 row of the MSQuery grid and giving the correct SQL statement [1]
    This gets changed by MSQuery to
    TRANSACTION_TYPE (4) and TRANSACTION_DATE (Between Date1 and Date2)
    or
    TRANSACTION_TYPE(5) and TRANSACTION_DATE (Between Date1 and Date2) using 2 rows of the MSQuery grid and giving an SQL statement which is wrong [2]

    What I would like to know is, is there a way of using MSQuery so that I consistently get the correct SQL statement?
    I hope this makes my problem clearer and that some one will be able to help me.
    John
    Last edited by j_Southern; 04-26-2019 at 09:08 AM.

  3. #3
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,907

    Re: Using OR in MSQuery

    Your second option (that MSQuery reverts to) should give the same results as the first one, logically.
    Rory

  4. #4
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,883

    Re: Using OR in MSQuery

    I can't see logical difference between two where clauses either.

    I suspect, there's some other cause for the difference (such as cursor type etc).
    ?Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something.?
    ― Robert A. Heinlein

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

    Re: Using OR in MSQuery

    CK76 and RoryA
    The result of the second query is that I get the data where TRANSACTION_TYPE = 5 only, when written TRANSACTION_TYPE(5 or 4)
    or data where TRANSACTION_TYPE= 4 only where TRANSACTION_TYPE = (4 OR 5)

    I have been plagued with this problem through several versions of Excel at least back to Excel2007 and have always found a work round, although I don't think that MSQuery has changed since then.
    I am intrigued by your mention of other causes. What should I look out for.
    This is important for me because there are many more 5's than 4's so it is easy to miss the 4's (5 = Invoice, 4 = Credit Note) In my early days I did miss them and the world found out that I was a stupid as I knew myself to be.
    Not funny!
    John

  6. #6
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,907

    Re: Using OR in MSQuery

    Do you have to use MSQuery rather than Power Query?

  7. #7
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,883

    Re: Using OR in MSQuery

    If you run both query directly on the db do you get same issue as when queries are run on MSQuery?

+ 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. MSQuery question
    By LarryLL in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-11-2006, 06:05 PM
  2. MSQuery
    By Lac in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 06-14-2006, 03:55 AM
  3. MSQuery problems
    By GoBobbyGo in forum Excel General
    Replies: 0
    Last Post: 04-13-2006, 05:35 PM
  4. [SOLVED] MsQuery Documentation
    By [email protected] in forum Excel General
    Replies: 0
    Last Post: 03-28-2006, 12:00 PM
  5. MSQuery Optimization
    By BAC in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-03-2006, 04:00 PM
  6. [SOLVED] Combo box from MSQuery
    By Dean in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 01-11-2006, 08:15 AM
  7. Using Case in MSquery
    By LBIdydys in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-17-2005, 12:10 AM
  8. Programming with msquery
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-30-2005, 08:05 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