1

After enlarging a database table's VarChar field from size 20 to size 50, I have to update the Size property of hundreds of design-time created TFields and I would like to do that once for all (I don't want to do the same work again, as soon someone will decide to change the field's size again).

The smartest way that comes to my mind consists in setting the TField's Size property at runtime:

const
  C_MY_FIELD_SIZE = 50;

...

MyField.Size := C_MY_FIELD_SIZE;

The only "problem" is that other programmers will see a misleading value displayed in the Size property of the Object Inspector at design time. I'm wondering if there is an equivalent design-time solution

6
  • 2
    Isn't this what a data module is for? Commented Dec 28, 2020 at 18:00
  • @DavidHeffernan: I don't understand what you mean, I have hundreds of different datasets with this field, used by as many forms/frames. Each dataset has its own different SQL text with different extractions, how data modules can help me? Commented Dec 29, 2020 at 6:35
  • I never use design time TField. I use fields from the dataset after having opened them, either by index or by field name. Doing that now in your software is probably a large work. Commented Dec 29, 2020 at 7:39
  • @fpiette: Yes and I also would loose a lot of design-time features. How do you manage DisplayLabel and properties/events of data-aware components (like grid columns hints, captions, editability, ecc...)? Commented Dec 29, 2020 at 11:35
  • Simple: I don't use data aware components! I found them easy for simple quick test program but it becomes very complex for real programs. I always disconnect user interface from database stuff. Database stuff remains simple and clean. I use standard components, including StringGrid to present the data. Presentation code is independent of persistence layer (database stuff). Of course this has to be designed like that from the beginning. Now that you have your application, it would be a lot of work to redesign it. Commented Dec 29, 2020 at 12:03

1 Answer 1

1

yes, I use SQL everywhere, not TTables and one of the problems is the max size of Edit boxes. In your case, that size is used for TField size. Well, what I do is at application start, do a query to the database that gets the size of all fields of the tables. In MsSqlServer is:

select table_name, column_name, character_maximum_length 
 from information_schema.columns where data_type='varchar' or data_type='nvarchar' 
 order by TABLE_NAME

that query returns: tableName, field, size in characters for all the fields. With that, build for example an array, or a List to get that info cached in your application.

For example, an array with:

 aLongs[i].sTable:=uppercase(qAux.fields[0].asstring);
 aLongs[i].sField:=uppercase(qAux.fields[1].asstring);
 aLongs[i].iLong:=qAux.fields[2].asinteger;

and then: Function getSize(sMiFieldName:string):integer; that simply search in that array the length for the field. It's necesary to get the info cached because normally that querys retrieving columns size are slows.

Sorry, I can't publish my code completely, thats a resume, I hope you get the idea: Get the sizes in run-time and cache the info in an array, list, collection or whatever.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.