Results 1 to 4 of 4
  1. #1
    jschlapi is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Nov 2011
    Posts
    8

    Query - need unpainted fire hydrants from subform

    I have a Master form that uses a subform for data entry. It feeds my main table perfectly, so I am able to create a report showing each fire hydrant and every date of service.

    My problem is that I cannot create a query to show any hydrant that has NEVER been painted. My code is:

    SELECT Maintenance.[Hydrant No], Hydrants.Location, Maintenance.[Paint OK], Maintenance.[Needs Paint], Maintenance.Touchup, Maintenance.Repainted, Maintenance.[Last Paint Date], Maintenance.Crew


    FROM Hydrants INNER JOIN Maintenance ON Hydrants.[Hydrant No] = Maintenance.[Hydrant No]
    WHERE (((Maintenance.[Last Paint Date]) Is Null))
    ORDER BY Maintenance.[Hydrant No];


    My result for this query is every line item that does not have a date will be displayed - if it was painted this year, for instance, I end up with 2 lines for 1 hydrant and the one without a paint date shows as unpainted, however, since it was painted this year, I do not want it showing in the query.

    I hope this makes sense - any ideas? Thank you.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,601
    This requires each record to test for a value in all other records in same table. Can be tricky. Use subquery or DLookup.

    Try:

    WHERE (((Maintenance.[Last Paint Date]) Is Null) AND NOT HydrantNo IN (SELECT HydrantNo FROM Maintenance WHERE Not [Last Paint Date] Is Null))
    ORDER BY Maintenance.[Hydrant No];


    That should return hydrants without a paint date only if there are no records for the hydrant with a paint date.
    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.

  3. #3
    Dal Jeanis is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    If table Hydrants includes a list of all hydrants, regardless of whether they have maintenance records, then do a LEFT JOIN like this -
    Code:
    SELECT 
      TH.[Hydrant No], 
      TH.Location, 
      NZ(TM.[Paint OK],""), 
      NZ(TM.[Needs Paint],""), 
      NZ(TM.Touchup,""), 
      NZ(TM.Repainted,""), 
      NZ(TM.[Last Paint Date],"NEVER"), 
      NZ(TM.Crew,"")
    FROM 
      Hydrants AS TH
      LEFT JOIN 
      Maintenance AS TM
      ON TH.[Hydrant No] = TM.[Hydrant No]
    WHERE 
      (((TM.[Last Paint Date]) Is Null))
    ORDER BY 
      TM.[Hydrant No];
    Something just seems odd to me about the design assumptions here.
    [Paint Ok] and [Needs Paint] seem like two Boolean variables, and seem mutually exclusive.

    Likewise, [Touchup] and [Repainted] seem like past maintenance actions, so they might be dates of that activity, or they might be mutually explucive Boolean variables to describe what happened at the [Last Paint Date].

    The problem is that, if the first two are inspection results, then there needs to be an inspection date somewhere, and the only place to put it is in the [Last Paint Date] field.

    If all fields those represent things that happened at different times, then [Crew] would just be the last people to touch the record...

    On the other hand, if each hydrant can have multiple maintenance records, then only one of the four fields would be filled true, the others false.

    I'm thinking that what you have here is an unnormalized form, that probably should look like
    Code:
    tblMaintActions
    PK           AutoNumber Key
    HydrantNo    FK to Hydrant
    MaintDate    Date
    MaintType    Text   (Inspection or Painting - could also be a code)
    MaintResults Text   (Paint OK or Needs paint for Inspections, Touchup or Repainted for Painting  - could also be a code)
    MaintNotes   Text   (a place for notes)

  4. #4
    jschlapi is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Nov 2011
    Posts
    8

    Thank you!!!

    This worked perfectly!! Thank you so much for your help.
    ===============================================

    Quote Originally Posted by June7 View Post
    This requires each record to test for a value in all other records in same table. Can be tricky. Use subquery or DLookup.

    Try:

    WHERE (((Maintenance.[Last Paint Date]) Is Null) AND NOT HydrantNo IN (SELECT HydrantNo FROM Maintenance WHERE Not [Last Paint Date] Is Null))
    ORDER BY Maintenance.[Hydrant No];


    That should return hydrants without a paint date only if there are no records for the hydrant with a paint date.

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

Similar Threads

  1. Fire Hydrants
    By rpsmsmith in forum Sample Databases
    Replies: 5
    Last Post: 11-08-2013, 09:52 AM
  2. Help with code to fire a create password dialog.
    By David618 in forum Programming
    Replies: 15
    Last Post: 05-08-2013, 10:22 PM
  3. Trying to fire event on record change
    By danielhowden in forum Forms
    Replies: 3
    Last Post: 05-13-2011, 06:30 AM
  4. Fire department forms
    By pentabarf in forum Forms
    Replies: 10
    Last Post: 09-21-2010, 08:00 AM
  5. when does an msquery fire
    By jonny dexter in forum Queries
    Replies: 0
    Last Post: 08-25-2010, 11:43 AM

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