Recent

Author Topic: [SOLVED] Checking the state of a Query?  (Read 948 times)

What I can do

  • Full Member
  • ***
  • Posts: 167
[SOLVED] Checking the state of a Query?
« on: October 15, 2024, 03:21:27 pm »
OS:Windows 10 @64
Lazarus 3.4
Project: Checking to see if a SQLQuery is in the dsEdit or dsInserrt
tried

 SQLQUery.state in [dsEdit, dsInsert]

doesn't seam to work.
« Last Edit: October 15, 2024, 11:37:20 pm by What I can do »

rvk

  • Hero Member
  • *****
  • Posts: 6760
Re: Checking the state of a Query?
« Reply #1 on: October 15, 2024, 04:04:37 pm »
OS:Windows 10 @64
Lazarus 3.4
Project: Checking to see if a SQLQuery is in the dsEdit or dsInserrt
tried

 SQLQUery.state in [dsEdit, dsInsert]

doesn't seam to work.
Then you need to provide more context because the SQLQuery.State in [dsEdit, dsInsert] should work.

If you want help... :
Show us code.
Tell us about that code.

What I can do

  • Full Member
  • ***
  • Posts: 167
Re: Checking the state of a Query?
« Reply #2 on: October 15, 2024, 11:36:47 pm »
Thanks for the response rvk
That gives me the confidence that at least this small element of my code is OK,
Now all I have to do is trace down the receive data and to the send data and see where the error is.
---------------------------------------------------------
OK, Got it
It's the focus of a cell in a DBGrid when dsInsert is set with a DBNavigator
at this point all is normal, but...
I am using none DB combo boxes to process data before posting.
at the moment of leaving focus from the DBGrid to the TComboBox
then the dsinsert State returns back to dsbrowes

Work around is to intercept the process at SQLQuery.beforeinsert and set the focus to the combobox
then let the normal process of dsinsert mode continue.

rvk

  • Hero Member
  • *****
  • Posts: 6760
Re: Checking the state of a Query?
« Reply #3 on: October 15, 2024, 11:51:21 pm »
Work around is to intercept the process at SQLQuery.beforeinsert and set the focus to the combobox
then let the normal process of dsinsert mode continue.
Or find out why the dataset is put back into dsBrowse mode when leaving the TDBGrid.
It shouldn't. It should stay in dsEdit with an asterisk before the record in the TDBGrid.
Do you have a TDBGrid.OnExit where you Cancel the dsEdit?

BTW. If you use a TDBNavigator with TEdits for editing it might be better to set the TDBGrid on rowselect instead of cell select and disable TDBGrid editing.

What I can do

  • Full Member
  • ***
  • Posts: 167
Re: [SOLVED] Checking the state of a Query?
« Reply #4 on: October 16, 2024, 03:19:43 am »
Quote
Or find out why the dataset is put back into dsBrowse mode when leaving the TDBGrid.
It shouldn't. It should stay in dsEdit with an asterisk before the record in the TDBGrid.
Do you have a TDBGrid.OnExit where you Cancel the dsEdit?

