{ search.pas } program Search; {$N+} {$IfDef VER80} uses SnipTool, SnipData, SysUtils, WinTypes, WinProcs, DbiProcs, DbiTypes, DbiErrs; {$Else} uses WinTypes, WinCrt, Strings, DbiProcs, DbiTypes, DbiErrs, SnipTool, SnipData; {$EndIf} const szTblName = 'customer'; { Name of table to be created. } szTblType = szPARADOX; { Table type to use. } {======================================================================== Code: SearchTable(); Input: None. Return: None. Description: This sample code will open a customer table and search it for the key. ======================================================================== } var rslt: DBIResult; { Value returned from IDAPI functions } hDb: hDBIDb; { Handle to the database } hCur: hDBICur; { Handle to the table } TblProps: CURProps; { Table properties } sCustNum: String; const pBuf: pBYTE = nil; { Pointer to the record buffer } fCustNum: FLOAT = 3052.00; { FLOAT value to seach for } szKey: pCHAR = 'Sarasota'; { CHAR value to search for } begin Screen('*** Search Record Example ***'); Screen(' Initializing IDAPI...'); if (InitAndConnect(hDb) <> DBIERR_NONE) then { Terminate example if } begin { Initialization fails } Screen(''); Screen('*** End of Example ***'); exit; end; Screen(' Setting the Database directory...'); ChkRslt(DbiSetDirectory(hDb, pCHAR(szTblDirectory)), DBIERR_NONE, ' Error - SetDirectorry.'); Screen(' Opening the Customer Table...'); rslt := ChkRslt(DbiOpenTable(hDb, pCHAR(szTblName), pCHAR(szTblType), nil, nil, 0, dbiREADWRITE, dbiOPENSHARED, xltFIELD, FALSE, nil, hCur), DBIERR_NONE, ' Error - OpenTable.'); if (rslt <> DBIERR_NONE) then { Check if successfull } begin Screen(' Close the Database and exit IDAPI...'); CloseDbAndExit(hDb); Screen(''); Screen('*** End of Example ***'); exit; end; { Get the properties of the table in order to create the correct buffer size for the record. } ChkRslt(DbiGetCursorProps(hCur,TblProps), DBIERR_NONE, ' Error - GetCursorProps.'); GetMem(pBuf, TblProps.iRecBufSize); if not Assigned (pBuf) then begin Screen(' Error - Out of memory.'); Screen(' Close the Database and exit IDAPI...'); CloseDbAndExit(hDb); Screen(''); Screen('*** End of Example ***'); exit; end; ChkRslt(DbiPutField(hCur, 1, pBuf, @(fCustNum)), DBIERR_NONE, ' Error - PutField.'); { Example 1: Search for full key value using a record buffer. } Str(fCustNum : 7 : 2, sCustNum); Screen(' Searching for the record where field: Customer'+ ' Number := '+ sCustNum); rslt := DbiSetToKey(hCur, keySEARCHEQ, FALSE, 0, 0, pBuf); if (rslt = DBIERR_NONE) then begin { Cursor is left on the crack before the record. } Screen(''); Screen(' Record was found:'); DisplayNextRecord(hCur); end else begin Screen(''); Screen(' Record was not found...'); ChkRslt(rslt, DBIERR_NONE, ' Error - SetToKey.'); end; { Example 2: Search on a partial key value using a direct buffer. } Screen(''); Screen(' Switching to the "Place" index...'); ChkRslt(DbiSwitchToIndex(hCur, pCHAR('Place'), nil, 0, FALSE), DBIERR_NONE, ' Error - SwitchToIndex.'); Screen(' Searching for the record using the "Place" index'+ ' where'); Screen(' the field: City = "'+StrPas(szKey)+'"... '); rslt := DbiGetRecordForKey(hCur, TRUE, 0, StrLen(szKey), (szKey), nil); if (rslt = DBIERR_NONE) then begin { Cursor is left on the record. } Screen(''); Screen(' Record was found:'); DisplayCurrentRecord(hCur); end else begin Screen(''); Screen(' Record was not found... '); ChkRslt(rslt, DBIERR_NONE, ' GetRecordForKey.'); end; Screen(''); Screen(' Close the Table...'); ChkRslt(DbiCloseCursor(hCur), DBIERR_NONE, ' Error - CloseCursor.'); FreeMem(pBuf, TblProps.iRecBufSize); Screen(' Close the Database and exit IDAPI...'); CloseDbAndExit(hDb); Screen('*** End of Example ***'); end.