步驟一:
建立一個處理編號記錄的『訂單編號』資料表,欄位只需要兩個:日期、編號。
步驟二:
訂單表單的BeforeInsert事件加入下方的程式碼:
-----------------------------------------------------------------------------------------------------------------------------------------
Private Sub Form_BeforeInsert(Cancel As Integer)
Dim Now_Date As Date
Now_Date = Format(Date, "yyyy/mm/dd")
'訂單編號資料表中尋找是否有今天日期的記錄
d = DLookup("編號", "訂單編號", "日期 =#" & Now_Date & "#")
If IsNull(d) Then '如果沒有,就新增以一筆記錄,日期為今天的日期,編號起始值為0
Set d1 = CurrentDb.OpenRecordset("訂單編號")
d1.AddNew
d1("日期") = Date
d1("編號") = 0
d1.Update
End If
'訂單表單之『訂單編號』欄位填入例如951201-01之訂單編號
Me![訂單編號] = Format(Date, "emmdd") & "-" & Format(d + 1, "00")
End Sub
-----------------------------------------------------------------------------------------------------------------------------------------
步驟三:
訂單表單的BeforeUpdate事件加入下方的程式碼:
-----------------------------------------------------------------------------------------------------------------------------------------
Private Sub Form_BeforeUpdate(Cancel As Integer)
'將訂單編號最後的兩位數值,回填訂單編號資料表之編號欄位,以記錄今天訂單編號最後使用的數值
If Me.NewRecord = True Then
Set b = CurrentDb.OpenRecordset("select * from 訂單編號 where 日期 =#" & Format(Date, "yyyy/mm/dd") & "#")
x = Right(Me![訂單編號], 2) '取得目前訂單編號最後的兩個數值
b.Edit
'取得的訂單編號數值去除前面的數值0,並填入訂單編號資料表的編號欄位中
b("編號") = CInt(x)
b.Update
End If
End Sub
------------------------------------------------------------------------------------------------------------------------------------------