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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

delete field in a table 1

Status
Not open for further replies.

samotek

Technical User
May 9, 2005
197
BG

I want to delete all the fields in a table except the fields F1,F2 and F3.
How can i do that?
I can delete for example the field F7 like that:
Set tdf = db.TableDefs("MyTable")
Set fld = tdf.Fields("F7")
tdf.Fields.Delete fld.Name

However,is it possible to declare also the fields that should not be deleted?

 
Hi
I don't think so, but you can delete by index number. This means that you could loop through a For ... Next [ponder]
 
... Then there is:
ALTER TABLE Employees DROP COLUMN Salary;
 
I have tried a suggestion to delete all fields except f1,f,2 and f3.
I wrote it in the OnClick event of a button,but on clicking
it nothing happens.is my code all right?





Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim i As Integer

Set db = CurrentDb

Set tdf = db.TableDefs("MyTable")

For i = tdf.Fields.Count - 1 To 0
Select Case tdf.Fields(i).Name
Case "F1", "F2", "F3"
' do nothing
Case Else
tdf.Fields.Delete tdf.Fields(i).Name
End Select
Next i

Set tdf = Nothing
' If you opened an external database
db.Close
Set db = Nothing
 
Hi
I do not think you can run a For ... Next in reverse, so you will need to use 0 to tdf.Fields.Count - 1
 
Going reverse, you need to specify step:

[tt] For i = tdf.Fields.Count - 1 To 0 step - 1[/tt]

BUT - all this changing of field names, types, deletions etc... sorry to be so frank/rude/direct... to me indicates flaws in design. In my view, it really shouldn't be any need to alter fields in a runtime environment (except possibly for temporary tables, but I do prefer even those to be pre created) - is this really needed?

Roy-Vidar
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top