Recent

Author Topic: IBX - Field not found - this cannot be  (Read 1151 times)

Nicole

  • Hero Member
  • *****
  • Posts: 970
IBX - Field not found - this cannot be
« on: October 04, 2022, 09:18:08 am »
Here is something, which may be  most obvious, but I cannot see it.
There is a very short and simple DB-request and a "Field not found".
The field is there, I post a SQL-request by FlameRobin (screenshot).

These are the 2 methods.

Code: Pascal  [Select][+][-]
  1. procedure TFrame_Margin.SpeedButton_alleMarginsClick(Sender: TObject);
  2. Var s: string;
  3.  
  4. begin
  5. RichMemo_Marginausgabe.Clear;
  6. s:='';
  7.  
  8. IBQuery_Margin.SQL.Text:='SELECT WARE, MARGIN, DATUM FROM TBMARGINS  order by DATUM desc';
  9. IBQuery_Margin.Active:=true;   // Query wird scharf gestellt
  10.  
  11.  while not IBQuery_Margin.Eof do begin
  12.        s:= s + IBQuery_Margin.FieldByName('WARE').AsString + ' '; // <===== problem
  13.        s:= s + IBQuery_Margin.FieldByName('MARGIN').AsString + ' ';
  14.        s:= s + IBQuery_Margin.FieldByName('DATUM').AsString;
  15.        RichMemo_Marginausgabe.Lines.Add(s);
  16.        IBQuery_Margin.Next;
  17.     end;
  18.  
  19. end;

gives the error:
Code: Text  [Select][+][-]
  1. [Debuggerausnahmen-Nachricht]
  2.  
  3.  
  4. [Break]
  5. Projekt project_Tiger hat Exception-Klasse »EDatabaseError« ausgelöst mit der Meldung:
  6. IBQuery_Margin : Field not found : "WARE"
  7.  
  8.  Bei Adresse 1002D1E73
  9.  
  10.  
  11. [Diesen Ausnahmetyp übergehen]
  12.  
  13. [Continue]


Put the line in comment does not give the error:

Code: Pascal  [Select][+][-]
  1. procedure TFrame_Margin.SpeedButton_alleMarginsClick(Sender: TObject);
  2. Var s: string;
  3.  
  4. begin
  5. RichMemo_Marginausgabe.Clear;
  6. s:='';
  7.  
  8. IBQuery_Margin.SQL.Text:='SELECT WARE, MARGIN, DATUM FROM TBMARGINS  order by DATUM desc';
  9. IBQuery_Margin.Active:=true;  
  10.  
  11.  while not IBQuery_Margin.Eof do begin
  12.       // =====>  s:= s + IBQuery_Margin.FieldByName('WARE').AsString + ' ';
  13.        s:= s + IBQuery_Margin.FieldByName('MARGIN').AsString + ' ';
  14.        s:= s + IBQuery_Margin.FieldByName('DATUM').AsString;
  15.        RichMemo_Marginausgabe.Lines.Add(s);
  16.        IBQuery_Margin.Next;
  17.     end;
  18.  
  19. end;


What is it, I cannot see?
Thanks!

rvk

  • Hero Member
  • *****
  • Posts: 6111
Re: IBX - Field not found - this cannot be
« Reply #1 on: October 04, 2022, 09:38:27 am »
That's weird. I don't see anything wrong.

You are sure this is the exact cijfer in your program (without snipping anything off)?
What happens if you change IBQuery_Margin.Active:=true to IBQuery_Margin.Open.

One potential BIG problem...
If you press the speedbutton a second time you SHOULD get an error because you don't close IBQuery_Margin on exit procedure. And that will mean you can't set IBQuery_Margin.SQL because it's still open.

So make sure to close IBQuery_Margin before setting Active or Open or better, close it before exit procedure. That's why I prefer to create a new instance of TIBQuery in the same procedure if its not used elsewhere. Doing it like you do here is asking for a 'hanging open query'.


Nicole

  • Hero Member
  • *****
  • Posts: 970
Re: IBX - Field not found - this cannot be
« Reply #2 on: October 04, 2022, 09:55:27 am »
I tried it all.
The strange thing is: "WARE" is the problem. "Margin" and "DATUM" is processed as expected.

I tried as well:
'select WARE as ware_", - with the same error.

This is that spooky. I hesitated to post it, because I said to myself, "this will go away after a night of sleep".

« Last Edit: October 04, 2022, 09:57:22 am by Nicole »

rvk

  • Hero Member
  • *****
  • Posts: 6111
Re: IBX - Field not found - this cannot be
« Reply #3 on: October 04, 2022, 09:57:35 am »
You tried the close as well?

What happens if you add a dummy field as first field?
You can just use 1 (as number).

