Results 1 to 7 of 7
  1. #1
    jjpuebla is offline Novice
    Windows 10 Access 2016
    Join Date
    Aug 2020
    Posts
    4

    Finding missing numbers in a Sequential field


    Hi everyone, I have a problem. I hope you can help me, please!

    I have a table with the sales of an specific day. This table has among other information the invoice numbers in a sequential order. I want to create a query to show if there is an invoice that is not in the sequential. For example, if yesterday's invoices start with invoice number 107015 and end with invoice 107610, I would like to know if there is one or more missing invoice numbers within that range.

    Thanks and have a great day...

  2. #2
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,250
    The easiest would be to create a new table and populate it with the entire sequence (you can do that in code or even Excel then copy and paste in Access), then use the Find Unmatched query wizard (basically will create a Left Outer join between your tblAllInvoiceSequence.InvoiceNumber to your tblInvoice.InvoiceNumber and put Is Null in the criteria field.
    https://www.fmsinc.com/MicrosoftAcce...oin/index.html

    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  3. #3
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,933
    perhaps code like this?

    Code:
    SELECT tblinv.invdate, M.MissingFrom, M.MissingTo
    FROM tblinv INNER JOIN (SELECT S.invdate, S.[invnum], Max(tblinv.[invnum]+1) AS MissingFrom, S.MissingTo FROM tblinv INNER JOIN (SELECT S.invdate, S.[invnum], S.[invnum]-1 AS MissingTo FROM tblinv AS S LEFT JOIN tblinv AS E ON (S.invdate = E.invdate) AND (S.[invnum]-1=E.[invnum]) WHERE (((E.[invnum]) Is Null)))  AS S ON tblinv.invdate=S.invdate WHERE (((tblinv.[invnum])<[MissingTo])) GROUP BY S.invdate, S.[invnum], S.MissingTo)  AS M ON (tblinv.[invnum] = M.[invnum]) AND (tblinv.invdate = M.invdate)
    ORDER BY tblinv.invdate, M.MissingFrom;
    from this data

    invDate invNum
    26/08/2020 1
    26/08/2020 2
    26/08/2020 4
    26/08/2020 5
    26/08/2020 8
    26/08/2020 9
    26/08/2020 10

    it produces this result

    invdate MissingFrom MissingTo
    26/08/2020 3 3
    26/08/2020 6 7

  4. #4
    jjpuebla is offline Novice
    Windows 10 Access 2016
    Join Date
    Aug 2020
    Posts
    4
    Gicu, thanks for your reply and I already try your solution. However, the database it is going to be used for users that I would like them to use the least effort and offer a direct solution (press a button and magicly, the missings numbers appear).

  5. #5
    jjpuebla is offline Novice
    Windows 10 Access 2016
    Join Date
    Aug 2020
    Posts
    4
    Ajax thanks for your reply, however I am kind of lost. The first table you wrote I think is "tblinv". Where are the other tables "S" and "M". Sorry if it is a dumb question, kind of a beginner here...

  6. #6
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,933
    S is an alias of tblInv > 'tblinv AS S'

    as is E > '
    tblinv AS E'

    S is also an alias for a query (sorry, could have given it a different letter)
    Code:
    (SELECT S.invdate, S.[invnum], S.[invnum]-1 AS MissingTo 
    FROM tblinv AS S LEFT JOIN tblinv AS E ON (S.invdate = E.invdate) AND (S.[invnum]-1=E.[invnum]) 
    WHERE (((E.[invnum]) Is Null)))  AS S
    And M is an alias for another query
    Code:
    (SELECT S.invdate, S.[invnum], Max(tblinv.[invnum]+1) AS MissingFrom, S.MissingTo 
    FROM tblinv INNER JOIN (SELECT S.invdate, S.[invnum], S.[invnum]-1 AS MissingTo FROM tblinv AS S LEFT JOIN tblinv AS E ON (S.invdate = E.invdate) AND (S.[invnum]-1=E.[invnum]) WHERE (((E.[invnum]) Is Null)))  AS S ON tblinv.invdate=S.invdate 
    WHERE (((tblinv.[invnum])<[MissingTo])) 
    GROUP BY S.invdate, S.[invnum], S.MissingTo)  AS M
    you can write these other queries as separate queries if you wish and then combine into 1 query

    And I use alias's to reduce the amount of typing and to indicated what the alias is about

    S=Start
    E=End
    M=Missing

    (it works for me)

    I have the code as a broad template query for this sort of scenario which can be used in many tables just by making a few simple changes. So if this was for say identifying missing employee hours I might change tblInv to tblEmpTimes, invnum to startTime and invDate to ClockDate- the alias's stay the same

  7. #7
    jjpuebla is offline Novice
    Windows 10 Access 2016
    Join Date
    Aug 2020
    Posts
    4
    Thanks for your response, now everything it is clear!

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

Similar Threads

  1. Add letters to sequential numbers
    By bassplayer79 in forum Programming
    Replies: 29
    Last Post: 09-12-2013, 04:20 AM
  2. Sequential numbers...
    By jlgray0127 in forum Programming
    Replies: 1
    Last Post: 04-03-2013, 10:06 AM
  3. Sequential Numbers
    By cactuspete13 in forum Queries
    Replies: 5
    Last Post: 03-27-2013, 12:14 PM
  4. Replies: 2
    Last Post: 06-11-2011, 10:39 PM
  5. Replies: 3
    Last Post: 10-18-2009, 08:38 PM

Tags for this Thread

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