Page 1 of 1

dbEval()

Posted: Sat Mar 17, 2018 3:26 pm
by unixkd
I try to convert this loop to dbeval() but certain records are omitted in the dbeval() routine below.

Static Function _GetBatchTotals()
Local aBT[0], cJVN
Select("MyTable")
Do While !Eof()
cJVN := MyTable->JVN
aBT := {0,0}
Do While cJVN == MyTable->JVN
aBT[1] += Mytable->dr
aBT[2] += Mytable->cr
DBSkip()
Enddo
Enddo
Return(aBT)

Static Function _GetBatchTotals()
Local aBT[0], cJVN
Select("MyTable")
DbEval({|| cJVN := MyTable->JVN, aBT := {0,0}, DC_Dowhile({|| cJVN == MyTable->JVN}, {||aBT[1] += Mytable->dr, aBT[2] += Mytable->cr, dbSkip() })})
Return aBt

Re: dbEval()

Posted: Sat Mar 17, 2018 4:55 pm
by rdonnay
certain records are omitted in the dbeval() routine below.
dbEval() automatically does a dbSkip() through all records that match your conditions.
If you put a dbSkip() in the code block, you will miss every other record.

Re: dbEval()

Posted: Sat Mar 17, 2018 5:13 pm
by unixkd
Hi Roger

My upper routine that utilises nested do .. while works just fine and that is what I am trying to accomplish using dbeval(). Without the dbskip I get incorrect result

Thanks.

Joe

Re: dbEval()

Posted: Sun Mar 18, 2018 8:52 am
by rdonnay
I see that you don't have an alias on your dbSkip() function.
Maybe that's the problem.
You may be skipping the wrong work area.

Re: dbEval()

Posted: Mon Apr 02, 2018 11:32 am
by messaoudlazhar
False logic
suppose the table is sorted on the variable JVN
The array aBT contains the last values of the table corresponding to the last value of JVN since aBT is reinitialized with each change of the value of JVN.

to have the same result:

Static Function _GetBatchTotals()
Local aBT[0], cJVN
Select("MyTable")
aBT := {0,0}
Go bott // Last Value JVN
cJVN:=MyTable->JVN
Locate for JVN=cJVN
// OR if indexed
// MyTable->(Dbseek(cJVN))
DbEval({|| aBT[1] += Mytable->dr, aBT[2] += Mytable->cr},,,,,.T.)
Return aBt