Article ID: 117218
Article Last Modified on 12/3/2003
**************************************************************
* *
* Function: DBMAX() *
* Parameters: *
* fieldname C Required *
* workarea N/C Optional *
* condition C Optional *
* *
* *
* Purpose: Returns the highest value of any field in the *
* current or specified work area for any logical *
* condition. *
* *
* NOTE: When you are finding the maximum of a field that is *
* not in the current work area, the alias must be supplied *
* in the first parameter. *
**************************************************************
PARAMETERS fieldname, workarea, condition
**************************************************************
* Store parameter count and current work area to memory
* variables.
**************************************************************
STORE PARAMETERS() TO parms
STORE SELECT() TO currselect
**************************************************************
* Store current record and total records to memory variables.
* Initialize m.max memory variable.
**************************************************************
reccount = RECCOUNT(IIF(parms>1,workarea,currselect))
currecord = RECNO(IIF(parms>1,workarea,currselect))
m.max = 0
**************************************************************
* Select the correct work area if not current work area.
**************************************************************
IF parms > 1
SELECT (workarea)
ENDIF
**************************************************************
* Position cursor at top of file. Begin maximum loop.
**************************************************************
GO TOP
SCAN FOR IIF(parms > 2,EVALUATE(condition),.T.)
**************************************************************
* Use a SCAN loop to move through the database and evaluate
* the previous record with the current record and return the
* greater value.
**************************************************************
SKIP -1
STORE EVALUATE(fieldname) TO m.oldmax
m.max = MAX(EVALUATE(fieldname),m.max,m.oldmax)
SKIP 1
SET MESSAGE TO ALLTRIM(STR(m.max,10,2))
ENDSCAN
**************************************************************
* NOTE: The SCAN loop above could be replaced by the following
* code:
*
* CALCULATE FOR IIF(parms > 2,EVALUATE(condition),.T.)
**************************************************************
**************************************************************
* Reset record pointer.
**************************************************************
DO CASE
CASE currecord > reccount
GO BOTTOM
SKIP 1
CASE currecord < reccount
GO TOP
SKIP -1
OTHERWISE
GO currecord
ENDCASE
**************************************************************
* Select the original work area if necessary.
**************************************************************
IF parms > 1
SELECT (currselect)
ENDIF
SET MESSAGE TO
RETURN m.max
ENDIF
The following is an example of how to use this function:
USE customer IN 1
SELECT 0
? dbmax("customer.ytdpurch",1,"state = 'NC'")
Additional query words: VFoxWin FoxWin FoxDos math user- defined function UDF
Keywords: kbcode KB117218