We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

DBComboBox help

QurbanDurrani
on
Medium Priority
1,075 Views
Last Modified: 2012-05-06
Hi Experts,
I have a form which has a DBGrid and a whole bunch of other components like DBEdit, DBRadioGroup, DBLookUpComboBox and DBComboBox. All these components point to the same table in the Database. As you select different records in the Grid, all the components get the corresponding values.
The DBComboBox1 points to the field 'Report' in the table. The field 'Report' is an integer field and currently only have the values 0,1 and 4. 0 stands for Reportdisabled, 1 stands for ReportEnabled and 4 stands for ReportInsiteonly.
I can get the DBComboBox1 to display 0,1 and 4. I can also click on the dropdown, select a different value and it is saved to that particular field.
1) What I want to do is for the DBComboBox1  to display Reportdisabled, ReportEnabled and ReportInsiteonly instead of 0,1 and 4?
2) Also when I select lets say 'ReportEnabled', It should save 1 in the field for that particular record?
I might have put in a little too much explaination, but I hope it helps understanding the question.
Comment
Watch Question

Yeah well all comboboed have two properties

1. Display member
2. value member
You will have to give the meaninful names in the display member and the value member contains the corresponding code ie 1 0 or 4
Geert GOracle dba
CERTIFIED EXPERT
Top Expert 2009

Commented:
this is what's called a lookupcombo
you want to display a text and this is translated to a value in code

this lookupcombobox has a listsource, listindex and listfield property

you need to set up a datasource and query/table
this new query/table needs to read from a table containing the values ReportDisabled, ReportEnabled and ReportSiteOnly and id 0, 1, 4

so your table for reportstates would be
CREATE TABLE REPORTSTATES (
  ID NUMBER(10) NOT NULL,
  DESCR VARCHAR2(100) NOT NULL);

ALTER TABLE REPORTSTATES ADD (CONSTRAINT PK_REPORTSTATES PRIMARY KEY (ID));

INSERT INTO REPORTSTATES (ID, DESCR) VALUES (0, 'Report Disabled');
INSERT INTO REPORTSTATES (ID, DESCR) VALUES (1, 'Report Enabled');
INSERT INTO REPORTSTATES (ID, DESCR) VALUES (2, 'Report Disabled');

dsReportStates: TDataSource;
qryReportStates: TQuery; // select * FROM reportstates;

lookupcombobox
Datasource -> as your combobox
Datafield -> as your combobox
ListSource -> dsReportStates;
ListField -> Descr
ListIndex -> Id
dsReportStates.Dataset -> qryReportStates

you need to open the qry first

Author

Commented:
Thanks to both for your responses thus far.
Well I wish Issackhazi was right. I was looking for a simple solution like that one.
Geert Gruwez, I know I could use a DBlookupcombobox, but I did not want to create another table.
Is there a way to avoid creating another table?
Top Expert 2004
Commented:
you could use the onSetText/onGetText-events of this TField to map your values as needed

sample follows, if needed

meikl ;-)

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Geert GOracle dba
CERTIFIED EXPERT
Top Expert 2009

Commented:
you could use the VirtualTable from www.devart.com
it's an in memory table
and behaves like a normal table, excellent for small lookup tables.
Geert GOracle dba
CERTIFIED EXPERT
Top Expert 2009

Commented:
a TClientDataset would do as well i guess
Top Expert 2004

Commented:
yep, Geert, alike memTable would be the most elegant method for my eyes

meikl ;-)
Oracle dba
CERTIFIED EXPERT
Top Expert 2009
Commented:
I created a wrapper for parameters and values like this:
it's basically contains a datasource and virtualtable

you use it like this:

type
  TForm1 = class(TForm)
  private
    fValueStore: TValueStore;
  public
    constructor Create(AOwner: TComponent); override;
  end;

constructor TForm1.Create(AOwner: TComponent);
var List: TStrings;
begin
  inherited Create(AOwner);
  fValueStore := TValueStore.Create(Self, TValueItem);
  List := TStringList.Create;
  try
    List.Add('Varchar=V');
    List.Add('Float=F');
    List.Add('Integer=I');
    List.Add('Date=D');
    fValueStore.Add('PARAMTYPE', List.Text);
  finally
    FreeAndNil(List);
  end;
  LookupParams.ListSource := fValueStore.Items['PARAMTYPE'];
  LookupParams.Listfield := 'Value';
  LookupParams.ListIndex := 0;
