[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2003-11-18
10
Medium Priority
?
515 Views
Last Modified: 2010-04-05
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
Comment
Question by:bcornett-dfi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 27

Accepted Solution

by:
kretzschmar earned 1000 total points
ID: 9771314

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
 

Author Comment

by:bcornett-dfi
ID: 9771849
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
 
LVL 2

Expert Comment

by:sgc_romania
ID: 9772415
DataSource1:TDataSource;
-------
DataSource1.DataSet:=DMF4.IntakeItemsQListSOurce;
TDBLookupComboBox(FindComponentDMF4.IntakeItemsQFieldName.AsString)).ListSource := DataSource1;
-----
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:bcornett-dfi
ID: 9772489
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
 
LVL 3

Expert Comment

by:Peter_
ID: 9772527
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
 

Author Comment

by:bcornett-dfi
ID: 9772649
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 9772670
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 9772701
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
 
LVL 3

Assisted Solution

by:Peter_
Peter_ earned 1000 total points
ID: 9772759
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
 

Author Comment

by:bcornett-dfi
ID: 9773021
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

649 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