Select 1 as dummy, WARE, etc.


Zvoni

  • Hero Member
  • *****
  • Posts: 2319
Re: IBX - Field not found - this cannot be
« Reply #4 on: October 04, 2022, 10:00:46 am »
For testing.
No idea if the Prop-Name is the same as with TSQLQuery.
Code: Pascal  [Select][+][-]
  1. Var
  2.   i:Integer;
  3. If not IBQuery_Margin.Eof do begin
  4.        For i:=0 To IBQuery_Margin.Fields.Count-1 Do Writeln(IBQuery_Margin.Fields[i].FieldName); //Or IBQuery_Margin.Fields[i].DisplayName
  5.     end;
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Nicole

  • Hero Member
  • *****
  • Posts: 970
Re: IBX - Field not found - this cannot be
« Reply #5 on: October 04, 2022, 10:18:42 am »
Attempt with negative results.


Code: Pascal  [Select][+][-]
  1. procedure TFrame_Margin.SpeedButton_alleMarginsClick(Sender: TObject);
  2. Var s: string;
  3.  
  4. begin
  5. RichMemo_Marginausgabe.Clear;
  6. s:='';
  7.  
  8. IBQuery_Margin.Active:=false;
  9. IBQuery_Margin.Close;
  10.  
  11. IBQuery_Margin.SQL.Text:='SELECT 1 as dummy, WARE, MARGIN, DATUM FROM TBMARGINS  order by DATUM desc';
  12. //IBQuery_Margin.Active:=true;   // Query wird scharf gestellt
  13.   IBQuery_Margin.Open;
  14.  
  15.  while not IBQuery_Margin.Eof do begin
  16.        s:= s + IBQuery_Margin.FieldByName('WARE').AsString + ' ';
  17.        s:= s + IBQuery_Margin.FieldByName('MARGIN').AsString + ' ';
  18.        s:= s + IBQuery_Margin.FieldByName('DATUM').AsString;
  19.        RichMemo_Marginausgabe.Lines.Add(s);
  20.        IBQuery_Margin.Next;
  21.     end;
  22.  
  23. end;


==================

and

  While not IBQuery_Margin.Eof do begin
    //   For i:=0 To IBQuery_Margin.Fields.Count-1 Do s:= s + (IBQuery_Margin.Fields.FieldName); //Or IBQuery_Margin.Fields.DisplayName
    For i:=0 To IBQuery_Margin.Fields.Count-1 Do s:= s + IBQuery_Margin.Fields.DisplayName;
    end;

makes - ou? - the thing freeze.


and:
Restart of Lazarus and restart of Win 7 is done.
It is that spooky!
« Last Edit: October 04, 2022, 10:46:31 am by Nicole »

dseligo

  • Hero Member
  • *****
  • Posts: 1196
Re: IBX - Field not found - this cannot be
« Reply #6 on: October 04, 2022, 11:03:43 am »
  While not IBQuery_Margin.Eof do begin
    //   For i:=0 To IBQuery_Margin.Fields.Count-1 Do s:= s + (IBQuery_Margin.Fields.FieldName); //Or IBQuery_Margin.Fields.DisplayName
    For i:=0 To IBQuery_Margin.Fields.Count-1 Do s:= s + IBQuery_Margin.Fields.DisplayName;
    end;

makes - ou? - the thing freeze.

Do it outside while, like this:
Code: Pascal  [Select][+][-]
  1. procedure TFrame_Margin.SpeedButton_alleMarginsClick(Sender: TObject);
  2. Var i:Integer;
  3. begin
  4.   IBQuery_Margin.Close;
  5.   IBQuery_Margin.SQL.Text:='SELECT WARE, MARGIN, DATUM FROM TBMARGINS  order by DATUM desc';
  6.   IBQuery_Margin.Open;
  7.  
  8.   If not IBQuery_Margin.Eof do begin
  9.     For i:=0 To IBQuery_Margin.Fields.Count-1 Do Writeln(IBQuery_Margin.Fields[i].FieldName); //Or IBQuery_Margin.Fields[i].DisplayName
  10.   end;

dseligo

  • Hero Member
  • *****
  • Posts: 1196
Re: IBX - Field not found - this cannot be
« Reply #7 on: October 04, 2022, 11:06:37 am »
What component is IBQuery_Margin?

