Crystal Report记录数达1000时重置计数异常问题求助
Your original code has a key issue: the condition if recordnumber >=1000 runs for every single record after the first 999. That means after hitting record 1000, you keep updating LastRecordCount to the current record number every time—so the subtraction recordnumber - LastRecordCount doesn’t give you the reset count you want. Instead, it either shows the full record number or (if there was a leftover value from a previous run) an incorrect offset like 2233.
Here’s the corrected code that properly resets the count every 1000 records:
global numbervar LastRecordCount; global numbervar PageRecordCount; // Reset the tracking variable at the start of the report if recordnumber = 1 then LastRecordCount := 0; // Update the tracking variable only when we finish a 1000-record block if recordnumber mod 1000 = 0 then ( PageRecordCount := 1000; // Show 1000 instead of 0 for the last record of a block LastRecordCount := recordnumber; ) else ( PageRecordCount := recordnumber - LastRecordCount; ); PageRecordCount;
How this works:
- Initialization: We reset
LastRecordCountto 0 on the first record to avoid leftover values from previous report runs (a common pitfall with global variables in Crystal Reports). - Block Check: Using
recordnumber mod 1000 = 0, we only trigger the reset logic when we reach the end of a 1000-record block (1000, 2000, 3000, etc.). For these records, we explicitly set the count to 1000 (since modulus would give 0 otherwise). - Current Count Calculation: For all other records, we subtract the last completed block’s total from the current record number to get the position within the current 1000-record group.
Simpler Alternative (No Variables Needed)
If you don’t need the global variables for other logic, you can use the modulus operator directly for a more concise solution:
if recordnumber mod 1000 = 0 then 1000 else recordnumber mod 1000;
This does the same thing: returns 1-1000 for each block, and correctly shows 1000 instead of 0 for the final record of each group.
内容的提问来源于stack exchange,提问作者emy shoj

