home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1997 May / VPR9705A.ISO / VPR_DATA / PROGRAM / CBTRIAL / SETUP / DATA.Z / FINDCUST.CPP < prev    next >
C/C++ Source or Header  |  1997-02-14  |  3KB  |  91 lines

  1. //----------------------------------------------------------------------------
  2. //Borland C++Builder
  3. //Copyright (c) 1987, 1997 Borland International Inc. All Rights Reserved.
  4. //----------------------------------------------------------------------------
  5. //---------------------------------------------------------------------------
  6. #include <vcl.h>
  7. #pragma hdrstop
  8.  
  9. #include "FindCust.h"
  10. #include "DM.h"
  11.  
  12. //---------------------------------------------------------------------------
  13. #pragma resource "*.dfm"
  14.  
  15. TfmFindCust* fmFindCust;
  16.  
  17. //---------------------------------------------------------------------------
  18. __fastcall TfmFindCust::TfmFindCust(TComponent* Owner)
  19.   : TForm(Owner)
  20. {
  21. }
  22.  
  23. //---------------------------------------------------------------------------
  24. void __fastcall TfmFindCust::ComboBox1Change(TObject *Sender)
  25. {
  26.   // Make the field we're locating in the leftmost field.
  27.   DM2->CustLookup->FieldByName(ComboBox1->Text)->Index = 0;
  28.  
  29.   // In order to use FindNearest, we have to be indexed based on the field
  30.   // we're doing the find in.  If we're using Locate, it doesn't matter what
  31.   // the current index is, so we will set it to company, which is the default
  32.   // in the Customer form.
  33.   if (ComboBox1->Text == "CustNo")
  34.     DM2->CustLookup->IndexName = "";
  35.   else
  36.     DM2->CustLookup->IndexName = "ByCompany";
  37.  
  38.   // Let the user choose whether or not to use a filter if they're going to be
  39.   // searching based on a field that has no index.
  40.   cbUseFilter->Enabled = ComboBox1->Text != "CustNo" && ComboBox1->Text != "Company";
  41.   if (cbUseFilter->Enabled)
  42.     cbUseFilter->SetFocus();
  43.   else
  44.     Edit1->SetFocus();
  45.   Edit1->Text = "";
  46. }
  47. //---------------------------------------------------------------------
  48. void __fastcall TfmFindCust::Edit1Change(TObject *Sender)
  49. {
  50.   // Now, we begin to find the field, as the user types, i.e.,
  51.   // incrementally.  FindNearest is used in the two indexed fields,
  52.   // Company and CustNo; Locate is used for all the non-indexed
  53.   // fields.
  54.   if (ComboBox1->Text == "Company") {
  55.     TVarRec q(Edit1->Text);
  56.     DM2->CustLookup->FindNearest(&q, 0);
  57.   }
  58.   else if (ComboBox1->Text == "CustNo") {
  59.     try {
  60.       TVarRec q(_atold(Edit1->Text.c_str()));
  61.       if (Edit1->Text != "")
  62.         DM2->CustLookup->FindNearest(&q, 0);
  63.     }
  64.     catch (...) {
  65.       throw Exception(Edit1->Text + " is not a valid customer number");
  66.     }
  67.   }
  68.   else { // Some non-indexed field.
  69.  
  70.     Set<TLocateOption,0,1> flags;
  71.     flags << loCaseInsensitive << loPartialKey;
  72.     // Possibly apply the filter so we see only records matching this value.
  73.     bool found;
  74.     found = DM2->CustLookup->Locate(ComboBox1->Text,
  75.                                     Edit1->Text,
  76.                                     flags);
  77.  
  78.     if (found && cbUseFilter->Checked) {
  79.       // Get an expression such as State = 'CA'.
  80.       DM2->CustLookup->Filter = ComboBox1->Text + " = '" + Edit1->Text + "'";
  81.       DM2->CustLookup->Filtered = true;
  82.       if (DM2->CustLookup->RecordCount == 0) //Filter is possibly too restrictive
  83.         DM2->CustLookup->Filter = ComboBox1->Text + " >= '" + Edit1->Text + "'";
  84.     }
  85.     else
  86.       DM2->CustLookup->Filtered = false;
  87.   }
  88.   DM2->CustLookup->Refresh();
  89. }
  90. //---------------------------------------------------------------------------
  91.