[SOLVED] Selecting/give focus of the current record in a TDBGrid lazarus / 16-Databases ---------------------------------------------------------------------- madref · 2015-12-14 14:21 I am using the following code to determine the ID of a game closest to the current date. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i    cSQL := 'SELECT Wed_ID, Wed_Datum, Wed_Tijd ' +          'FROM tbl_Wedstrijden ' +          'WHERE Date(Wed_Datum) > Date() ' +          'ORDER BY Wed_Datum, Wed_Tijd'; I am also using a DBGrid_Wedstrijden: TDBGrid; which contains the actual games and so on How can i select by code the correct game? ---------------------------------------------------------------------- mangakissa · 2015-12-14 17:34 I'll try to understand what you mean. DBGrid_Wedstrijden has the output of your query. Is it not the same data, you can use Tdataset.locate() to find your record. ---------------------------------------------------------------------- balazsszekely · 2015-12-14 17:49 I assume you want to locate a specific record in the grid. Since all grids has a datasource and all datasources has a dataset,  use the locate function of the dataset like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   SQLQuery.Locate('Wed_ID', IDToFind, []);For more complex search you can loop through the dataset(first disable the update of the visual controls): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function TForm1.LocateInGrid(const ID: Integer): Boolean;var  SQLQuery: TSQLQuery;  Orig_ID: Integer;begin  Result := False;  SQLQuery := (DBGrid_Wedstrijden.DataSource.DataSet as TSQLQuery);  Orig_ID := SQLQuery.FieldByName('Wed_ID').AsInteger;    SQLQuery.DisableControls;  try    SQLQuery.First;    while not SQLQuery.EOF do    begin      if SQLQuery.FieldByName('Wed_ID').AsInteger = ID then //if you need add more conditions here       begin        Result := True;        Break;      end;      SQLQuery.Next;    end;    if not Result then      SQLQuery.Locate('Wed_ID', Orig_ID, []);  finally    SQLQuery.EnableControls;  end;end; ---------------------------------------------------------------------- madref · 2015-12-14 20:22 I was on the right track but i was looking at the wrong query. i needed to look at TQ_Wedstrijden instead of TQ_Rapport This is my final version of what i need: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   TQ_Wedstrijden.Active := True;    TQ_Wedstrijden.Last;  TQ_Wedstrijden.First;    GroupBox_Wedstrijden.Caption :=    Format('Games (%d)', [TQ_Wedstrijden.RecordCount]);    // Bepaal de wedstrijd die het dichtstbij de huidige datum ligt.  cSQL := 'SELECT Wed_ID, Wed_Datum, Wed_Tijd ' +          'FROM tbl_Wedstrijden ' +        'WHERE Date(Wed_Datum) > Date() ' +          'ORDER BY Wed_Datum, Wed_Tijd';    with Form_Information do begin    TQ_Rapport.Active := False;    TQ_Rapport.DataBase := Connect_RefereeDB;    TQ_Rapport.SQL.Text := cSQL;    TQ_Rapport.Active := True;    TQ_Rapport.Last;    TQ_Rapport.First;    if TQ_Rapport.RecordCount > 0 then begin      wi := TQ_Rapport.FieldByName('Wed_ID').AsInteger;    showmessage ('Wed_ID = ' + IntToStr(wi));      TQ_Wedstrijden.Locate('Wed_ID', TQ_Rapport.FieldByName('Wed_ID').AsInteger, []);    end;  // if  end;  // with ---------------------------------------------------------------------- mangakissa · 2015-12-15 08:16 Why creating a local variabele if you don't need it? Code: [Select]showmessage ('Wed_ID = ' + TQ_Rapport.FieldByName('Wed_ID').AsString); ---------------------------------------------------------------------- madref · 2015-12-15 21:15 it was to check the value while in runtime. But yes it's obsolete