Subject Re: [ib-support] Key of varying length problem
Author Robert F. Tulloch
> Given that each character has a different meaning, and that they
> cover progressively larger areas as you move from right to left, it
> makes it more complex. For example K9J 5A2 is a postal code (I
> actually lived there my first 20 years or so, the other side of this
> one block street was K9J 5A3). Now here is the problem, if each
> were a dos file, then K9J is the equivilent of K9J* In other words it
> should match any postal code that starts with K9J. I can do this,
> however the problem is, that if I have K9J and I have K9 or even K
> then K9J 5A2 should match K9J but NOT K9 or K. That is where
> the problem is. I don't know how to tell it to look until it finds a
> match and then quit.

Oh, that is no sweat. I do that with lookups in a database all the
time.

I just do a locate and if it fails repeat in a loop knocking off the
last
character until it hits. I chopped some out of this below.

void __fastcall TMainForm::LocateMemClick(TObject *Sender)
{
TLocateOptions LocateOptions;
LocateOptions << loCaseInsensitive <<loPartialKey;
String LookFor;

//Find by Surname
if (Trim(SurNameToFind->Text) == "")
return;
else
{
DisableAssocTenantsBtns();
MainDM->IBM->Close();
MainDM->IBM->SelectSQL->Clear();
MainDM->IBM->SelectSQL->Add("SELECT * FROM MEMBERS");
MainDM->IBM->SelectSQL->Add("ORDER BY NAMELAST ASC");
MainDM->IBM->Open();

LookFor = SurNameToFind->Text;

while ((LookFor.Length()>0) &&
(!MainDM->IBM->Locate("NAMELAST",
LookFor, LocateOptions)))
{
LookFor = LookFor.SubString(1,LookFor.Length()-1);
SurNameToFind->Text = LookFor;
SurNameToFind->Refresh();
}
if (LookFor.Length() == 0)
{
Application->MessageBox("The last name could not be
located "
"in the Database. Please check the
name "
"and try again.",
"Locate Member by Last Name",
MB_OK | MB_ICONEXCLAMATION);
ScrollMembersClick(this, nbTFirst);
return;
}
}
ScrollMembersClick(this, nbTFirst);
}