วิธีการตรวจสอบว่า ตาราง ใน Access มีคนเปิดใช้อยู่
กระทู้เก่าบอร์ด อ.สุภาพ ไชยา

 343   2
URL.หัวข้อ / URL
วิธีการตรวจสอบว่า ตาราง ใน Access มีคนเปิดใช้อยู่

คือดิฉันทำโครงงานโดยใช้ VB+Access  
และมีปัญหาหนึ่งที่ ที่ขอรบกวนผู้รู้ช่วยชี้แนะ 
หน่อยคะ ปัญหาก็คือ ต้องการตรวจสอบว่า 
ถ้ามีผู้ใช้รายหนึ่งเปิด Recordset ของ 
ตาราง A ในฐานข้อมูล Access จะใช้คำสั่ง 
หรือวิธีการใด ในการตรวจสอบว่า ตาราง A 
มีคนกำลังเปิดอยู่  
รบกวนผู้รู้ช่วยขี้แนะด้วยคะ 


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

1 @R01100
การเปิด Recordset ทุกคนสามารถเปิดเข้ามาได้ แต่จะเปิดแบบ Read Only หรือ Edit ก็คงต้องขึ้นอยู่กับความต้องการ การทำงานแบบนี้จะช่วยให้สามารถทำงานได้หลายคนพร้อมๆ กัน ผมไม่ทราบว่าความต้องการที่จะตรวจดูว่าใครเปิด Recordset นี้บ้าง จะใช้ในวัตถุประสงค์อะไรครับ ถ้าต้องการที่จะกันไม่ให้มีการแก้ไขข้อมูลซ้ำซ้อนกัน ก็ควรจะดูว่า มีใครกำลังแก้ไขข้อมูลใดบ้างมากกว่าครับ ลองดู LockEdits property ใน Help ของ DAO ดูครับ ปกติจะอยู่ในห้อง C:\Program Files\Common Files\Microsoft Shared\dao ถ้าเรากำหนดให้เป็น True ข้อมูลใน Record ที่เราเรียกใช้ จะถูก Locked ตั้งแต่บันทัด .Edit ถ้าเรากำหนดให้เป็น False ข้อมูลใน Record ที่เราเรียกใช้ จะถูก Locked ที่บันทัด .Update การ Lock แบบนี้ จะเป็น Page Locking เมื่อ User ท่านอื่นจะแก้ไข Record ที่ถูก Locked นี้ จะมีข้อความแจ้งให้เขารู้ได้ ฉะนั้นการ Lock ตั้งแต่บันทัด .Edit จะช่วยกันไม่ให้คนอื่นเข้ามาแก้ไขซ้ำกันได้ตั้งแต่เนินๆ ครับ นี่คือตัวอย่างโค้ดที่นำมาจาก Help ของ DAO ครับ Sub LockEditsX() Dim dbsNorthwind As Database Dim rstCustomers As Recordset Dim strOldName As String Set dbsNorthwind = OpenDatabase("Northwind.mdb") Set rstCustomers = _ dbsNorthwind.OpenRecordset("Customers", _ dbOpenDynaset) With rstCustomers ' Store original data. strOldName = !CompanyName If MsgBox("Pessimistic locking demonstration...", _ vbOKCancel) = vbOK Then ' Attempt to modify data with pessimistic locking ' in effect. If PessimisticLock(rstCustomers, !CompanyName, _ "Acme Foods") Then MsgBox "Record successfully edited." ' Restore original data... .Edit !CompanyName = strOldName .Update End If End If If MsgBox("Optimistic locking demonstration...", _ vbOKCancel) = vbOK Then ' Attempt to modify data with optimistic locking ' in effect. If OptimisticLock(rstCustomers, !CompanyName, _ "Acme Foods") Then MsgBox "Record successfully edited." ' Restore original data... .Edit !CompanyName = strOldName .Update End If End If .Close End With dbsNorthwind.Close End Sub Function PessimisticLock(rstTemp As Recordset, _ fldTemp As Field, strNew As String) As Boolean dim ErrLoop as Error PessimisticLock = True With rstTemp .LockEdits = True ' When you set LockEdits to True, you trap for errors ' when you call the Edit method. On Error GoTo Err_Lock .Edit On Error GoTo 0 ' If the Edit is still in progress, then no errors ' were triggered; you may modify the data. If .EditMode = dbEditInProgress Then fldTemp = strNew .Update .Bookmark = .LastModified Else ' Retrieve current record to see changes made by ' other user. .Move 0 End If End With Exit Function Err_Lock: If DBEngine.Errors.Count > 0 Then ' Enumerate the Errors collection. For Each errLoop In DBEngine.Errors MsgBox "Error number: " & errLoop.Number & _ vbCr & errLoop.Description Next errLoop PessimisticLock = False End If Resume Next End Function Function OptimisticLock(rstTemp As Recordset, _ fldTemp As Field, strNew As String) As Boolean dim ErrLoop as Error OptimisticLock = True With rstTemp .LockEdits = False .Edit fldTemp = strNew ' When you set LockEdits to False, you trap for errors ' when you call the Update method. On Error GoTo Err_Lock .Update On Error GoTo 0 ' If there is no Edit in progress, then no errors were ' triggered; you may modify the data. If .EditMode = dbEditNone Then ' Move current record pointer to the most recently ' modified record. .Bookmark = .LastModified Else .CancelUpdate ' Retrieve current record to see changes made by ' other user. .Move 0 End If End With Exit Function Err_Lock: If DBEngine.Errors.Count > 0 Then ' Enumerate the Errors collection. For Each errLoop In DBEngine.Errors MsgBox "Error number: " & errLoop.Number & _ vbCr & errLoop.Description Next errLoop OptimisticLock = False End If Resume Next End Function * ส่วนที่ต้องการดูว่ามีการเปิด Table หรือ Query หรือไม่นั้น คงจะไม่เกิดประโยชน์มากเท่าไร เพราะทุกคนสามารถที่จะเปิดแบบ Read Only ได้ ผมอาจจะผิดก็ได้ครับ ท่านอื่นคิดเห็นอย่างไร ก็ร่วมแสดงความคิดเห็นได้
2 @R01109
ขอบคุณคะ เข้าใจว่าสามารถจะกำหนดให้ User เปิดTable แบบ Read Only ได้ แต่หนูอยากจะรู้วิธีการตรวจสอบว่า Table ถูกเปิดใช้อยู่ นะคะ
@ ประกาศใช้งานเว็บบอร์ดใหม่ => บอร์ดเรียนรู้ Access สำหรับคนไทย
แล้วจะใส่ลิ้งอ้างอิงมาที่โพสต์เก่านี้หรือไม่ก็ตามสะดวกครับ
Time: 0.0906s