Try this
Code:
Option Compare Database
Option Explicit
Private Sub cmdUpdateProductTables_Click()
10 On Error GoTo cmdUpdateProductTables_Click_Error
Dim fieldForIndex As String
20 fieldForIndex = "MSHP_LEGACY_REPORT_NUMBER"
Dim sSQL1 As String, sSQL2 As String, sqlFinal As String
Dim indexName As String
Dim i As Integer
30 sSQL1 = "CREATE UNIQUE INDEX "
40 sSQL2 = " ON "
'Declare variables
Dim tdf As DAO.TableDef
'Perform count of the product tables
50 For Each tdf In CurrentDb.TableDefs
60 If Left(tdf.Name, 4) = "tbl_" Then
70 i = i + 1
80 sqlFinal = sSQL1 & tdf.Name & i & sSQL2 & tdf.Name & "(" & fieldForIndex & ");"
90 Debug.Print sqlFinal
100 CurrentDb.Execute sqlFinal, dbFailOnError
'Commented out next line since I want to alter **all** tables that have table name prefix = "tbl_"
'ALTER TABLE tbl_PERSON
' Alter TABLE tdf
'I don't want to add a primary key (e.g., autonumber).
'Instead, all tables with prefix "tbl_" will contain field [MSHP_LEGACY_REPORT_NUMBER].
'ADD PRIMARY KEY (ID);
'... for example SET [MSHP_LEGACY_REPORT_NUMBER].INDEX = "Yes (No Duplicates)"
' CREATE UNIQUE INDEX index_name ON tdf (MSHP_LEGACY_REPORT_NUMBER);
110 End If
120 Next tdf
'Throw message box
130 MsgBox "If VBA is correct, then INDEXED property should have been updated.", vbInformation, "Status Message"
140 On Error GoTo 0
cmdUpdateProductTables_Click_Exit:
150 Exit Sub
cmdUpdateProductTables_Click_Error:
160 MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure cmdUpdateProductTables_Click, line " & Erl & "."
170 GoTo cmdUpdateProductTables_Click_Exit
End Sub