Try this:
Code: Pascal  [Select][+][-]
  1. procedure TFrame_Margin.SpeedButton_alleMarginsClick(Sender: TObject);
  2. Var s: string;
  3. begin
  4.   RichMemo_Marginausgabe.Clear;
  5.  
  6.   IBQuery_Margin.Close;
  7.   IBQuery_Margin.SQL.Text:='SELECT WARE, MARGIN, DATUM FROM TBMARGINS  order by DATUM desc';
  8.   IBQuery_Margin.Open;
  9.  
  10.   while not IBQuery_Margin.Eof do begin
  11.     s := ''; // you probably want this here because you add lines in memo in each iteration
  12.     s:= s + IBQuery_Margin.Fields[0].AsString + ' ';
  13.     s:= s + IBQuery_Margin.FieldByName('MARGIN').AsString + ' ';
  14.     s:= s + IBQuery_Margin.FieldByName('DATUM').AsString;
  15.     RichMemo_Marginausgabe.Lines.Add(s);
  16.     IBQuery_Margin.Next;
  17.   end;
  18. end;
« Last Edit: October 04, 2022, 11:08:52 am by dseligo »

Nicole

  • Hero Member
  • *****
  • Posts: 970
[solved] Re: IBX - Field not found - this cannot be
« Reply #8 on: October 04, 2022, 11:28:34 am »
My problem is "solved" and I need a little help for base functions of IBQueries.

This way I found it.

I added a component "IBQuery_Margin1" and did the same with it.
This worked.

So WHAT!!, what is the difference?
I found it, if I right-clicked the IBQuery_Margin and said "edit fields" (in German = bearbeiten).
There I found listed: Margin and datum (=date) and NOT Ware.
"Ware" is the one which has been the prob lem all the time.

See screenshot.

Is there a way to give the beginner-user an error message, if this happens reading,
"you dummy have used the field editor and not added the field, you request now"

or
Do anything from programmer-side, that the field-editor fields do not "hide" fields?

or
Do not allow to use those field-edited-queries any more for other queries?
(this is tricky as it may block code, which worked for years)

dseligo

  • Hero Member
  • *****
  • Posts: 1196
Re: [solved] Re: IBX - Field not found - this cannot be
« Reply #9 on: October 04, 2022, 11:50:27 am »
I found it, if I right-clicked the IBQuery_Margin and said "edit fields" (in German = bearbeiten).
There I found listed: Margin and datum (=date) and NOT Ware.
"Ware" is the one which has been the prob lem all the time.

See screenshot.

Is there a way to give the beginner-user an error message, if this happens reading,
"you dummy have used the field editor and not added the field, you request now"

or
Do anything from programmer-side, that the field-editor fields do not "hide" fields?

or
Do not allow to use those field-edited-queries any more for other queries?
(this is tricky as it may block code, which worked for years)

I avoid adding fields to TSQLQuery because of harder maintenance.

You could dynamically create queries needed only for procedure, function or method:
Code: Pascal  [Select][+][-]
  1. var MyLocalQuery: TSQLQuery;
  2. begin
  3.   MyLocalQuery := TSQLQuery.Create(nil);
  4.   try
  5.     MyLocalQuery.SQLConnection := YourDBConnection;
  6.     MyLocalQuery.SQL.Text := 'select ...';
  7.     MyLocalQuery.Open;
  8.     ...
  9.  
  10.   finally
  11.     MyLocalQuery.Free;
  12.   end;

To check if you have more of this fields added to SQLqueries, you could search your project for declarations that ends in 'Field;'.

They are declared in forms interfaces like this:
Code: Pascal  [Select][+][-]
  1.     SQLQuery1test1: TLongintField;
  2.     SQLQuery1test2: TStringField;

tonyw

  • Sr. Member
  • ****
  • Posts: 319
    • MWA Software
Re: [solved] Re: IBX - Field not found - this cannot be
« Reply #10 on: October 04, 2022, 06:27:32 pm »
Is there a way to give the beginner-user an error message, if this happens reading,
"you dummy have used the field editor and not added the field, you request now"

or
Do anything from programmer-side, that the field-editor fields do not "hide" fields?

or
Do not allow to use those field-edited-queries any more for other queries?
(this is tricky as it may block code, which worked for years)
The Fields Editor is part of the Lazarus/Delphi IDE and the TDataset model and this is common behaviour for all database drivers. It should avoid you having to use the inefficient "FieldByName" method. When you add a field using the fields editor, it adds a reference to the field as a property of the form allowing a more efficient way of accessing a field by a property name rather than having to look up the field by name at run time. If, once you used the fields editor, you always accessed fields as form properties then the compiler would tell you when you haven't added a field using the fields editor.

In principle, it would be possible to add an error message as you suggest. The problem is that the fix has to be to the FPC RTL. The TDataset.FieldByName method is not virtual. Neither is the FindField method The implication is that IBX cannot itself override the method to improve the error message. This has to be done in the RTL.

Nicole

  • Hero Member
  • *****
  • Posts: 970
Re: IBX - Field not found - this cannot be
« Reply #11 on: October 06, 2022, 05:15:29 pm »
thank you for the explanation!

 

TinyPortal © 2005-2018