I have a Delphi 2007 app that interacts with an Access 2000 database. I have made changes to the database structure (addition of fields) and need to make them visible in the Delphi application but that is proving difficult. I made a minimal Delphi application and Access database and the same issue is seen there.
My steps were:
- create an Access database with Field 1 and Field2
- use Settings/Control Panel/Administrative Tools/Data Sources (ODBC 32-bit) to create an alias to the MS database.
- create a Delphi application.
- add a
TDBGrid
to the form - add a
TDataSource
onto the form - set the
Datasource
property of the grid toDataSource1
- add a
TTable
to the form - set the
Dataset
property of theTDataSource
toTable1
- set the
DataBaseName
property ofTable1
to the alias name created for the database. - set the
TableName
property to the name of the table in your original Access database. - set the
Active
property of the Table to TRUE. All the fields originally defined in the database appear in the grid. The problem is when I want to add fields later to this. I can make them appear as columns in the grid by rebuilding the whole thing from scratch, but there must be an easier way!
What is accepted best practice for forcing database structure changes through to the Delphi IDE and resulting application?
** Answer to Ken White's comment **
Thanks Ken. I appreciate the reasons for the Columns
property of the DBGrid
when you may not want all the fields in your grid - my issue is that when I use MS Access to add some fields to the table and then re-open my Delphi project:
- if I disconnect the
TTable
and reconnect it, theFieldDefs
property shows the added fields. All good. - I then move to the TDBGrid. The displayed columns don't show the additional fields, only the original ones. The
Columns
collection is empty. - when I inspect the
Columns
property and try to add all fields I just get the original fields. If I try to add one field` column, the picklist only gives me the original fields to choose from.
I don't understand why the TTable
can see the new fields but the TDatasource
(which has the TTable specified as it's Dataset
property value) cannot.