Search code examples
delphims-access-2010delphi-7

How to display a record field from dbgrid to an editbox


I have built an access database and connected it with an ADOquery and Datasource.I have built a table named BagCost which has field names bag size and cost. I have attached a DBgrid into a form and linked it with the "Bagcost" table. The Dbgrid has the following fields:

enter image description here

How can I display the four costs from the dbgrid column to different editboxes at runtime?


Solution

  • Explanations:

    I hope I understand you correctly. What you need to do is to read data from each of your four records. Use First and Next methods to change the active record, Eof method to test if the active record is the last record in a dataset. Optionally (for large number of records), use DisableControls and EnableControls methods to prevent data-aware controls from updating every time the active record changes.

    Example:

    Next is a basic example, that uses AfterOpen event to read field values from each record of your query.

    procedure TfrmMain.qryAfterOpen(DataSet: TDataSet);
    begin
       DataSet.DisableControls;
       try
          DataSet.First;
          if not DataSet.Eof then Edit1.Text := DataSet.FieldByName('cost').AsString else Edit1.Text := 'Record not found';
          DataSet.Next;
          if not DataSet.Eof then Edit2.Text := DataSet.FieldByName('cost').AsString else Edit2.Text := 'Record not found';
          DataSet.Next;
          if not DataSet.Eof then Edit3.Text := DataSet.FieldByName('cost').AsString else Edit3.Text := 'Record not found';
          DataSet.Next;
          if not DataSet.Eof then Edit4.Text := DataSet.FieldByName('cost').AsString else Edit4.Text := 'Record not found';
          DataSet.First;
       finally
          DataSet.EnableControls;
       end{try};
    end;