Reputation: 2128
I've build a dataset in Delphi in which I have a ADOQuery that selectes 2 colums from a table... I drag and dropped a column from that ADOQuery into a form, how can I Acces my values from that column. I want to put that values into the combobox.
unit SCArabica;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DB, Mask, DBCtrls, Grids, DBGrids;
type
TForm1 = class(TForm)
ComboBox1: TComboBox;
DBGrid1: TDBGrid;
Label9: TLabel;
DBEdit1: TDBEdit;
DataSource1: TDataSource;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
published
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
end;
end.
DATASET Code
unit SCArabicaDataSet;
interface
uses
SysUtils, Classes, FMTBcd, DB, SqlExpr, ADODB;
type
TDataModule1 = class(TDataModule)
ad: TADOConnection;
ADOQuery1: TADOQuery;
ADOQuery1CategoriePermisID: TIntegerField;
ADOQuery1Categorie: TWideStringField;
private
{ Private declarations }
public
{ Public declarations }
end;
var
DataModule1: TDataModule1;
implementation
{$R *.dfm}
end.
I'm new with Delphi can you guide me to some tutorials on how to build a program in Delphi that acces databases?
Upvotes: 3
Views: 4342
Reputation: 2128
while not dm1.ADOQuery2.Eof do
begin
DBComboBox1.Items.Add(dm1.ADOQuery2.Recordset.Fields['IDMasina'].Value) ;
dm1.ADOQuery2.Next;
end;
This is my solution.
dm1 is a DataSource
ADOQuery2 is a DataSet
Upvotes: 2
Reputation: 12292
In my opinion, a good design would be to add a method in TDataModule1 to fetch records from the dataset and fill a TStrings. Then from the main code, you simply pass combobox.items.
procedure TDataModule1.FetchRecords(OutStrings : TStrings);
begin
ADOQuery1.SQL.Text := 'SELECT COMPANY FROm COMPANIES WHERE COUNTRY="BELGIUM"';
ADOQuery1.Open;
while not ADOQuery1.Eof do begin
OutStrings.Add(ADOQuery1.Fields[0].AsString;
ADOQuery1.Next;
end;
end;
Then you can write code in the form:
ComboBox1.Items.Clear;
DataModule1.FetchRecords(ComboBox1.Items);
I just typed this out of my head, so maybe some minor error, but you get the idea...
Upvotes: 7