Solved

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

Posted on 2003-11-18
10
479 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
  • 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

919 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now