M
Mike Labosh
I can't find anything in the help or MSDN about this, but this silly doofus
issue just wasted many hours of my time. All I needed to do was spin on the
stupid Fields collection of a TableDef. Newbie code, right? So what's
wrong with the CurrentDB method?!?
This code doesn't work:
Public Sub CreateIndexes(tableName As String)
' Creates an index on each column of the specified table
' except the surrogate key "ID" column
Dim fl As DAO.Field
Dim qd As DAO.QueryDef
Set qd = CurrentDb.QueryDefs("qtmpCreateIndex")
'This line dies because .Fields dissapears before fl is assigned
For Each fl In CurrentDb.TableDefs(tableName).fields
If fl.Name <> "ID" Then
qd.sql = "CREATE INDEX " & fl.Name & "IX " & _
"ON " & tableName & " (" & fl.Name & ")"
qd.Execute
End If
Next
End Sub
BUT -- if I get CurrentDB into a variable, it DOES work:
Public Sub CreateIndexes(tableName As String)
' Creates an index on each column of the specified table
' except the surrogate key "ID" column
Dim fl As DAO.Field
Dim qd As DAO.QueryDef
Dim db As DAO.Database
Set db = CurrentDb 'cache CurrentDB in a variable
Set qd = db.QueryDefs("qtmpCreateIndex")
'only difference here is "db" instead of "CurrentDB"
'otherwise it's all the same, and this works
For Each fl In db.TableDefs(tableName).fields
If fl.Name <> "ID" Then
qd.sql = "CREATE INDEX " & fl.Name & "IX " & _
"ON " & tableName & " (" & fl.Name & ")"
qd.Execute
End If
Next
End Sub
issue just wasted many hours of my time. All I needed to do was spin on the
stupid Fields collection of a TableDef. Newbie code, right? So what's
wrong with the CurrentDB method?!?
This code doesn't work:
Public Sub CreateIndexes(tableName As String)
' Creates an index on each column of the specified table
' except the surrogate key "ID" column
Dim fl As DAO.Field
Dim qd As DAO.QueryDef
Set qd = CurrentDb.QueryDefs("qtmpCreateIndex")
'This line dies because .Fields dissapears before fl is assigned
For Each fl In CurrentDb.TableDefs(tableName).fields
If fl.Name <> "ID" Then
qd.sql = "CREATE INDEX " & fl.Name & "IX " & _
"ON " & tableName & " (" & fl.Name & ")"
qd.Execute
End If
Next
End Sub
BUT -- if I get CurrentDB into a variable, it DOES work:
Public Sub CreateIndexes(tableName As String)
' Creates an index on each column of the specified table
' except the surrogate key "ID" column
Dim fl As DAO.Field
Dim qd As DAO.QueryDef
Dim db As DAO.Database
Set db = CurrentDb 'cache CurrentDB in a variable
Set qd = db.QueryDefs("qtmpCreateIndex")
'only difference here is "db" instead of "CurrentDB"
'otherwise it's all the same, and this works
For Each fl In db.TableDefs(tableName).fields
If fl.Name <> "ID" Then
qd.sql = "CREATE INDEX " & fl.Name & "IX " & _
"ON " & tableName & " (" & fl.Name & ")"
qd.Execute
End If
Next
End Sub