Results 1 to 6 of 6
  1. #1
    Lou_Reed is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2015
    Posts
    1,664

    SQL Code Error?

    In the tutorial shown here: http://www.functionx.com/access/Lesson29.htm

    I am at the point where the SQL code should look like this:

    SELECT Properties.[Property Type],
    Properties.City,
    Properties.State,
    Properties.[ZIP Code],
    Properties.Bedrooms,
    Properties.Bathrooms,
    Properties.Stories,
    Properties.[Year Built],
    Properties.Condition,
    Properties.[Market Value]
    FROM Properties
    WHERE (((Properties.[Property Type])="single family") OR
    (((Properties.[Property Type])="townhouse")) AND
    ((Properties.[ZIP Code]) Between 20850 And 20950)) ;

    However, it does not produce the output shown in the tutorial. The output has in fact more records with
    : 13 as opposed to the previous steps 10. The modifications of the SQL were supposed to eliminate more records leaving
    only the MD state with single family and townhouses. That is a total of 4 records. The output looks like this in datasheet view:

    Property Type City State ZIP Code Bedrooms Bathrooms Stories Year Built Condition Market Value
    Single Family Silver Spring MD 20904 4 2.5 3 1995 Good Shape $495,880.00
    Single Family Alexandria VA 22231 4 3.5 2 2000 Excellent $620,724.00
    Single Family Laurel MD 20707



    2
    Good Shape $422,625.00
    Single Family Alexandria
    22024 3

    1965
    $345,660.00
    Single Family Martinsburg WV 25401 4 3.5 3 2005 Good Shape $325,000.00
    Single Family Silver Spring MD 20906 3 3 3 1996 Excellent $625,450.00
    Single Family Chevy Chase
    20956

    3 2001
    $525,450.00
    Single Family Washington DC 20008 5 3.5 3 2000 Good Shape $555,885.00
    Single Family Falls Church VA
    5 2.5 2 1995 Excellent $485,995.00
    Single Family Washington DC 20004 4 3.5 1 2004 Good Shape $735,475.00
    Single Family Gaithersburg MD 20872 4 2.5 1 1965 Unknown $615,775.00
    Townhouse Rockville MD 20854 3 2.5 2 1988 Good Shape $525,995.00
    Single Family York PA 17405 4 3.5 3 2002 Excellent $326,885.00


    When there should only be 4 records, those in MD and with a zip code between 20850 and 20920.

    I reordered the code the way the tutorial said to do. My output is simply incorrect. So what is wrong with my
    code or is it the tutorial?

    Thanks in advance.

    Respectfully,


    Lou Reed
    Last edited by Lou_Reed; 08-28-2015 at 08:02 AM. Reason: correction

  2. #2
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    You parentheses are not correct, so you are essentially checking to see if it is either property type OR that zip code.
    Try changing your WHERE like this:
    Code:
    WHERE (((Properties.[Property Type])="single family") OR 
    ((Properties.[Property Type])="townhouse")) AND
    ((Properties.[ZIP Code]) Between 20850 And 20950);
    This is, of course, assuming that your Zip Code is numeric and not Text. If it is Text, you will need quotes around them.

  3. #3
    Lou_Reed is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2015
    Posts
    1,664
    I tried it and it worked! Now the question is what was wrong with my original code?

    Also does the code shown in the Lesson 29 tutorial work? It is different from yours and mine.

    Respectfully,


    Lou Reed

    PS I am not a professional programmer, but I have done a lot of programming as an engineer.

  4. #4
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    You need to be very carefully when dealing with AND and OR and parentheses. If you have them in the wrong place, it might not return your intended results.
    Note that:
    (Condition1 OR Condition2) AND Condition3
    is not the same as
    Condition1 OR (Condition2 AND Condition3)
    Things embedded deeper in the parentheses are evaluated first (so you need to back to your mathematic rules of basic logic and order of operations).

    I feel that Access sometimes confuses/muddles by adding a lot more parentheses than are needed in WHERE clauses. So it sometimes helps to work through it and remove the unnecessary ones.

    For example, it is not necessary to write:
    (Properties.[ZIP Code])
    You can just write:
    Properties.[ZIP Code]

  5. #5
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    One trick I like to use in writing these myself is the following:

    Write out the Conditions you want (and order that they need to happen in) on paper.
    Create the SQL code for all the different Conditions of the WHERE clause independent of each other.
    Then combine them and add the parentheses based on the order they need to happen in.

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,641
    The tutorial correctly shows (although it still has unnecessary parens):

    WHERE ((Properties.[Property Type] = "single family") OR
    (Properties.[Property Type] = "townhouse")) AND
    (Properties.[ZIP Code] Between 20850 And 20920);

    You posted:
    WHERE (((Properties.[Property Type])="single family") OR
    (((Properties.[Property Type])="townhouse")) AND
    ((Properties.[ZIP Code]) Between 20850 And 20950));

    Simplest would be:
    WHERE (Properties.[Property Type] = "single family" OR
    Properties.[Property Type] = "townhouse") AND
    Properties.[ZIP Code] Between 20850 And 20920;

    As Joe said, Access will throw in all those unneeded parens.

    So be careful in the query design grid about building with OR and AND operators. Access won't always get the parens the way you expect.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Error in code
    By cbende2 in forum Programming
    Replies: 5
    Last Post: 06-04-2015, 10:39 AM
  2. Error in VBA Code
    By guillermoftw in forum Access
    Replies: 3
    Last Post: 02-10-2015, 12:12 PM
  3. error with a code
    By ashraf_al_ani in forum Forms
    Replies: 3
    Last Post: 08-04-2014, 02:56 AM
  4. VBA Code Returning Error Run Time Error 3061
    By tgwacker in forum Access
    Replies: 2
    Last Post: 11-24-2013, 11:00 AM
  5. Error in Code
    By Lockrin in forum Programming
    Replies: 3
    Last Post: 02-25-2010, 03:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums