Recent

Author Topic: TDBF.Filter and TDBF.Found  (Read 11520 times)

tshumak

  • New Member
  • *
  • Posts: 36
TDBF.Filter and TDBF.Found
« on: July 12, 2013, 10:33:24 am »
I'm trying to search a TDBF dataset using Filter, and checking for results using Found.  I'm having trouble getting Filter to work.

EX:
TDBF.Filtered := False;
TDBF.Filter := 'FieldName=intValue'; -or- TDBF.Filter := 'FieldName="strValue"';
TDBF.Filtered := True;

//Now check to see if we have results
If TDBF.Found then  // some code here
 -or
If TDBF.FindFirst then // some code here
-or-
TDBF.First;
If Not TDBF.EOF then // some code here

TDBF.Found and TDBF.FindFirst return false.  TDBF.EOF returns what I would expect, but the dataset is not filtered.

Also the comparison operators and be either <, <=, >=, > as well.  I also have
OnFilterRecord event returning Accept := True.  I know I'm not doing something right. The dataset seems to be acting as if I did not apply a filter.

Could some one be good enough to iterate or offer suggestions?  I hope this post makes sense to somebody.  Thanks for reading.

Heres my code.

Code: [Select]
// Function Find.
// Locates first occurance in a record based on
// information supplied. And returns whether or
// not the dataset was able to able to locate the
// information.
//
// Parameters:
//   V : String
//     The value to be searched for.
//   CS : String
//     Compare String.  Used to build filter string
//     to supplied to Field.dataset.filter property.
//   F : TField.
//     Field to be used compare with.
//
// Variables:
//   DS : TDBF
//     Dataset derived from the field passed to this
//     function.
//   N : String
//     Name of the field derived from the field
//     passed to this function

Function TDM.Find(V, CS : String; F : TField) : Boolean;

var
  //DS^ : TDataSet;
  N : String;
  ft : TFieldType;
Begin
  //DS := F.^DataSet;
  N := F.FieldName;
  ft := F.DataType;
  with (TDBF(F.DataSet)) do
  begin

    Filtered := False;
    if ft = ftString then
      Filter := N + CS + '"' + V + '"'
    else
      Filter := N + CS + V;
    Filtered := True;
    Refresh;
 
  // If Found then
  // begin
// check first char in CS to determine direction of search.
      if POS('<', CS) > 0 then
      begin
        Result := FindLast;
  //      Result := Found;
  //      Result := not BOF;
      end
      else
      begin
        Result := FindFirst;
  //      Result := Found;
  //      Result := not EOF;
      end;
  //end
  //else
  //      Result := False;
  end;
End;         
« Last Edit: July 18, 2013, 09:17:18 pm by tshumak »

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: TDBF.Filter and TDBF.Found
« Reply #1 on: July 12, 2013, 10:51:31 am »
Have you checked the documentation to see if your filter text is supported?
https://sourceforge.net/projects/tdbf/files/TDbf%20documentation/TDbf%20documentation%201.1/

Also, wouldn't the filtered records just "disappear", leaving only the found records - i.e. you wouldn't need to use .Found or .FindFirst?
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

tshumak

  • New Member
  • *
  • Posts: 36
Re: TDBF.Filter and TDBF.Found
« Reply #2 on: July 13, 2013, 03:44:17 am »
Thank you for your reply and the link.  Yes, I've read it.  It talks a little about FindFirst Not so much for Found.  Ive read through almost every related post here and on sourceforge as well.  There does not seem to be good examples for Found, or FindFirst.

What is a good method of verifying that Filter was successful other than visually through DBGrid or the like?

 I'm also thinking that my problem may be related to object passing to functions.  Are objects passed by reference or by value?  I think I tried to put the 'var' modifier in the parameter declaration clause, but the compiler complained of some sort of pointer issue or error.

tshumak

  • New Member
  • *
  • Posts: 36
Re: TDBF.Filter and TDBF.Found
« Reply #3 on: July 13, 2013, 04:50:51 am »
Hi BigChimp,

Sorry, I did not catch the second sentence of your post.  Well I'm looking for a method to verify if Filter was successful at locating records that match the filter string.  Tonight I think I might look at RecordCount as an option.

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: TDBF.Filter and TDBF.Found
« Reply #4 on: July 13, 2013, 01:35:25 pm »
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

tshumak

  • New Member
  • *
  • Posts: 36
Re: TDBF.Filter and TDBF.Found
« Reply #5 on: July 14, 2013, 02:30:44 am »
Good, thank you, however I read this one as well.  I did a little more work last night.  I found that I for some reason needed to set an index against some of the tables.  I thought .Filter did not need indexes.  I also see FilterOptions being set.  Does FilterOptions need to be set?  I'm not setting the options. 

