รบกวนสอบถาม Auto Number แบบมีเงื่อนไขครับ
กระทู้เก่าบอร์ด อ.Yeadram

 1,130   3
URL.หัวข้อ / URL
รบกวนสอบถาม Auto Number แบบมีเงื่อนไขครับ

ผมจะรันเลขโดยให้รันแบบนี้
H601-2559-001 โดย
2559   คือ   ปี
001     คือ   ตัวรันตัวเลข โดยให้รันจนถึงเลข 150 แล้วรันใหม่เป็น 1

คือผมค้นหาหลายกระทู้แต่ยังไม่เจอ การรันแบบมีเงื่อนไข รบกวนสอบถามด้วยครับ ขอบคุณมากครับผม

3 Reply in this Topic. Dispaly 1 pages and you are on page number 1

1 @R22645
สอบถามแก้ไขเพิ่มเติมครับ
H601-2559-60-001 โดย

H601   คือ ตัวเลขที่กำหนดไว้ตายตัวไม่เปลี่ยนแปลง
2559   คือ   ปี
60       คือ   กล่อง
001     คือ   ตัวรันตัวเลข

โดยเมื่อรันตัวเลขถึง 150 ให้ เริ่มเลข 1 ใหม่ พร้อมให้เลขกล่องเปลี่ยนจาก 60 เป็น 61

ไม่ทราบว่าต้องทำอย่างไรบ้างครับ รบกวนด้วยครับผม

ขอบคุณมากครับผม
2 @R22648
แบบนี้มันต้องเขียนโปรแกรม VBA แล้ว   โดยใช้ Table หนึ่งเก็บค่า 4 field ที่คุณนำมายกตัวอย่าง คือ H601,2559,60,001     จากนั้นเมื่อมีการป้อนข้อมูลที่ Table Transaction เช่น รับ-จ่าย-นับยอด-คืน สินค้า    ก็เขียน VBA โดย Event Update ของ Form ให้มาดูตัวเลขใน Field 4 Field ใน Table ที่ตั้งค่าไว้   และนำเลขที่คุณตั้งค่าไว้นำไปใช้ตามเงื่อนไข   ความคิดผมเป็นแบบนี้ แต่อาจารย์ท่านอื่น ท่านอาจมีวิธีอื่น ๆ ของท่าน
3 @R22649
ขอบคุณมากครับผม
ตอนนี้ผมทำตัวรันตัวเลขมาก่อนโดยสร้างโมดูลดังนี้ครับ

Public Function NewCustID() As Long

On Error GoTo NextID_Err

Dim lngNextID As Long

    'Find highest Employee ID in the tblPracticeEmployeeData table and add 1
    lngNextID = DMax("[lngID]", "tblPracticeEmployeeData") + 1

    'Assign function the value of the Next ID
    NewCustID = lngNextID

    'Exit function now after successful incrementing or after error message
Exit_NewCustID:
Exit Function

    'If an error occurred, display a message, then go to Exit statement
NextID_Err:
    MsgBox "Error " & Err & ": " & Error$

    Resume Exit_NewCustID

End Function

ส่วนในฟอร์มเขียนโค้ดดังนี้คับ

Option Compare Database

Private Sub cmdNewId_Click()
'On click of button a new Customer ID is generated and
'focus is moved to strLastName field.
    Me![lngId] = NewCustID()
    Me![strLastName].SetFocus
    Me![cmdNewId].Enabled = False
End Sub

Public Sub EnableIfNull()
    
    'Attached to On Current property of Employee form
    'Checks to see if all text boxes are null
    'and then disables or enables the cmdNewID button
    
    Dim intControlCounter As Integer
    Dim binAllNull As Boolean
    Dim MyForm As Form
    Dim MyControl As Control

    'Set initial values for form and flag
    Set MyForm = Me
    binAllNull = True
    
    'Look at all text boxes to check if they are null
    For intControlCounter = 0 To MyForm.Count - 1
        Set MyControl = MyForm(intControlCounter)
    
       If TypeOf MyControl Is TextBox Then
           If Len(MyControl) > 0 Then 'The Value property is assumed (MyControl is same as MyControl.Value)
              binAllNull = False
              Exit For               'Exit the For loop after the first non-null textbox is found
            End If
        End If

     Next intControlCounter
     'If all text boxes are populated, there is no need for cmdNewID button
     'to be available
    If binAllNull = True Then
        cmdNewId.Enabled = True
    Else
        cmdNewId.Enabled = False
    End If


End Sub

Private Sub Form_Current()
EnableIfNull
End Sub
BLUEBLUEBLUE

ตรงนี้รันแค่ตัวเลขไปเรื่อยๆโดยไม่มีเงื่อนไขดังกล่าวคับ ต้องแก้ตรงไหนบ้างคับ ขอบคุณมากคับผม
@ ประกาศใช้งานเว็บบอร์ดใหม่ => บอร์ดเรียนรู้ Access สำหรับคนไทย
แล้วจะใส่ลิ้งอ้างอิงมาที่โพสต์เก่านี้หรือไม่ก็ตามสะดวกครับ
Time: 0.2949s