OK, I built this Application
Code: Pascal  [Select][+][-]
  1. unit uDBGrid;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, DBGrids, StdCtrls,
  9.  
  10.   DBCtrls, odbcconn,odbcsqldyn, SQLDB, DB,ComCtrls;
  11.  
  12. type
  13.  
  14.   { TForm1 }
  15.  
  16.   TForm1 = class(TForm)
  17.     ODBCConnection1:TODBCConnection;
  18.     SQLTransaction1:TSQLTransaction;
  19.     SQLQuery1:TSQLQuery;
  20.     DataSource1:TDataSource;
  21.     ComboBox1: TComboBox;
  22.     DBGrid1: TDBGrid;
  23.     DBNavigator1: TDBNavigator;
  24.     procedure FormActivate(Sender: TObject);
  25.     procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
  26.     Function ShowMSG(const M:String;But:TMsgDlgButtons=[mbOK]):Boolean;
  27.   private
  28.  
  29.   public
  30.  
  31.   end;
  32.  
  33. var
  34.   Form1: TForm1;
  35.  
  36. implementation
  37. uses utoolkit;
  38.  
  39. {$R *.lfm}
  40.  
  41. { TForm1 }
  42.   Function TForm1.ShowMSG(const M:String;But:TMsgDlgButtons=[mbOK]):Boolean;
  43.   begin
  44.    Result:=False;
  45.    if MessageDlg(M, mtInformation , But,1) in [mrYes,mrOK] then
  46.     Result:=True;
  47.   end;
  48.  
  49. procedure TForm1.FormActivate(Sender: TObject);
  50. var
  51.   r,p:Integer;
  52.   e,f,ps:String;
  53. begin
  54.    ODBCConnection1:=TODBCConnection.Create(ODBCConnection1);
  55.    ODBCConnection1.DatabaseName:='Access64';
  56.    f:=ExtractShortPathName('G:\t\Dad\Program Files\Lazarus\projectsessions\test\MainHelp.mdb');
  57.   ODBCConnection1.Connected:=false;
  58.   ODBCConnection1.KeepConnection:=false;
  59.   e:=ExtractFileExt(F);
  60.   ODBCConnection1.DatabaseName:='Access64';//ODBC Manager
  61.   if ODBCConnection1.Params.Count>0 then
  62.    for r:=0 to ODBCConnection1.Params.Count-1 do
  63.      begin
  64.       P:=pos('DBQ=', ODBCConnection1.Params[r]);
  65.       if p>0 then
  66.        ODBCConnection1.Params[r]:='DBQ='+F
  67.      end
  68.   else
  69.    ODBCConnection1.Params.Add('DBQ='+F);
  70.   Try
  71.    ODBCConnection1.Connected:=TRUE;
  72.    ODBCConnection1.KeepConnection:=TRUE;
  73.   except
  74.    on E: Exception do
  75.     ShowMSG('An exception was raised: ' + E.Message);
  76.    end;
  77.   ODBCConnection1.LogEvents:=[];
  78.  
  79.    SQLTransaction1:=TSQLTransaction.Create(SQLTransaction1);
  80.    SQLTransaction1.DataBase:=ODBCConnection1;
  81.    SQLTransaction1.Action:=caCommit;
  82.    ODBCConnection1.Transaction:=SQLTransaction1;
  83.  
  84.    SQLQuery1:=TSQLQuery.Create(SQLQuery1);
  85.    SQLQuery1.DataBase:=ODBCConnection1;
  86.    SQLQuery1.Transaction:=SQLTransaction1;
  87.    SQLQuery1.AutoCalcFields:=TRUE;
  88.    SQLQuery1.Options:=[sqoAutoApplyUpdates,sqoAutoCommit,sqoKeepOpenOnCommit];
  89.    SQLQuery1.Upda]"]>Blockedde:=upWhereAll;
  90.    SQLQuery1.UsePrimaryKeyAsKey:=false;
  91.    SQLQuery1.DataBase:=ODBCConnection1;
  92.    SQLQuery1.Transaction:=ODBCConnection1.Transaction;
  93.  
  94.    DataSource1:=TDataSource.Create(DataSource1);
  95.    DataSource1.Dataset:= SQLQuery1;
  96.  
  97.   DBGrid1.DataSource:=DataSource1;
  98.   DBNavigator1.DataSource:=DataSource1;
  99.   PS:= 'SELECT * FROM HLPMain';
  100.  
  101.   try
  102.     SQLQuery1.Close;
  103.     SQLQuery1.SQL.Text:=ps;
  104.     SQLQuery1.Open;
  105.    except
  106.     on E: Exception do ShowMSG(ps);
  107.    end;
  108. end;
  109.  
  110. procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
  111. begin
  112.   DataSource1.Free;
  113.   SQLQuery1.Free;
  114.   SQLTransaction1.Free;
  115.   ODBCConnection1.Free;
  116. end;
  117.  
  118. end.
  119.  

This is as simple as it gets and can reproduce the exact situation constantly and precisely.
click Navigator

DBGrid will insert the new empty row and waiting for input
Now click on the ComboBox and boom no more inseret...
now focus on the combobox then click
  • Navigator and everything works fine.

+-------------------------------------
Update: add the Options but still the same
Code: Pascal  [Select][+][-]
  1.   DBGrid1.Options:=[dgAlwaysShowSelection,
  2.                     dgCancelOnExit,
  3.                     dgColLines,
  4.                     dgColumnMove,
  5.                     dgColumnResize,
  6.                     dgConfirmDelete,
  7.                     //dgEditing,
  8.                     dgIndicator,
  9.                     dgRowLines,
  10.                     dgTabs,
  11.                     dgTitles
  12.                     ];
  13.  
« Last Edit: October 16, 2024, 03:38:39 am by What I can do »

rvk

  • Hero Member
  • *****
  • Posts: 6760
Re: [SOLVED] Checking the state of a Query?
« Reply #5 on: October 16, 2024, 09:17:01 am »
Update: add the Options but still the same
Code: Pascal  [Select][+][-]
  1.   DBGrid1.Options:=[dgAlwaysShowSelection,
  2.                     dgCancelOnExit,
  3.                     dgColLines,
  4.                     dgColumnMove,
  5.                     dgColumnResize,
  6.                     dgConfirmDelete,
  7.                     //dgEditing,
  8.                     dgIndicator,
  9.                     dgRowLines,
  10.                     dgTabs,
  11.                     dgTitles
  12.                     ];
  13.  
Can you try removing that dgCancelOnExit (or remove the checkmark in Options of the TDBGrid).
The dgCancelOnExit calls the TDataset.Cancel when you exit the TDBGrid.
So when leaving the TDBGrid without posting, the edit gets cancelled and the TDataset returns to dsBrowse.

Quote
dgCancelOnExit  Indicates an active editing operation is cancelled when the control loses focus.

Although it's weird... I got the dgCancelOnExit checked but when I'm edititing the TDBGrid and go to another control, the dataset stays in dsEdit.
You can see that by the little edit cursor in front of the row, otherwise it's a triangle.
And the TDBNavigator also stays in edit mode.

Edit: Forget the dgCancelOnExit. It's only for canceling when there is an dsInsert (not dsEdit) and there has no edits taken place (i.e. empty insert). Only then will the TDBGrid cancel the dsInsert. (not very clear)

But... for me the TDBGrid stays in edit mode when it looses focus. So I'm not sure why this is not the case for you.

Edit #2: I made a little test-project with memdataset (so no actual database but that's not needed to test dbgrid).
Can you check if you have the same here.
Keep close attention to the "|" cursor in front of the row that's being edited (and that the DBNavigator stays in edit) when you focus the DBCombobox.
« Last Edit: October 16, 2024, 09:46:40 am by rvk »

 

TinyPortal © 2005-2018