#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Location
    Virginia
    Posts
    4
    Rep Power
    0

    TADO components with MS-Access


    I have just gotten back into Delphi after 10 years away when I was using Delphi with dBase tables. I followed a tutorial on using TADOConnectioin, TADOQuery, TDataSource, DBGrid for usiing dbGo with MS-Access.

    I connect to the database OK.
    The query appears to be created OK.
    The data source links to the dbGrid.
    The table I am using contains only 1 record and the dbGrid has the column names, one row, but the values are empty. This makes the query suspect.

    Any help is needed. Code follows.
    Thank you in advance.
    bill

    Code:
    unit CouncilInfo;
    
    interface
    
    uses
      Winapi.Windows, Winapi.Messages,
      System.SysUtils, System.Variants, System.Classes, System.UITypes,
      Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Grids, Vcl.DBGrids,
      Data.DB, Data.Win.ADODB;
    
    type
      TfrmKCInfo = class(TForm)
        ADOConnKCInfo: TADOConnection;
        ADOQryKCInfo: TADOQuery;
        KCds: TDataSource;
        KCCouncilInfo: TDBGrid;
    
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
        tSQLstring: String;
      public
        { Public declarations }
      end;
    
    var
      frmKCInfo: TfrmKCInfo;
    
    implementation
    
    {$R *.dfm}
    
    
    procedure TfrmKCInfo.FormCreate(Sender: TObject);
    begin
      { Initiate the connection }
      try
        ADOConnKCInfo.Connected:=true;
        except
          on e: EADOError do
          begin
            MessageDlg('Error while connecting', mtError, [mbOK], 0);
            Exit;
          end;
      end;
    
      { Create query to retrieve data }
      ADOQryKCInfo := TADOQuery.Create(Self);
      ADOQryKCInfo.Connection := ADOConnKCInfo;
      tSQLstring := 'SELECT * FROM KCINFO';
      ADOQryKCInfo.SQL.Add(tSQLstring);
      ADOQryKCInfo.Prepared := true;
      try
        ADOQryKCInfo.Active := true;
        except
          on e: EADOError do
          begin
            MessageDlg('Error while doing query', mtError, [mbOK], 0);
            Exit;
          end;
      end;
    
      { Create the data source }
      KCds := TDataSource.Create(Self);
      KCds.DataSet := ADOQryKCInfo;
      KCds.Enabled := true;
    
      { Populate the data grid }
      KCCouncilInfo.DataSource := KCds;
    
    end;
    
    end.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Posts
    352
    Rep Power
    7
    The only thing I see is that you have an ADO Query component and the TDataSource defined in your form's type declaration..
    Code:
      ...
      TfrmKCInfo = class(TForm)
        ADOConnKCInfo: TADOConnection;
        ADOQryKCInfo: TADOQuery; <---ADOQuery Declared Here
        KCds: TDataSource;<--Datasource Declared Here
        KCCouncilInfo: TDBGrid;
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
        tSQLstring: String;
      public
        { Public declarations }
      end;
      ...
    Since it's declared where it is, it tells me that you have placed an ADO component and a Datasource component on your form, and renamed the components. But, you also dynamically create an ADO Query component and the Datasource and name them the same thing.
    Code:
       ...
          end;
      end;
      ADOQryKCInfo := TADOQuery.Create(Self); <--Creating New Component
      ADOQryKCInfo.Connection := ADOConnKCInfo;
      tSQLstring := 'SELECT * FROM KCINFO';
      ADOQryKCInfo.SQL.Add(tSQLstring);
       ...
          end;
      end;
      KCds := TDataSource.Create(Self);<--Create New Datasource
      KCds.DataSet := ADOQryKCInfo;
      ...
    You should either remove the components from the form and declare the ADOQuery and Datasource in the private section
    Code:
      ...
      TfrmKCInfo = class(TForm)
        ADOConnKCInfo: TADOConnection;
        KCCouncilInfo: TDBGrid;
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
        tSQLstring: String;
        ADOQryKCInfo: TADOQuery; <---Remove ADO Component then Manually Add to Private Section
        KCds: TDataSource;<--Remove DS Component then Manually Add to Private Section
      public
        { Public declarations }
      end;
      ...
    or remove the calls to dynamically create those two components, since they already "exist"

    I'm am not, however, sure this is the reason the query returns an empty set...
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Location
    Virginia
    Posts
    4
    Rep Power
    0
    majlumbo

    I commented out the "create" statements.
    That got me closer.
    I added a navigator to the form.

    I changed the select clause to a different table:
    SELECT * FROM ADDRESS ORDER BY AddrNum
    Got the entire data set!!

    I tried adding "order by" to the first query -- still no data set.
    SELECT * FROM KCINFO ORDER BY CouncilName

    Thanks for the help
    bill

IMN logo majestic logo threadwatch logo seochat tools logo