19
ห้อง MS Access / : สอบถามการทำ RunID Invoice ในกรณีที่มีผู้ใช้งานพร้อมกันหลายๆคน
« เมื่อ: 09 ม.ค. 62 , 23:51:24 »
กำหนดให้เป็น Primary Key ถ้าเลขเดียวกันมันก็จะเพิ่มเข้าไปในตารางไม่ได้ จะเกิด Error แจ้งมา
This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
Dim KeyShow() 'กำหนด array ใน form scope เพื่อให้ทุก sub หรือ func มองเห็น
Private Sub Form_Load()
SetKeyShow "visit_No", "VD" 'ตอนโหลดข้อมูลเสร็จให้สร้าง array ที่เก็บ PK แรกของค่าที่ไม่ซ้ำ
End Sub
Private Sub SetKeyShow(PK_Name As String, FieldShow_Name As String)
Dim MyRS As Recordset, CurrentValue As Variant
ReDim Preserve KeyShow(0 To 0) 'set ขนาด array มีค่า 1 ตัว
Set MyRS = Me.RecordsetClone
If MyRS.RecordCount > 0 Then
MyRS.MoveFirst
KeyShow(0) = MyRS(PK_Name) 'บรรทัดแรกต้องแสดงอยู่แล้ว เก็บ PK ไว้
Do While Not MyRS.EOF
CurrentValue = MyRS(FieldShow_Name) 'เก็บค่าบรรทัดปัจจุบันไว้
MyRS.MoveNext
If Not MyRS.EOF Then
If MyRS(FieldShow_Name) <> CurrentValue Then 'ถ้าบรรทัดถัดไปไม่ซ้ำกับบรรทัดปัจจุบันก็ดำเนินการเก็บค่าไว้
ReDim Preserve KeyShow(0 To UBound(KeyShow) + 1) 'เพิ่มขนาด array
KeyShow(UBound(KeyShow)) = MyRS(PK_Name) 'เก็บค่า PK ที่ไม่ซ้ำเพิ่มใน array
End If
End If
Loop
End If
MyRS.Close
Set MyRS = Nothing
End Sub
Private Function IsShow(PK As Variant) As Boolean
Dim i As Long
'ตรวจสอบค่า PK ที่จะต้องแสดง
IsShow = False
For i = LBound(KeyShow) To UBound(KeyShow)
If KeyShow(i) = PK Then
IsShow = True
Exit For
End If
Next i
End Function