home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / 32SNIPIT.PAK / RANGE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  4.8 KB  |  147 lines

  1. // BDE32 3.x - (C) Copyright 1996 by Borland International
  2.  
  3. // Range.c
  4. #include "snipit.h"
  5.  
  6. static const char szTblName[] = "stock";
  7. static const char szTblType[] = szPARADOX;
  8.  
  9. //=====================================================================
  10. //  Function:
  11. //          Range();
  12. //
  13. //  Description:
  14. //          This example shows how to limit the accessible records
  15. //          within a table using ranges.
  16. //=====================================================================
  17. void
  18. Range (void)
  19. {
  20.     hDBIDb      hDb = 0;        // Handle to the Database
  21.     hDBICur     hCur = 0;       // Handle to the table
  22.     DBIResult   rslt;           // Value returned from IDAPI functions
  23.     CURProps    TblProps;       // Table Properties
  24.     pBYTE       pRecBufLow;     // Record Buffer
  25.     pBYTE       pRecBufHigh;    // Record Buffer
  26.     UINT32      uNumRecs = 0;   // Number of records to display
  27.     DFLOAT      fLowRange;      // Low value for the range
  28.     DFLOAT      fHighRange;     // High value for the range
  29.  
  30.     Screen("*** Range Operations Example ***\r\n");
  31.  
  32.     BREAK_IN_DEBUGGER();
  33.  
  34.     Screen("    Initializing IDAPI...");
  35.     if (InitAndConnect(&hDb) != DBIERR_NONE)
  36.     {
  37.         Screen("\r\n*** End of Example ***");
  38.         return;
  39.     }
  40.  
  41.     Screen("    Setting the Database directory...");
  42.     rslt = DbiSetDirectory(hDb, (pCHAR)szTblDirectory);
  43.     ChkRslt(rslt, "SetDirectory");
  44.  
  45.     Screen("    Open the %s table...", szTblName);
  46.     rslt = DbiOpenTable(hDb, (pCHAR)szTblName, (pCHAR)szTblType, NULL, NULL, 0,
  47.                         dbiREADWRITE, dbiOPENSHARED, xltFIELD, FALSE, NULL,
  48.                         &hCur);
  49.     if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE)
  50.     {
  51.         CloseDbAndExit(&hDb);
  52.         Screen("\r\n*** End of Example ***");
  53.         return;
  54.     }
  55.  
  56.     // Get the size of the record buffer
  57.     rslt = DbiGetCursorProps(hCur, &TblProps);
  58.     ChkRslt(rslt, "GetCursorProps");
  59.  
  60.     // Allocate space for the Record Buffer
  61.     pRecBufLow = (pBYTE)malloc(TblProps.iRecBufSize * sizeof(BYTE));
  62.     pRecBufHigh = (pBYTE)malloc(TblProps.iRecBufSize * sizeof(BYTE));
  63.     if ((pRecBufHigh == NULL) || (pRecBufLow == NULL))
  64.     {
  65.         if (pRecBufHigh) free(pRecBufHigh);
  66.         if (pRecBufLow) free(pRecBufLow);
  67.         Screen("    Error - Could not allocate memory");
  68.         CloseDbAndExit(&hDb);
  69.         Screen("\r\n*** End of example ***");
  70.         return;
  71.     }
  72.  
  73.     fLowRange = 2315.0;  // lowest stock number to display
  74.     fHighRange = 5313.0; // highest stock number to display
  75.  
  76.     Screen("\r\n    Change the range of the table: only display"
  77.            " records\r\n        which have a 'Stock No' between %.1f"
  78.            " and %.1f", fLowRange, fHighRange);
  79.  
  80.     rslt = DbiPutField(hCur, 1, pRecBufLow, (pBYTE)&fLowRange);
  81.     ChkRslt(rslt, "PutField");
  82.  
  83.     rslt = DbiPutField(hCur, 1, pRecBufHigh, (pBYTE)&fHighRange);
  84.     ChkRslt(rslt, "PutField");
  85.  
  86.     rslt = DbiSetRange(hCur, FALSE, 0, 0, pRecBufLow, FALSE, 0, 0,
  87.                        pRecBufHigh, TRUE);
  88.     ChkRslt(rslt, "SetRange");
  89.  
  90.  
  91.     Screen("\r\n    Display the %s table...", szTblName);
  92.  
  93.     rslt = DbiSetToBegin(hCur);
  94.     ChkRslt(rslt, "SetToBegin");
  95.     DisplayInMemoryTable(hCur, uNumRecs);
  96.  
  97.     Screen("\r\n    Change the range of the table: no range set");
  98.     rslt = DbiResetRange(hCur);
  99.     ChkRslt(rslt, "SetRange");
  100.  
  101.     rslt = DbiSetToBegin(hCur);
  102.     ChkRslt(rslt, "SetToBegin");
  103.  
  104.     Screen("    Display the %s table...", szTblName);
  105.     DisplayInMemoryTable(hCur, uNumRecs);
  106.  
  107.     Screen("\r\n    Change to the secondary index on field two...");
  108.     rslt = DbiSwitchToIndex(&hCur, NULL, NULL, 2, FALSE);
  109.     ChkRslt(rslt, "SwitchToIndex");
  110.  
  111.     fLowRange = 4000.0;  // Lowest Vendor Number to display
  112.     fHighRange = 6000.0; // Highest Vendor Number to display
  113.     Screen("\r\n    Change the range of the table: only display records\r\n"
  114.            "        which have a Vendor No between %.1f and"
  115.            " %.1f...", fLowRange, fHighRange);
  116.  
  117.     rslt = DbiPutField(hCur, 2, pRecBufLow, (pBYTE)&fLowRange);
  118.     ChkRslt(rslt, "PutField");
  119.  
  120.     rslt = DbiPutField(hCur, 2, pRecBufHigh, (pBYTE)&fHighRange);
  121.     ChkRslt(rslt, "PutField");
  122.  
  123.     rslt = DbiSetRange(hCur, FALSE, 0, 0, pRecBufLow, FALSE, 0, 0,
  124.                        pRecBufHigh, FALSE);
  125.     ChkRslt(rslt, "SetRange");
  126.  
  127.  
  128.     Screen("\r\n    Display the %s table...", szTblName);
  129.  
  130.     rslt = DbiSetToBegin(hCur);
  131.     ChkRslt(rslt, "SetToBegin");
  132.     DisplayInMemoryTable(hCur, uNumRecs);
  133.  
  134.     // Release allocated memory
  135.     free(pRecBufLow);
  136.     free(pRecBufHigh);
  137.  
  138.     Screen("\r\n    Close the %s table...", szTblName);
  139.     rslt = DbiCloseCursor(&hCur);
  140.     ChkRslt(rslt, "CloseCursor");
  141.  
  142.     Screen("    Close the Database and exit IDAPI...");
  143.     CloseDbAndExit(&hDb);
  144.  
  145.     Screen("\r\n*** End of Example ***");
  146. }
  147.