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

descriptive dbcombobox for enumerated integer fields?

Status
Not open for further replies.

colttaylor

Programmer
Aug 20, 2002
117
US
I have an application which tracks customer support events such as phone-calls, bug-reports, faxes, etc. and stores them in a database table called Events.

I have an integer field in my events table called eventtype. It has eight distinct values, each of which represent a different type of event such that...

a value of 0 means phone call
a value of 1 means fax
a value of 2 means email

...and so on.


I want to put a dbcombo on the event entry screen which will allow the user to select what type of event they are entering, but although the stored values are integers, I would like the items available in the combobox's pulldown to be human-readable text.

One last twist...
The event entry screen is actually a DBCtrlGrid, so dblookupcombo won't work.

Any Ideas?

Thanks for the help!
Peace,
Colt

If it's stupid but it works, it isn't stupid
 
Drop a DBComboBox component on your DBCtrlGrid and set the DataField and DataSource properties to the appropriate persistent field in your TDataset (e.g. TTable) component.

In the Items property of your DBComboBox component add the various descriptions for your event codes 0 to 7. This could be done at run time if the event codes are stored in a separate table. For example
Code:
phone call
fax
email
snail mail
telegraph
carrier pigeon
cell phone
smoke signals

Write OnGetText and OnSetText event handlers as follows (assumes your field is called EventCode):
Code:
procedure TForm1.Table1EventCodeSetText(Sender: TField; const Text: String);
begin
  (Sender as TIntegerField).AsInteger  := DBComboBox1.Items.IndexOf ( text );
end;
Code:
procedure TForm1.Table1EventCodeGetText(Sender: TField; var Text: String;
  DisplayText: Boolean);
var
  n: integer;
begin
  n := (Sender as TIntegerField).AsInteger;
  if n < 8 then
    text := DBComboBox1.Items[n]
  else
    text := IntToStr(n);   // Unexpected Value
end;


Andrew
Hampshire, UK
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top