I was going ask another question, I forgot what it was.  Sorry.  I'll either reply here or start a new thread when I remember. 

tshumak

  • New Member
  • *
  • Posts: 36
Re: TDBF.Filter and TDBF.Found
« Reply #6 on: July 14, 2013, 04:01:45 am »
Al-right I remember now!  Is the TDBF component included with Lazarus/FPC the same as the component available on sourceforge?  Should I be using the sourceforge component instead? Is it better?

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: TDBF.Filter and TDBF.Found
« Reply #7 on: July 14, 2013, 05:05:45 am »
The FPC one was copied from the sourceforge one. We recently improved a lot of code in the FPC version (in FPC trunk), so I wouldn't use the SourceForge one.

We're planning to try and pass the fixes on to the sourceforge version.
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: TDBF.Filter and TDBF.Found
« Reply #8 on: July 14, 2013, 05:06:39 am »
I did a little more work last night.  I found that I for some reason needed to set an index against some of the tables.  I thought .Filter did not need indexes.  I also see FilterOptions being set.  Does FilterOptions need to be set?  I'm not setting the options. 
TBH, no idea. Haven't looked at the filtering part yet (have been fixing Visual FoxPro support and streams).
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

tshumak

  • New Member
  • *
  • Posts: 36
Re: TDBF.Filter and TDBF.Found
« Reply #9 on: July 15, 2013, 09:58:38 pm »
Yesterday I did a lot more work on this subject.  I have a test app I originally built to test the indexes.  So I modified it to test filtering.  I found that .Filter does work and does not need indexes.  My problem was in my table data being so scrambled, when I applied my filter, the table view came back and looked as if it was unfiltered.  When I applied an index, there was the record I was looking for on the top!  I also needed to set .FilterOptions := [foCaseInsensitive];.

Anyway, .Found and .FindFirst still do not return True.  So I'm still looking for a method to determine if .Filter found records and maybe how many.  I suppose I could use found := .BOF = True and .EOF = False, however I think (.BOF and .EOF) = True still could mean 0 or 1 record found.

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: TDBF.Filter and TDBF.Found
« Reply #10 on: July 16, 2013, 10:21:56 am »
Perhaps .RecordCount could work (don't know if it takes filtering into account).
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

tshumak

  • New Member
  • *
  • Posts: 36
Re: TDBF.Filter and TDBF.Found
« Reply #11 on: July 17, 2013, 04:36:05 am »
More testing last night.  I should be able to use found := not (.BOF and .EOF);.  And .RecordCount displays the total number of records in the currently set index and does not change with a filter applied.  Maybe I could do a hidden DBGrid and look at the Rows.Count property.  FindFirst and FindLast don't do anything.

Thanks again BigChimp for your suggestions.  If I figure out something for FilteredRecordCount, I'll post back here.

BTW - Is the DBF component in Lazarus/FPC the same as the one on sourceforge?  If not, which one is better?

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: TDBF.Filter and TDBF.Found
« Reply #12 on: July 17, 2013, 09:58:33 am »
BTW - Is the DBF component in Lazarus/FPC the same as the one on sourceforge?  If not, which one is better?
Already answered:
http://forum.lazarus.freepascal.org/index.php/topic,21497.msg125826.html#msg125826
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

tshumak

  • New Member
  • *
  • Posts: 36
Re: TDBF.Filter and TDBF.Found
« Reply #13 on: July 17, 2013, 09:26:48 pm »
Ok, sorry about this.  I missed your post in this thread.  I'll stick with FPC.  Thanks again.

OzeCode

  • New Member
  • *
  • Posts: 20
Re: TDBF.Filter and TDBF.Found
« Reply #14 on: July 18, 2013, 02:06:26 am »
Lazarus 1.0.10 and FPC 2.6.2 on Windows 64 bit

I have been using different Methods for the RecordCount...

Code: [Select]
// counts ALL records in Table even if it is the Detail Table
// TOTAL RECORDS in TABLE
DM.Series.Last;   
id := DM.Series.PhysicalRecordCount;


// get number of records in the Detail Table
// where master detail exists
// TOTAL RECORDS in Filtered TABLE
DM.Series.Last; 
NumRecs := DM.Series.ExactRecordCount;

That might be what you can use...
Lazarus 1.0.10, 64 bit; Free Pascal 2.6.2; Windows OS; 
Macintosh Desktop AMD 64 bit - Win7 (64 bit) installed on C:\Bootcamp.

 

TinyPortal © 2005-2018