• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 525
  • Last Modified:

How to dynamically assign a TDBLookupComboBox.ListSource from a query result(string)

Hello,
    I am in the process of creating a user defined form that allows the user to pick and choose fields that they want to see on the form.  My problem is when I add TDBLookupComboBox's.  I am trying to populate the listsource value from the results of a query.  The query returns the needed values to populate the listsource, listfield and keyfield.  The error I get is Incompatible type Tdatasource and String.  I have tried .listsource := TDataSource(DMF4.IntakeTemplateQListSource.value) but it returns nil.  If anyone has a suggestion, THANKS in advance.
0
bcornett-dfi
Asked:
bcornett-dfi
  • 4
  • 3
  • 2
  • +1
2 Solutions
 
kretzschmarCommented:

listsource := Datasource1; //a TDatasource-Objec should be asdsigned here

in runtime
var
  ds : TDatasource;
  q  : TQuery;

begin
  q := TQuery.Create(self); //or TAdoQuery
  q.sql.text := 'YourSQLHere';
  q.Open;
  ds := TDatasource.Create(self);
  ds.Dataset := q;
  dbLookUpComboBox1.ListSource := ds;
  ....
end;

meikl ;-)
0
 
bcornett-dfiAuthor Commented:
Thanks for the response!. Here is a sample portion of my code.

DMF4.IntakeItemsQ.Close;
DMF4.IntakeItemsQ.Open;

DMF4.IntakeItemsQ.First;

if DMF4.IntakeItemsQFieldType.Value = 'TDBLookupComboBox' then
    begin

    CreateControl(TDBLookupComboBox, DMF4.IntakeItemsQFieldName.AsString, DMF4.IntakeItemsQX.AsInteger, DMF4.IntakeItemsQY.AsInteger, 200, 20);
     CreateControl(TLabel,DMF4.IntakeItemsQLabelNAme.AsString, DMF4.IntakeItemsQX.Value - 110, DMF4.IntakeItemsQY.AsInteger, 200, 20);

     ListSource := TDataSource(DMF4.IntakeItemsQListSOurce.Value);

     TEdit(FindComponent(DMF4.IntakeItemsQFieldName.AsString)).Text := '';
     TDBLookupComboBox(FindComponentDMF4.IntakeItemsQFieldName.AsString)).ListSource := TDataSource(ListSource);
     TDBLookupComboBox(FindComponent(DMF4.IntakeItemsQFieldName.AsString)).ListField := DMF4.IntakeItemsQListField.Value;
     TDBLookupComboBox(FindComponent(DMF4.IntakeItemsQFieldName.AsString)).KeyField := DMF4.IntakeItemsQKeyField.Value;

     NamePrefix := DMF4.IntakeItemsQLabelNAme.AsString;
     LabelCaption := DMF4.IntakeItemsQLabelCAption.AsString;
     TabOrder := DMF4.IntakeItemsQTabOrder.Value;

     TLabel(FindComponent(NamePrefix)).Caption:= LabelCaption;
     TEdit(FindComponent(NamePrefix)).TabOrder := TabOrder;

    end
DMF4.IntakeItemsQ.NExt

I hope this added info will help.   The query return the table contents that has the property values that I use.  I just need to know how to convert the string value of DMF4.IntakeItemsQListSOurce.Value to TDataSOurce.    


Thanks Again for a quick reponse!!!!
0
 
sgc_romaniaCommented:
DataSource1:TDataSource;
-------
DataSource1.DataSet:=DMF4.IntakeItemsQListSOurce;
TDBLookupComboBox(FindComponentDMF4.IntakeItemsQFieldName.AsString)).ListSource := DataSource1;
-----
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
bcornett-dfiAuthor Commented:
Sorry sgc_romania , but I get a similar error with this change.  Incompatible types Tdataset and String.  Any other ideas...I have researched alot with no avail.

THANKS!!!!!!
0
 
Peter_Commented:
As kretzschmar's example shows above, you can not convert a string to a DataSource since TDataSource serves as a "connector" it does not hold any values.

The closest you could get in theory would be to "convert" the string to any decendent of TDataSet, then create a TDatasource. Connect this TDatasource to your decendent of TDataset and then connect the TDatasource to your datasetproperty.

I don't think this is a very good idea in practise though? TDataset is designed to get its data from a database, trying to add data from a string is unlikely to work. Though it would be the proper way in an environment such as .NET where dataset is meant to work in a disconnected state, this is not the case with Delphi.
0
 
bcornett-dfiAuthor Commented:
I am trying to get data from the database.  The listsource that I am wanting is DFM4.EMplyment_StatusS and this is the value that is stored in the database.  The value that I am pulling out of the query is considered a string 'DMF4.Employment_StatusS'.   I just want to know if there is a way to pass that as a listsource.

THANKS!!!!
0
 
kretzschmarCommented:
instead of

ListSource := TDataSource(DMF4.IntakeItemsQListSOurce.Value);

use

ListSource := TDataSource.Create(self);

then instead of

     TDBLookupComboBox(FindComponentDMF4.IntakeItemsQFieldName.AsString)).ListSource := TDataSource(ListSource);
     
use

     TDBLookupComboBox(FindComponentDMF4.IntakeItemsQFieldName.AsString)).ListSource := ListSource;
     
don't forget to free the ListSource
after you don't need it,
before you create a new one

meikl ;-)
0
 
kretzschmarCommented:
oops, didn't see your last comment before my post

in this case

instead of

ListSource := TDataSource(DMF4.IntakeItemsQListSOurce.Value);

use

ListSource := TDataSource(FindComponent(DMF4.IntakeItemsQListSOurce.AsString));

the second change is quite valid, but doesn't matter then

meikl ;-)


0
 
Peter_Commented:
Maybe something like this could work though? It will create a resultset exactly as stated in the query which is much the same as converting the string.

You will have to have the TQuery connected properly to a database.


listsource := Datasource1; //a TDatasource-Objec should be asdsigned here

in runtime
var
  ds : TDatasource;
  q  : TQuery;

begin
  q := TQuery.Create(self); //or TAdoQuery
  q.sql.text := 'SELECT ' + DMF4.IntakeItemsQListSOurce.Value + '  AS LookUpValue';
  q.Open;
  ds := TDatasource.Create(self);
  ds.Dataset := q;
  dbLookUpComboBox1.ListSource := ds;
  ....
end;

'SELECT ' + DMF4.IntakeItemsQListSOurce.Value + '  AS WhatEverYouLike ToCallThisField' would work, but it would leave the combobox with just one item to choose from? So I wonder if this is really the correct interpretation?
0
 
bcornett-dfiAuthor Commented:
Thanks to all who helped.  I finally found a work around...the sample code as follows:

     begin
      DMF2.MyQuery.Close;
      DMF2.MyQuery.SQL.CLear;

      DMF2.MyQuery.SQL.Add('SELECT * from');
      DMF2.MyQuery.SQL.Add(DMF4.IntakeItemsQListSOurce.Value);

      if not DMF2.MyQuery.Prepared then
        DMF2.MyQuery.Prepare;
      DMF2.MyQuery.Open;

      TempDS := TDatasource.Create(self);
      TempDS.Dataset := DMF2.MyQuery;
     end;


     TDBLookupComboBox(PatIntakeFrm.PageControl1.ActivePage.FindComponent(DMF4.IntakeItemsQFieldName.AsString)).ListSource := TempDS;
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now