Coleção de índices (DAO)
Aplica-se ao: Access 2013, Office 2013
Uma coleção Indexes contém todos os objetos Index armazenados de um objeto TableDef (apenas espaços de trabalho do Microsoft Access).
Comentários
Quando acessar um objeto Recordset do tipo tabela, use a propriedade Index do objeto para especificar a ordem dos registros. Defina essa propriedade para a configuração da propriedade Name de um objeto Index existente na coleção Indexes do objeto TableDef, subjacente ao objeto Recordset.
Observação
[!OBSERVAçãO] É possível usar o método Append ou Delete numa coleção Indexes apenas se a configuração da propriedade Updatable do objeto contentor TableDef for True.
Depois de criar um novo objeto Index, você deverá usar o método Append para adicioná-lo à coleção Indexes do objeto TableDef.
Importante
[!IMPORTANTE] Verifique se seus dados são adequados aos atributos de seu novo índice. Se o seu índice exigir valores únicos, assegure que não existam duplicatas nos registros de dados existentes. Se existirem duplicatas, o mecanismo do banco de dados do Microsoft Access não poderá criar o índice; ocorrerá um erro interceptável se você tentar usar o método Append no novo índice.
Exemplo
Este exemplo cria um novo objeto Index, acrescenta-o à coleção Indexes do TableDef Employees e enumera a coleção Indexes do TableDef. Por último, enumera um Recordset, primeiramente usando o Index principal em, em seguida, usando o novo Index. O procedimento IndexOutput é exigido para a execução deste procedimento.
Sub IndexObjectX()
Dim dbsNorthwind As Database
Dim tdfEmployees As TableDef
Dim idxNew As Index
Dim idxLoop As Index
Dim rstEmployees As Recordset
Set dbsNorthwind = OpenDatabase("Northwind.mdb")
Set tdfEmployees = dbsNorthwind!Employees
With tdfEmployees
' Create new index, create and append Field
' objects to its Fields collection.
Set idxNew = .CreateIndex("NewIndex")
With idxNew
.Fields.Append .CreateField("Country")
.Fields.Append .CreateField("LastName")
.Fields.Append .CreateField("FirstName")
End With
' Add new Index object to the Indexes collection
' of the Employees table collection.
.Indexes.Append idxNew
.Indexes.Refresh
Debug.Print .Indexes.Count & " Indexes in " & _
.Name & " TableDef"
' Enumerate Indexes collection of Employees
' table.
For Each idxLoop In .Indexes
Debug.Print " " & idxLoop.Name
Next idxLoop
Set rstEmployees = _
dbsNorthwind.OpenRecordset("Employees")
' Print report using old and new indexes.
IndexOutput rstEmployees, "PrimaryKey"
IndexOutput rstEmployees, idxNew.Name
rstEmployees.Close
' Delete new Index because this is a
' demonstration.
.Indexes.Delete idxNew.Name
End With
dbsNorthwind.Close
End Sub
Sub IndexOutput(rstTemp As Recordset, _
strIndex As String)
' Report function for FieldX.
With rstTemp
' Set the index.
.Index = strIndex
.MoveFirst
Debug.Print "Recordset = " & .Name & _
", Index = " & .Index
Debug.Print " EmployeeID - Country - Name"
' Enumerate the recordset using the specified
' index.
Do While Not .EOF
Debug.Print " " & !EmployeeID & " - " & _
!Country & " - " & !LastName & ", " & !FirstName
.MoveNext
Loop
End With
End Sub
Este exemplo usa o método CreateIndex para criar dois novos objetos Index e acrescenta-os à coleção Indexes do objeto TableDef de Employees. Em seguida, é enumerada a coleção Indexes do objeto TableDef, a coleção Fields dos novos objetos Index e a coleção Properties dos novos objetos Index. A função CreateIndexOutput é exigida para a execução deste procedimento.
Sub CreateIndexX()
Dim dbsNorthwind As Database
Dim tdfEmployees As TableDef
Dim idxCountry As Index
Dim idxFirstName As Index
Dim idxLoop As Index
Set dbsNorthwind = OpenDatabase("Northwind.mdb")
Set tdfEmployees = dbsNorthwind!Employees
With tdfEmployees
' Create first Index object, create and append Field
' objects to the Index object, and then append the
' Index object to the Indexes collection of the
' TableDef.
Set idxCountry = .CreateIndex("CountryIndex")
With idxCountry
.Fields.Append .CreateField("Country")
.Fields.Append .CreateField("LastName")
.Fields.Append .CreateField("FirstName")
End With
.Indexes.Append idxCountry
' Create second Index object, create and append Field
' objects to the Index object, and then append the
' Index object to the Indexes collection of the
' TableDef.
Set idxFirstName = .CreateIndex
With idxFirstName
.Name = "FirstNameIndex"
.Fields.Append .CreateField("FirstName")
.Fields.Append .CreateField("LastName")
End With
.Indexes.Append idxFirstName
' Refresh collection so that you can access new Index
' objects.
.Indexes.Refresh
Debug.Print .Indexes.Count & " Indexes in " & _
.Name & " TableDef"
' Enumerate Indexes collection.
For Each idxLoop In .Indexes
Debug.Print " " & idxLoop.Name
Next idxLoop
' Print report.
CreateIndexOutput idxCountry
CreateIndexOutput idxFirstName
' Delete new Index objects because this is a
' demonstration.
.Indexes.Delete idxCountry.Name
.Indexes.Delete idxFirstName.Name
End With
dbsNorthwind.Close
End Sub
Function CreateIndexOutput(idxTemp As Index)
Dim fldLoop As Field
Dim prpLoop As Property
With idxTemp
' Enumerate Fields collection of Index object.
Debug.Print "Fields in " & .Name
For Each fldLoop In .Fields
Debug.Print " " & fldLoop.Name
Next fldLoop
' Enumerate Properties collection of Index object.
Debug.Print "Properties of " & .Name
For Each prpLoop In .Properties
Debug.Print " " & prpLoop.Name & " - " & _
IIf(prpLoop = "", "[empty]", prpLoop)
Next prpLoop
End With
End Function