简体   繁体   English

MS Access查询以在数字存储在“短文本”字段中时查找连续数字中的间隔

[英]MS Access Query to find gaps in sequential numbers when numbers are stored in Short Text field

I have a table (tblParts) with a PartNumber field (Short Text) which stores 6 digit part numbers for parts belonging to several families. 我有一个带有PartNumber字段(短文本)的表(tblParts),其中存储着属于几个家族的零件的6位零件号。 The families are denoted by the first 2 digits of the part number (00, 01, 02, etc). 系列由零件号的前两位数字表示(00、01、02等)。 (NOTE: I did not create this table and am not able to change it at this time) (注意:我没有创建此表,目前无法更改它)

I need to find gaps in the numbering in order to fill in unused part numbers. 我需要找到编号上的空白以填写未使用的零件号。 If I have a project starting that needs 6 consecutive part numbers in a specific family, I want to find the first unused number in the first gap of that size or greater within that family. 如果我的项目开始时需要特定系列中的6个连续零件号,那么我想在该系列中该大小或更小的第一个间隙中找到第一个未使用的编号。

Here is a small subset of the data. 这是数据的一小部分。

PartNumber
020001
020002
020003
020004
020005
020006
020007
020009
020010
020011
020012
020013
020014
020019
020101

If I needed a single number, the query should find 020008. If I needed 3 numbers, it should find 0200015 and if I needed 10 numbers it should find 020020. 如果我需要一个数字,则查询应找到020008。如果我需要3个数字,则应找到0200015,如果我需要10个数字,则应找到020020。

My SQL knowledge is very limited but I am trying to learn. 我的SQL知识非常有限,但我正在尝试学习。 I realize this would be much easier if the information was stored properly but I have no control over it. 我意识到,如果信息存储正确,这会容易得多,但是我无法控制它。

I once wrote an article on the subject: 我曾经写过一篇关于该主题的文章:

Find and Generate Missing Values in an Access Table 在访问表中查找并生成缺失值

but that will fill up any gap until all new numbers were established. 但这将填补所有空白,直到建立所有新数字。 So, that code will need an expansion with an outer loop to ensure juxtaposed numbers at all times. 因此,该代码将需要使用外部循环进行扩展,以确保始终保持并置数字。

Private Sub btnSearch_Click()

' Read table/query sequentially to 
' record all missing IDs.
' Fill a ListBox with missing values.
' A reference to Microsoft DAO must be 
' present.

  ' Define search table or query.
  Const cstrTable As String = "Orders"
  Const cstrField As String = "OrderID"

  Dim dbs As DAO.Database
  Dim rst As DAO.Recordset
  Dim lst As ListBox
  Dim col As Collection

  Dim strSQL As String
  Dim strList As String
  Dim lngLast As Long
  Dim lngNext As Long
  Dim lngMiss As Long

  strSQL = "Select " & cstrField & "" _
   & " From " & cstrTable & _
   & " Order By 1;"  

  Set lst = Me!lstMissing
  Set col = New Collection
  Set dbs = CurrentDb
  Set rst = dbs.OpenRecordset(strSQL)

  If rst.RecordCount = 0 Then
    'The recordset is empty.
    'Nothing to do.
  Else
    lngLast = rst(cstrField).Value
    rst.MoveNext
    While rst.EOF = False
      lngNext = rst(cstrField).Value
      For lngMiss = lngLast + 1 To _
       lngNext - 1
        col.Add (lngMiss)
      Next
      lngLast = lngNext
      rst.MoveNext
    Wend
      'Generate next value in sequence.
      'Discard if collecting only 
      'missing values.
      col.Add (lngLast + 1)
  End If
  rst.Close

  'Populate list box from collection.
  For lngMiss = 1 To col.Count
    If Len(strList) > 0 Then 
      strList = strList & ";"
    End If
    strList = strList & col(lngMiss)
    Debug.Print col(lngMiss)
  Next
  lst.RowSource = strList
  Debug.Print strList

  Set rst = Nothing
  Set dbs = Nothing
  Set col = Nothing
  Set lst = Nothing

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM