Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 12-03-2006, 04:22 PM
struct struct is offline
Senior Member
 
Join Date: Nov 2003
Posts: 667
Default Getting Distict from ADOTable

I'm trying to populate a combobox with all the "languages" in my database. the problem is, this field has the same value for multiple rows. in sql you can use select distict to only return one of each value. how do i do this with an adotable?

[DELPHI]procedure TForm1.LoadLanguageBox();
begin
ADOTable1.Connection := ADOConnection1;
ADOTable1.TableName := 'Snippits';
ADOTable1.Open;
with ADOTable1 do
begin
First;
ListBox1.Items.Clear;
while not eof do
begin
ListBox1.Items.Add(FieldByName('Language').AsStrin g);
Next;
end;
end;
ADOTable1.close;
end;

// I tried this, but I don't think it's the write
// approch... it's not working and there has
// to be an easier way.
procedure TForm1.LoadLanguageBox();
var I : Integer;
begin
ADOTable1.Connection := ADOConnection1;
ADOTable1.TableName := 'Snippits';
ADOTable1.Open;
with ADOTable1 do
begin
First;
ListBox1.Items.Clear;
while not eof do
begin
For I := 0 to Listbox1.Count - 1 do
begin
If FieldByName('Language').AsString <>
Listbox1.Items.ValueFromIndex[I] then
ListBox1.Items.Add(FieldByName('Language').AsStrin g);
end;
Next;
end;
end;
ADOTable1.close;
end;[/DELPHI]

Code:
I love this forum!
function LearnSomethingNew(User:String):Boolean;
Reply With Quote
  #2  
Old 12-03-2006, 08:38 PM
davidj davidj is offline
Senior Member
 
Join Date: Jan 2001
Posts: 2,900
Default RE: Getting Distict from ADOTable

Why not use a ADOQuery and the distinct clause?

You can do exactly that you have just done.
Reply With Quote
  #3  
Old 12-04-2006, 03:47 AM
digitiger digitiger is offline
Senior Member
 
Join Date: Jan 2001
Posts: 716
Default RE: Getting Distict from ADOTable

one simple and general way of adding distinct values to any list is to check if that value already exists in the list or not and if it does not exists then add the same. In your case it can be done as below:

while not eof do
begin
if ListBox1.Items.Indexof(FieldByName
('Language').AsString) < 0 then
ListBox1.Items.Add(FieldByName('Language').AsStrin g);
Next;
end;


though if possible then use ADOQuery as suggested in earlier answere because that will be more optimized approach.

Hope it helps and if it does then do accept the answere
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 02:54 AM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2019, vBulletin Solutions, Inc.