Sub Create_tblTableIndexes()
Dim db As DAO.Database
Dim ThisDB As DAO.Database
Dim tblLoop As DAO.TableDef
Dim fldLoop As DAO.Field
Dim idxLoop As DAO.Index
Dim TD1 As DAO.TableDef
Dim QD1 As DAO.QueryDef
Dim TempSet1 As DAO.Recordset
Dim Position As Integer
Dim CountIndexes As Integer
Dim strDatabase As String
'strDatabase = "C:\hartman\ImportDB2.mdb"
strDatabase = Forms!frmPrintDoc!txtDBName
CountIndexes = 0
Set ThisDB = CurrentDb()
If strDatabase = "" Then
Set db = CurrentDb()
Else
Set db = DBEngine.Workspaces(0).OpenDatabase(strDatabase)
End If
db.Containers.Refresh
Set QD1 = ThisDB.QueryDefs!QdeltblTableIndexes
QD1.Execute
Set TD1 = ThisDB.TableDefs!tblTableIndexes
Set TempSet1 = TD1.OpenRecordset
' Loop through TableDefs collection.
For Each tblLoop In db.TableDefs
' Enumerate Fields collection of each
' TableDef object.
'' add error checking for 3024 - not found in collection
'' add error checking for 3110 - no permission to read msysmodules2
On Error GoTo ErrorHandler
For Each idxLoop In tblLoop.Indexes
CountIndexes = CountIndexes + 1
Forms!frmPrintDoc!txtIndexCount = CountIndexes
Forms!frmPrintDoc!txtIndexName = tblLoop.Name
Forms!frmPrintDoc.Repaint
If Left(tblLoop.Name, 4) = "MSys" Or Left(tblLoop.Name, 1) = "z" Or Left(tblLoop.Name, 1) = "~" Then
Else
Position = 1
For Each fldLoop In idxLoop.Fields
TempSet1.AddNew
TempSet1!IndexName = idxLoop.Name
TempSet1!Unique = idxLoop.Unique
TempSet1!OrdinalPosition = Position
TempSet1!FieldName = fldLoop.Name
TempSet1.Update
Position = Position + 1
Next fldLoop
End If
Next idxLoop
Next tblLoop
db.Close
Exit Sub
ErrorHandler:
Select Case Err.Number
Case 3110
MsgBox "Open " & strDatabase & " and change the admin security to allow read for MSysModules", vbOKOnly
Case 3043
MsgBox "Please select a valid database", vbOKOnly
Case 91 ' db was not opened so it cannot be closed.
Exit Sub
Case Else
MsgBox Err.Number & "-" & Err.Description
End Select
End Sub