0

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 to DataSource1
  • add a TTable to the form
  • set the Dataset property of the TDataSource to Table1
  • set the DataBaseName property of Table1 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, the FieldDefs 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.

rossmcm
  • 5,493
  • 10
  • 55
  • 118
  • 2
    The way to minimize problems when you change the structure of the db is to avoid using persistent TFields iin the datasets in the Delphi app and, if the Delphi dataset type is one which supports persistent TFieldDefs, avoid using those too. There is nothing that these two types of pesistent collections can do that cannot be done in code instead, and you can make the code adaptable to changes in underlying dataset structure. But there is no way of forcing changes through as you seem to envisage. – MartynA Oct 12 '20 at 11:33
  • 1
    The reason this can't automatically be done is that the IDE has no way of knowing whether you want the new columns included or not. It's common, for instance, to create persistent fields to use in a TDBGrid to display to a user but not include every column in the table to that grid for display, or when you're using a related database from another application that includes columns you'll never use in your app. – Ken White Oct 12 '20 at 21:53
  • Hi Ken, comments in question. – rossmcm Oct 13 '20 at 01:27

0 Answers0