37
ห้อง MS Access / > auto increment primary key กำหนด ค่าเริ่มต้น ค่าสุดท้าย ก่อนเริ่มนับรอบใหม่
« เมื่อ: 30 เม.ย. 61 , 11:33:39 »
Private Sub ID_DblClick(Cancel As Integer)
Dim strDate As String
Dim intNum As Integer, intMax As Variant
Dim strSuffix As String
strDate = Format(date, "yymmdd")
intMax = DMax("Val(Mid([ID],7))", "table1", "Left([ID],6) = '" & strDate & " '")
If Me.ID = "" Or IsNull(Me.ID) Then
If IsNull(intMax) Then
intMax = 0
'Debug.Print "1"
Else
intMax = intMax + 1
If intMax > 99 Then intMax = 0
'Debug.Print "1"
End If
Me.ID = strDate & Format(intMax, "00")
End If
End Sub
เมื่อครบ 99 แล้วให้มันกลับเป็น 00 แต่ระวังถ้า Me.ID เป็น primary key มันจะเซฟไม่ได้นะครับเพราะจะมีค่าซ้ำ
Dim strDate As String
Dim intNum As Integer, intMax As Variant
Dim strSuffix As String
strDate = Format(date, "yymmdd")
intMax = DMax("Val(Mid([ID],7))", "table1", "Left([ID],6) = '" & strDate & " '")
If Me.ID = "" Or IsNull(Me.ID) Then
If IsNull(intMax) Then
intMax = 0
'Debug.Print "1"
Else
intMax = intMax + 1
If intMax > 99 Then intMax = 0
'Debug.Print "1"
End If
Me.ID = strDate & Format(intMax, "00")
End If
End Sub
เมื่อครบ 99 แล้วให้มันกลับเป็น 00 แต่ระวังถ้า Me.ID เป็น primary key มันจะเซฟไม่ได้นะครับเพราะจะมีค่าซ้ำ