PRODUCT : Paradox Engine NUMBER : 1538 VERSION : 3.0 OS : All DATE : October 25, 1993 PAGE : 1/3 TITLE : Example of a closest match search with Paradox Engine /***************************************************************** SRCHCLST.C The SETUP.C program must be compiled and run before this example can be executed. This program is a modification of the existing Paradox Engine 3.0 example SRCHFLD.C. This program demonstrates searching for a closest match, rather than an exact match. The key to successfully searching for a closest match is to check for the relevant Engine error codes from the search function. When doing a CLOSESTRECORD search: PXERR_RECNOTFOUND means a record was found that is greater than the search value ( usually the intended result ). The current record is set to this record. PXERR_ENDOFTABLE means that no record is greater. The current record does not change. Another point worth noting is that this example conditionally adds an index to the table if it does not exist. This is because a "closest match" will be undefined unless the field being searched on is in order. *****************************************************************/ #include #include #include "pxengine.h" #define LOC __LINE__ #define TABLENAME "table" char search[] = "Value 1"; // search for a value // not in the table void err(int loc, int rc ); // function to display all // Engine return values int main(void) { char name[80]; TABLEHANDLE tblHandle; RECORDHANDLE recHandle, readBuffer; FIELDHANDLE fldHandle = 1; PRODUCT : Paradox Engine NUMBER : 1538 VERSION : 3.0 OS : All DATE : October 25, 1993 PAGE : 2/3 TITLE : Example of a closest match search with Paradox Engine FIELDHANDLE indexHandles[] = { 1 }; PXCODE pxErr; RECORDNUMBER recNum; struct ffblk ffblk; PXInit(); // don't add index if it already exists... if( (findfirst("table.px",&ffblk,0) !=0 )) // findfirst returns 0 if found { err(LOC, PXKeyAdd(TABLENAME, 1, indexHandles, PRIMARY )); printf("Adding primary key to table\n"); } err(LOC, PXTblOpen(TABLENAME, &tblHandle, 0, 0)); err(LOC, PXRecBufOpen(tblHandle, &recHandle)); // used for search value err(LOC, PXRecBufOpen(tblHandle, &readBuffer)); // used to read found value err(LOC, PXPutAlpha(recHandle, fldHandle, search)); /* Origional code which searched for an exact match on an Alpha field */ /* if ((pxErr = PXSrchFld(tblHandle, recHandle, fldHandle, SEARCHFIRST)) != PXSUCCESS) { if (pxErr == PXERR_RECNOTFOUND) printf("No match found.\n"); else printf("%s\n",PXErrMsg(pxErr)); } else printf("Match found.\n"); */ /* Modified code to search for closest match... */ if( (pxErr = PXSrchFld(tblHandle, recHandle, fldHandle, CLOSESTRECORD)) != PXSUCCESS) { if (pxErr == PXERR_RECNOTFOUND) //we found a record > target { PRODUCT : Paradox Engine NUMBER : 1538 VERSION : 3.0 OS : All DATE : October 25, 1993 PAGE : 3/3 TITLE : Example of a closest match search with Paradox Engine err( LOC, PXRecNum( tblHandle, &recNum )); err( LOC, PXRecGet( tblHandle, readBuffer )); err( LOC, PXGetAlpha( readBuffer, 1, 80, name )); printf("The first record that is greater than\n"); printf("the search value is record number: %d\n", recNum); printf("Field contents: %s\n", name); } else { // If ... PXERR_RECNOTFOUND if( pxErr == PXERR_ENDOFTABLE ) { printf("no record greater\n"); //no record greater than target else } else { // if ... PXERR_ENDOFTABLE printf("%d: %s\n",LOC, PXErrMsg(pxErr)); //some other error? } } } else { // else matching PXSrchFld() if err( LOC, PXRecNum(tblHandle, &recNum )); printf("Match found on record Number %d\n", (int)recNum); } } err( LOC,PXRecBufClose(recHandle)); err( LOC,PXTblClose(tblHandle)); err( LOC,PXExit()); return(pxErr); } void err(int loc, int rc ) { if(rc != PXSUCCESS ) printf("%d: %s\n", loc, PXErrMsg(rc) ); } DISCLAIMER: You have the right to use this technical information subject to the terms of the No-Nonsense License Statement that you received with the Borland product to which this information pertains.