2017-08-03 33 views
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 
+0

爲什麼你的程序之外顯示的代碼?那'如果SOS ...'應該導致編譯錯誤。 – June7

回答

0

從循環中退出函數繞過關閉和清除記錄集對象的行。這不是導致問題,但爲什麼讓他們,如果他們沒有使用每次記錄集打開?

以下修訂程序爲我工作,返回相應的序列時,我調用函數從VBA即時窗口:

Public Function fRetNextInSequence() As Long 
Dim MyDB As DAO.Database 
Dim rst As DAO.Recordset 
Dim rstClone As DAO.Recordset 

If Nz(DMin("strSerialNumber", "tblOrderData", "dtmDateOrdered=Date()"), 0) <> 8000 Then 
    'If there are no Records or the gap is 8000 for current date, Function returns 8000 
    fRetNextInSequence = 8000 
Else 
    Set MyDB = CurrentDb 
    Set rst = MyDB.OpenRecordset("SELECT strSerialNumber FROM tblOrderData WHERE dtmDateOrdered=Date() ORDER BY strSerialNumber", 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 
      'Found the Gap! 
      fRetNextInSequence = (rstClone![strSerialNumber] + 1) 
      Exit Do 
     End If 
     .MovePrevious    'Move in sync, 1 Record apart 
     rstClone.MovePrevious 
    Loop 
    End With 

    If fRetNextInSequence = 0 Then 
     'No Gap found, return next number in sequence! 
     rst.MoveLast 
     fRetNextInSequence = (rst![strSerialNumber] + 1)  
    End If 
    rstClone.Close 
    rst.Close 
    Set rstClone = Nothing 
    Set rst = Nothing 
End If 
End Function