Solved

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

Posted on 2003-11-18
10
492 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 250 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
Independent Software Vendors: 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 250 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

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

729 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