Results 1 to 7 of 7
  1. #1
    JRodko is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Oct 2018
    Posts
    127

    Using DLookup with NZ in query.

    Hello,



    I am trying to use a combination of the Nz function and DlookUp function to find numerical gaps in data and append the offending rows to another table.

    I would like the query to return rows where the "From Depth" does not match the "To Depth" in the previous record. Currently the query returns all data and not only the records I want to see.

    I have attached a sample database but the SQL statement for the entire query is

    "
    INSERT INTO tbl_QAQC_Intervals ( [HOLE ID], [FROM], [TO], [TABLE] )SELECT Geotechnical.GEOT_Hole_ID, Geotechnical.GEOT_From_m, Geotechnical.GEOT_To_m, "GEOTECHNICAL GAP" AS [Table]
    FROM tblSelectedDrillHoleandDeclination INNER JOIN Geotechnical ON tblSelectedDrillHoleandDeclination.hole_NUM = Geotechnical.GEOT_Hole_ID
    WHERE (((Nz([GEOT_From_m]<>DLookUp("[GEOT_To_m]","Geotechnical","GEOT_ID=[Geotechnical]![GEOT_ID]-1"),False))=False))
    ORDER BY Geotechnical.GEOT_From_m;
    "

    And the formula I'm using to select rows is

    "Expr1: Nz([GEOT_From_m]<>DLookUp("[GEOT_To_m]","Geotechnical","GEOT_ID=[Geotechnical]![GEOT_ID]-1"),False)"

    Where GEOT_From_m is the "From Depth", GEOT_To_m is the "To Depth", GEOT_Hole_ID is a project ID, and "GEOT_ID" is an autonumber which I am using to compare the depths from each record.

    The data is entered in a form however referencing the table itself is fine I believe. I got the idea to use NZ from Allen Brown.

    Thank you.Gaps.zip

  2. #2
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    You would be better left joining the table to itself - copy and paste this sql

    Code:
    INSERT INTO tbl_QAQC_Intervals ( [HOLE ID], [FROM], [TO], [TABLE] )
    SELECT Geotechnical.GEOT_Hole_ID, Geotechnical.GEOT_From_m, Geotechnical.GEOT_To_m, "GEOTECHNICAL GAP" AS [Table]
    FROM (tblSelectedDrillHoleandDeclination INNER JOIN Geotechnical ON tblSelectedDrillHoleandDeclination.hole_NUM = Geotechnical.GEOT_Hole_ID) LEFT JOIN Geotechnical AS Geotechnical_1 ON (Geotechnical.GEOT_Hole_ID = Geotechnical_1.GEOT_Hole_ID) AND (Geotechnical.GEOT_From_m = Geotechnical_1.GEOT_To_m)
    WHERE (((Geotechnical_1.GEOT_To_m) Is Null))
    ORDER BY Geotechnical.GEOT_From_m;
    Note that Table is a reserved word, use it at your own risk

  3. #3
    JRodko is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Oct 2018
    Posts
    127
    Wow. Thank you very much.

    How does this work without referencing the previous record?

  4. #4
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    your requirement is to find 'Froms' without a matching 'To'. You have no guarantee that you don't have missing ID numbers nor that the 'previous' ID contains the matching To. In databases there is no such thing as 'previous' or 'next' without specifying an order. So the usual way is to find a record with the highest ID that is less than the current ID

    Also just realised you are also using 'From' as a field name - another reserved word. Before you start to suffer grief and waste a lot of time trying to solve an issue with a misleading error - see this link about reserved words
    https://support.office.com/en-us/art...9-f855bdd9c5a2

    Note that I have seen instances where using square brackets does not always solve the issue

  5. #5
    JRodko is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Oct 2018
    Posts
    127
    I had not thought of solving the problem that way. Thank you very much!!!

  6. #6
    JRodko is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Oct 2018
    Posts
    127
    One small modification needed. The Query returns the starting record every time even though there is no error. This happens because there is no matching (To) record for the lowest number (From).

    The number could be anything and will often not be the same otherwise I'd set a simple criteria like From <> 0.

    Any input on skipping the first error would be great. I am thinking Dmin but i think that would search the entire table and not just the query selection?

  7. #7
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    you can use dmin - with a where clause that identifies the hole_id, or you can use another query which will generally be quicker - copy and paste this code

    Code:
    INSERT INTO tbl_QAQC_Intervals ( [HOLE ID], [FROM], [TO], [TABLE] )
    SELECT Geotechnical.GEOT_Hole_ID, Geotechnical.GEOT_From_m, Geotechnical.GEOT_To_m, "GEOTECHNICAL GAP" AS [Table]
    FROM ((tblSelectedDrillHoleandDeclination INNER JOIN Geotechnical ON tblSelectedDrillHoleandDeclination.hole_NUM = Geotechnical.GEOT_Hole_ID) LEFT JOIN Geotechnical AS Geotechnical_1 ON (Geotechnical.GEOT_From_m = Geotechnical_1.GEOT_To_m) AND (Geotechnical.GEOT_Hole_ID = Geotechnical_1.GEOT_Hole_ID)) INNER JOIN (SELECT GEOT_Hole_ID, Min(GEOT_From_m) as minFrom FROM Geotechnical GROUP BY  GEOT_Hole_ID)  AS Geotechnical_Min ON tblSelectedDrillHoleandDeclination.hole_NUM = Geotechnical_Min.GEOT_Hole_ID
    WHERE (((Geotechnical_1.GEOT_To_m) Is Null) AND ((Geotechnical.GEOT_From_m)<>[minfrom]))
    ORDER BY Geotechnical.GEOT_From_m

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

Similar Threads

  1. Replies: 4
    Last Post: 02-13-2016, 06:00 PM
  2. dlookup on query
    By molly13 in forum Access
    Replies: 3
    Last Post: 10-21-2014, 09:00 AM
  3. Need help on Dlookup within query
    By cp1981 in forum Queries
    Replies: 9
    Last Post: 07-25-2014, 01:57 AM
  4. Dlookup in query
    By Bertrand82 in forum Queries
    Replies: 9
    Last Post: 11-14-2012, 06:42 AM
  5. Query with Dlookup
    By mari_hitz in forum Queries
    Replies: 5
    Last Post: 10-14-2011, 09:22 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