end;
type
  TValueItem = class(TCollectionItem)
  private
    fParamName: string;
    fData: TVirtualTable;
    fDS: TDatasource;
  public
    constructor Create(Collection: TCollection); override;
    destructor Destroy; override;
    procedure SetValues(aParamName, aValues: string);
    procedure AddValue(aParamItem, aParamValue: string);
    property DataSource: TDataSource read fDs;
  end;
 
  TValueStore = class(TOwnedCollection)
  private
    function GetItems(aParamName: string): TDataSource;
  public
    function Add(aParamName, aValues: string): TValueItem;
    function AddValue(aParamName, aParamItem, aParamValue: string): TValueItem;
    property Items[aParamName: string]: TDataSource read GetItems;
  end;
 
{ TValueItem }
 
constructor TValueItem.Create(Collection: TCollection);
begin
  inherited Create(Collection);
  fData := TVirtualTable.Create(TComponent(TOwnedCollection(Collection).Owner));
  fDS := TDataSource.Create(fData.Owner);
  fDS.DataSet := fData;
  fParamName := '';
end;
 
procedure TValueItem.SetValues(aParamName: string; aValues: string);
var
  I: Integer;
  List: TStrings;
begin
  fParamName := aParamName;
  fData.Active := False;
  fData.DeleteFields;
  List := TStringList.Create;
  try
    List.CommaText := aValues;
    fData.AddField('Param', ftString, 100);
    fData.AddField('Value', ftString, 100);
    fData.Active := True;
    for I := 0 to List.Count-1 do
    begin
      fData.Append;
      if Pos('=', aValues) > 0 then
      begin
        fData.FieldByName('Param').AsString := List.Names[I];
        fData.FieldByName('Value').AsString := List.ValueFromIndex[I];
      end
        else
      begin
        fData.FieldByName('Param').AsString := List[I];
        fData.FieldByName('Value').AsString := List[I];
      end;
      fData.Post;
    end;
  finally
    FreeAndNil(List);
  end;
end;
 
destructor TValueItem.Destroy;
begin
  fData.Active := False;
  FreeAndNil(fDs);
  FreeAndNil(fData);
  inherited Destroy;
end;
 
procedure TValueItem.AddValue(aParamItem, aParamValue: string);
begin
  if not fData.Active then
  begin
    fData.DeleteFields;
    fData.AddField('Param', ftString, 100);
    fData.AddField('Value', ftString, 100);
    fData.Active := True;
  end;
  fData.Append;
  fData.FieldByName('Param').AsString := aParamItem;
  fData.FieldByName('Value').AsString := aParamValue;
  fData.Post;
end;
 
{ TValueStore }
 
function TValueStore.Add(aParamName, aValues: string): TValueItem;
var I: Integer;
begin
  Result := nil;
  for I := 0 to Count-1 do
    if SameText(TValueItem(inherited Items[I]).fParamName, aParamName) then
    begin
      Result := TValueItem(inherited Items[I]);
      Break;
    end;
  if Result = nil then
  begin
    Result := TValueItem(inherited Add);
    Result.SetValues(aParamName, aValues);
  end;
end;
 
function TValueStore.AddValue(aParamName, aParamItem, aParamValue: string): TValueItem;
var I: Integer;
begin
  Result := nil;
  for I := 0 to Count-1 do
    if SameText(TValueItem(inherited Items[I]).fParamName, aParamName) then
    begin
      Result := TValueItem(inherited Items[I]);
      Break;
    end;
  if Result = nil then
  begin
    Result := TValueItem(inherited Add);
    Result.fParamName := aParamName;
  end;
  Result.AddValue(aParamItem, aParamValue);
end;
 
function TValueStore.GetItems(aParamName: string): TDataSource;
var I: Integer;
begin
  Result := nil;
  for I := 0 to Count-1 do
    if SameText(TValueItem(inherited Items[I]).fParamName, aParamName) then
    begin
      Result := TValueItem(inherited Items[I]).DataSource;
      Break;
    end;
end;

Open in new window

Author

Commented:
Greet and Kretzschmar,
Thanks a bunch to both of you for excellent answers. Right before you guys answered, I was able to use a JvDBComboBox in the desired manner without any extra coding. However, as soon as I get some time I would like to revisit this and try both of yours solutions. By the way JvDBComboBox  is a pain because there are no help documents available.
Thanks again to both of you.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.