I routinely use the CL command DSPFD command and direct the output to the system-supplied outfile QAFDMBR.
The CL code is:
DSPFD FILE(MyLib/MyFile) TYPE(*MBR) OUTPUT(*OUTFILE) FILEATR(*ALL) OUTFILE(QTEMP/QAFDMBR)
OVRDBF FILE(QAFDMBR) TOFILE(QTEMP/QAFDMBR) POSITION(*START)
Then check the DB field &MBNRCD to determine the number of records.
If you are executing this code in batch then the QTEMP file will disappear when the batch job ends. If you execute this code interactively then the QTEMP file will not disappear when your session ends. You should include additional code to handle the existence of the QTEMP file if you plkn to run your code multiple times within the same interactive session.
Retrieving record counts is a major purpose of RTVMBRD. The NBRCURRCD() parameter returns the number of <i>current</i> records. ‘Current’ are ones that are not deleted. This is meaningful because RTVMBRD can also return the number of deleted records in the NBRDLTRCD() parameter. Adding the two values together results in the total amount of space taken for records by the member.