2
我有這個功能,它試圖自動排序一個數字,從數字8000開始,數字每天回到8000。該功能還會嘗試確保沒有間隙,因此如果存在手動輸入並且該數字會創建間隙,則該字段將不會從手動輸入進行排序。但我似乎無法讓代碼正常工作,因爲它與前一個條目保持相同的編號,並且不會增加。DMax編號序列修正差距
Public Function fRetNextInSequence() As Long
Dim MyDB As DAO.Database
Dim rst As DAO.Recordset
Dim rstClone As DAO.Recordset
'If there are no Records in tblData, then have the Function return 8000
If DCount("strSerialNumber", "tblOrderData", "dtmDateOrdered=#" & Date & "#") = 0 Then
fRetNextInSequence = 8000
Exit Function
End If
Set MyDB = CurrentDb
Set rst = MyDB.OpenRecordset("tblOrderData", dbOpenSnapshot)
Set rstClone = rst.Clone
rst.MoveLast 'Move to Last Record [MyNum]
With rstClone 'Move to Next-to-Last Record [MyNum]
.MoveLast
.Move -1 'Clone now at Next-to-Last Record [MyNum]
End With
With rst
Do While Not rstClone.BOF
If Abs(![strSerialNumber] - rstClone![strSerialNumber]) > 1 Then
fRetNextInSequence = (rstClone![strSerialNumber] + 1) 'Found the Gap!
Exit Function
End If
.MovePrevious 'Move in sync, 1 Record apart
rstClone.MovePrevious
Loop
End With
rst.MoveLast
fRetNextInSequence = (rst![strSerialNumber] + 1) 'No Gap found, return next number in sequence!
rstClone.Close
rst.Close
Set rstClone = Nothing
Set rst = Nothing
End Function
If SOS = "ES-S" Then
SerialNbrValue = fRetNextInSequence
'SerialNbrValue = Val(Nz(DMax("strSerialNumber", "tblOrderData", "dtmDateOrdered=#" & Date & "#"), 7999)) + 1
Else
SerialNbrValue = ""
End If
爲什麼你的程序之外顯示的代碼?那'如果SOS ...'應該導致編譯錯誤。 – June7