auto increment primary key กำหนด ค่าเริ่มต้น ค่าสุดท้าย ก่อนเริ่มนับรอบใหม่


0 สมาชิก และ 1 บุคคลทั่วไป กำลังดูหัวข้อนี้

30 เม.ย. 61 , 09:55:21
อ่าน 1774 ครั้ง

preechaaesanan

พอดีเจอ Code มาจากกระทู้นึง ของสมาชิกชื่อ Suchat/ชลบุรี

เลยเอามาปรับเพื่อให้เข้ากับที่ต้องการใช้งานนิดหน่อย แต่ เจอปัญหาว่า มัน เพิ่มไปเรื่อยๆ เกินที่ ต้องการ 
ที่ต้องการคือ ต้องการ Run ID แค่  yymmddxx  โดย  xx จะเริ่ม ตั้ง 00 จนถึง 99 พอ ครบแล่วเริ่ม นับ จาก 00 ---> 99 ใหม่
แต่จาก code ตัวด้านล่าง  มันจะนับพวกเพิ่มไปเรื่อยๆ เลย



Private Sub ID_DblClick(Cancel As Integer)
Dim strDate As String
Dim intNum As Integer, intMax As Integer
Dim strSuffix As String

strDate = Format(date, "yymmdd")

If Me.ID = "" Or IsNull(Me.ID) Then
        If IsNull(DMax("Val(Mid([ID],7))", "table1", "Left([ID],6) = '" & strDate & " '")) Then
            Me.ID = strDate & "00"
            'Debug.Print "1"
        Else
            intMax = DMax("Val(Mid([ID],7))", "table1", "Left([ID],6) = '" & strDate & " '")
            intMax = intMax + 1
            Me.ID = strDate & Format(intMax, "00")
            'Debug.Print "1"
        End If

End If

End Sub

 

30 เม.ย. 61 , 11:33:39
ตอบกลับ #1

pizza_p

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 มันจะเซฟไม่ได้นะครับเพราะจะมีค่าซ้ำ

 
โพสต์นี้ได้รับคำขอบคุณจาก: preechaaesanan

30 เม.ย. 61 , 14:05:23
ตอบกลับ #2

preechaaesanan

ขอบคูณมากก ๆ ครับ @pizza_p คิดว่า คงพอเป็นแนวทาง ให้ท่านที่สนใจ ไปประยุกต์ใช้กันต่อนะครับ

 

02 ก.ค. 61 , 16:06:21
ตอบกลับ #3

preechaaesanan

ถามเพิ่ม เติม นิดนึงนะครับ   ถ้า เรา จะ ให้มี ตัวอักขระ นำหน้า  เช่น  NC-61070201    หรือ  ตามหลัง เช่น  61070201NC
ต้องตั้งค่าอย่างไรครับ

 

02 ก.ค. 61 , 19:01:00
ตอบกลับ #4

ปิ่นณรงค์

ถามเพิ่ม เติม นิดนึงนะครับ   ถ้า เรา จะ ให้มี ตัวอักขระ นำหน้า  เช่น  NC-61070201    หรือ  ตามหลัง เช่น  61070201NC
ต้องตั้งค่าอย่างไรครับ

ถ้าต่อหลังแบบ 61070201NC กำหนดดังนี้

Private Sub ID_DblClick(Cancel As Integer)
Dim strDate As String
Dim intNum As Integer, intMax As Variant
Dim strSuffix As String
Dim strID As String
strID = "NC"

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") & strID
End If
End Sub


ถ้าทำแบบต่อด้านหน้า แบบ NC-61070201 กำหนดดังนี้

Private Sub ID_DblClick(Cancel As Integer)
Dim strDate As String
Dim intNum As Integer, intMax As Variant
Dim strSuffix As String
Dim strID As String
strID = "NC-"
strDate = strID & Format(Date, "yymmdd")

intMax = DMax("Val(Mid([ID],10))", "table1", "Left([ID],9) = '" & 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


« แก้ไขครั้งสุดท้าย: 02 ก.ค. 61 , 19:11:39 โดย ปิ่นณรงค์ »
:love: :grin:
 
โพสต์นี้ได้รับคำขอบคุณจาก: preechaaesanan

02 ก.ค. 61 , 20:23:28
ตอบกลับ #5

preechaaesanan

ขอบคุณมากๆครับ

 


บอร์ดเรียนรู้ Access สำหรับคนไทย


 

Sitemap 1 2 3 4 5