{ recupdat.pas } program RecUpdat; {$IfDef VER80} uses SnipTool, SnipData, SysUtils, WinTypes, WinProcs, DbiProcs, DbiTypes, DbiErrs; {$Else} uses WinTypes, WinCrt, Strings, DbiProcs, DbiTypes, DbiErrs, SnipTool, SnipData; {$EndIf} const szTblName = 'contacts'; { Name of table to be opened } szTblType = szPARADOX; { Type of the above table } {===================================================================== Function: RecordUpdate(); Description: This example shows how to update records in a table. ===================================================================== } var rslt: DBIResult; { Value returned from IDAPI functions } hDb: hDBIDb; { Handle to the database } hCur: hDBICur; { Handle to the table } pRecBuf: pBYTE; { Pointer to the record buffer } curProp: CURProps; { Properties of the table } szLastName: array[0..20] of CHAR; { LastName } szFirstName: array[0..20] of CHAR; { First Name } szCompany: array[0..30] of CHAR; { Company Name } szPhone: array[0..16] of CHAR; { Phone Number } begin Screen('*** Updating Records ***'); Screen(' Initializing IDAPI...'); if (InitAndConnect(hDb) <> DBIERR_NONE) then { Terminate example if } begin { Initialization fails } Screen(''); Screen('*** End of Example ***'); exit; end; Screen(' Set the directory which is used by the database...'); ChkRslt(DbiSetDirectory(hDb, pCHAR(szTblDirectory)), DBIERR_NONE, ' Error - SetDirectory.'); Screen(' Opening the "'+szTblName+'" Table...'); if (ChkRslt(DbiOpenTable(hDb, pCHAR(szTblName), pCHAR(szTblType), nil, nil, 0, dbiREADWRITE, dbiOPENSHARED, xltFIELD, FALSE, nil, hCur), DBIERR_NONE, ' Error - OpenTable.') <> DBIERR_NONE) then begin CloseDbAndExit(hDb); Screen('*** End of Example ***'); exit; end; { Add a record. } Screen(''); Screen(' Determine the size of the record and allocate a'+ ' buffer to hold records...'); ChkRslt(DbiGetCursorProps(hCur, curProp), DBIERR_NONE, ' Error - GetCursorProps.'); { Allocate memory for the record buffer } GetMem(pRecBuf, curProp.iRecBufSize); if not Assigned (pRecBuf) then begin Screen(' Error - Out of Memory.'); ChkRslt(DbiCloseCursor(hCur), DBIERR_NONE, ' Error - CloseCursor.'); CloseDbAndExit(hDb); Screen('*** End of Example ***'); exit; end; { Set the data to add. } StrCopy(szLastName, 'Smith'); StrCopy(szFirstName, 'John'); StrCopy(szCompany, 'Kauai Dive Shoppe'); StrCopy(szPhone, '(304) 113-2244'); Screen(' Initialize the record buffer...'); ChkRslt(DbiInitRecord(hCur, pRecBuf), DBIERR_NONE, ' Error - InitRecord.'); Screen(' Add the fields to the record buffer...'); ChkRslt(DbiPutField(hCur, 1, pRecBuf, @(szLastName)), DBIERR_NONE, ' Error - PutField.'); ChkRslt(DbiPutField(hCur, 2, pRecBuf, @(szFirstName)), DBIERR_NONE, ' Error - PutField.'); ChkRslt(DbiPutField(hCur, 3, pRecBuf, @(szCompany)), DBIERR_NONE, ' Error - PutField.'); ChkRslt(DbiPutField(hCur, 4, pRecBuf, @(szPhone)), DBIERR_NONE, ' Error - PutField.'); Screen(' Add the record to the table...'); Screen(''); rslt := ChkRslt(DbiInsertRecord(hCur, dbiNOLOCK, pRecBuf), DBIERR_NONE, ' Error - InsertRecord.'); if (rslt <> DBIERR_NONE) then begin if (rslt = DBIERR_KEYVIOL) then begin Screen(' Expected error - Continue....'); end else begin ChkRslt(DbiCloseCursor(hCur), DBIERR_NONE, ' Error - CloseCursor.'); CloseDbAndExit(hDb); Screen('*** End of Example ***'); exit; end; end; Screen(' Search for the value in the table...'); Screen(''); ChkRslt(DbiSetToKey(hCur, keySEARCHEQ, FALSE, 0, 0, pRecBuf), DBIERR_NONE, ' Error - SetToKey.'); Screen(' Change the Phone number of the just added record...'); Screen(''); Screen(' Get the record from the table...'); ChkRslt(DbiGetNextRecord(hCur, dbiWRITELOCK, pRecBuf, nil), DBIERR_NONE, ' Error - GetNextRecord.'); Screen(' Change the value in the "Phone" field...'); StrCopy(szPhone, '(304) 222-9876'); ChkRslt(DbiPutField(hCur, 4, pRecBuf, @(szPhone)), DBIERR_NONE, ' Error - PutField.'); Screen(' Write the changes to the table...'); ChkRslt(DbiModifyRecord(hCur, pRecBuf, TRUE), DBIERR_NONE, ' Error - ModifyRecord.'); Screen(' Delete the record which was added...'); Screen(''); ChkRslt(DbiDeleteRecord(hCur, nil), DBIERR_NONE, ' Error - DeleteRecord.'); FreeMem(pRecBuf, curProp.iRecBufSize); Screen(' Close the "'+szTblName+'" table...'); ChkRslt(DbiCloseCursor(hCur), DBIERR_NONE, ' Error - CloseCursor.'); Screen(''); Screen(' Close the Database and exit IDAPI...'); CloseDbAndexit(hDb); Screen('*** End of Example ***'); end.