?
Solved

DBComboBox help

Posted on 2009-02-10
9
Medium Priority
?
993 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.
0
Comment
Question by:QurbanDurrani
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 8

Expert Comment

by:isaackhazi
ID: 23608748
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
0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 23608793
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

0
 

Author Comment

by:QurbanDurrani
ID: 23614542
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?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 27

Assisted Solution

by:kretzschmar
kretzschmar earned 600 total points
ID: 23619300
you could use the onSetText/onGetText-events of this TField to map your values as needed

sample follows, if needed

meikl ;-)
0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 23620047
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.
0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 23620051
a TClientDataset would do as well i guess
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 23620083
yep, Geert, alike memTable would be the most elegant method for my eyes

meikl ;-)
0
 
LVL 38

Accepted Solution

by:
Geert Gruwez earned 1400 total points
ID: 23620089
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

0
 

Author Comment

by:QurbanDurrani
ID: 23624293
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.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question