เกี่ยวกับ index ของ recordset
กระทู้เก่าบอร์ด อ.สุภาพ ไชยา

 228   2
URL.หัวข้อ / URL
เกี่ยวกับ index ของ recordset

ขอรบกวนอาจารย์สุภาพ และท่านผู้รู้ทุกท่านช่วยแนะนำหน่อยค่ะ คือ การกำหนดคำสั่ง
recordset.Index ="ชื่อ index "
Index ที่ใช้กับ Recordset มีเงื่อนไขอะไรบ้างค่ะ ทำทีไร error บรรทัด Index ทุกที
เช่นถ้าฟิลด์ที่หาเป็น Number แบบ Long Integer จะใช้กับ Index recordset ได้ไหม
แล้วถ้าฟิลด์เป็น Primary Key จะทำ Index ได้หรือไม่ แล้วโปรแกรมจะรู้ได้อย่างไรว่าฟิลด์ใดเป็น key ถ้าตารางนั้น ๆ เรากำหนดดัชนีมากกว่า 1 ฟิลด์ค่ะ


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

1 @R05852
การจะใช้ index ตรงนี้ได้ ตารางต้นสังกัดจะต้องเป็น local ไม่ใช่ linked ครับ

ทุกฟีลด์กำหนดเป็น index ได้หมดครับ

ถ้าฟีลด์ไหนถูกเลือกเป็น primary key มันจะทำ index ให้โดยอัตโนมัติ
ส่วนฟีลด์ที่เชื่อมแบบเป็น foreign key (ฟีลด์ที่ใช้เชื่อมด้านที่เป็น manay) มันก็จะสร้าง index ให้ด้วยเช่นกัน

ถ้าเราทำ index แยกกันกับหลายๆ ฟีลด์ใน 1 ตาราง เราก็ต้องระบุว่าจะใช้ index ตัวไหน เพราะ index แต่ละตัวจะมีชื่อของมันเอง

ถ้าอยากรู้ว่าในตารางเป้าหมาย ได้สร้าง index ไว้กี่ตัวบ้าง ให้ลองดูโค้ดนี้ครับ ผมเอามาจาก Help ของ Access เอง

Sub IndexPropertyX()

Dim dbsNorthwind As Database
Dim tdfEmployees As TableDef
Dim rstEmployees As Recordset
Dim idxLoop As Index

Set dbsNorthwind = OpenDatabase("Northwind.mdb")
Set rstEmployees = _
dbsNorthwind.OpenRecordset("Employees")
Set tdfEmployees = dbsNorthwind.TableDefs!Employees

With rstEmployees

' Enumerate Indexes collection of Employees table.
For Each idxLoop In tdfEmployees.Indexes
.Index = idxLoop.Name
Debug.Print "Index = " & .Index
Debug.Print " EmployeeID - PostalCode - Name"
.MoveFirst

' Enumerate Recordset to show the order of records.
Do While Not .EOF
Debug.Print " " & !EmployeeID & " - " & _
!PostalCode & " - " & !FirstName & " " & _
!LastName
.MoveNext
Loop

Next idxLoop

.Close
End With

dbsNorthwind.Close

End Sub

2 @R05873
ขอตอบเพิ่มจาก อ.ครับ
ผมเคยหาคำตอบตรงนี้หลายครั้งเหมือนกันครับ มาเจอวิธีนี้ ใช้ได้ผลดีเหมือนกันครับ(ปัญหาที่สงสัยหายไปครับ)
Dim dbs As DAO.Database
Dim rst As DAO.Recordset

Private Sub Form_Load()
Set dbs = CurrentDb()
Set rst = dbs.OpenRecordset("ตาราง1")
rst.Index = "PrimaryKey"
End Sub

Private Sub คำสั่ง8_Click()
Dim Ctl As Control
rst.Seek "=", Val(txtNumID), Me.txtName.Value
If rst.NoMatch = True Then
rst.AddNew
Else
rst.Edit
End If
rst.Fields("NumID").Value = Me.txtNumID.Value
rst.Fields("Name").Value = Me.txtName.Value
rst.Fields("Bm").Value = Me.txtBm.Value
rst.Fields("Sc").Value = Me.txtSc.Value
rst.Update
'*************
For Each Ctl In Me.Form.Controls 'ทำ Text box ให้ว่าง
If Ctl.ControlType = acTextBox Or Ctl.ControlType = acComboBox Then
If IsNull(Ctl) = False Then
Ctl = Null
End If
End If
Next Ctl

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