Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Iteration through field collection not identifiying all table fields

Status
Not open for further replies.

bdbBear

Programmer
Apr 29, 2005
54
US
I have the following code wherein I am removing all (but one) fields from a table. I am identifying the field names by iterating through the field collection of that table.

Dim dbs As DAO.Database
Set dbs = CurrentDb

Dim t As DAO.TableDef
Set t = dbs.TableDefs("tblCorrelations")

Dim fld As Field

For Each fld In t.Fields
Debug.Print fld.Name
If fld.Name <> "Company Name" Then
t.Fields.Delete fld.Name
End If
Next fld

Set fld = Nothing


For some reason this doesn't identify all of the fields (they are all number/double).

After I run the code and open up the table it shows that some of the fields were deleted but some are still there.

If I run the code again, then some more of the fields are removed and some still remain.

It seems as if the iteration through the field collection is random.

Anyone have any ideas on how to solve this problem?

Thanks!

- Bruce
 
Dim i As Integer
For i = t.Fields.Count-1 To 0 Step -1
Set fld = t.Fields(i)
Debug.Print fld.Name
...
Next i

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top