Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Calculate total values when check column on grid is true
Message
 
To
01/08/2007 12:25:22
Luis Santos
Biglevel-Soluções Informáticas, Lda
Portugal
General information
Forum:
Visual FoxPro
Category:
Forms & Form designer
Environment versions
OS:
Windows XP SP2
Network:
Windows 2003 Server
Database:
MS SQL Server
Miscellaneous
Thread ID:
01244964
Message ID:
01245177
Views:
56
>Hi Naomi
>
>Q.Ok, good. I meant, what is the ControlSource for the column with the checkbox?
>
>Ans.the controlsource of this column is ccheques.deposita, following your instructions I coded the beforerowcolchange event of the grid to scan my cursor, calculate the total, present the sum in another textbox outside the grid and in the end returning to the original recno(). The code is as follows
>PROCEDURE BEFOREROWCOLCHANGE
>LPARAMETERS nColIndex
>LOCAL lnValor as Single
>LOCAL lnRecno as Integer
>
>	IF nColIndex = 1 then
>		SELECT ccheques
>		lnRecno = RECNO()
>		lnValor = 0.00
>		SCAN
>			IF ccheques.deposita = 1 then
>				lnValor = lnValor + ccheques.valor
>			ENDIF
>		ENDSCAN
>		GOTO lnRecno
>		thisform.total.Value = lnValor
>	ENDIF
>Thanks for all your help
>Luis Santos

Luis,
there is NO need to do the every time the user tries to leave Column 1. (I didn't follow whole thread here). You must have something like:
SELECT SUM(valor) FROM ccheques WHERE deposita = 1 INTO ARRAY _laValors
thisform.total.Value = NVL(_laValors[1],0.00)
After you create the record source of the grid.
Then in all columns which could reflect on total value:
In GotFocus event of the control Store old value, in LostFocus of the control Check if OldValue is different than NewOne and if it is make appropriate corrections.
Check this to see what I mean:
oForm = CREATEOBJECT([Form1])
oForm.Show(1)

**************************************************
*-- Form:         form1 (d:\all_zapl\test.scx)
*-- ParentClass:  form
*-- BaseClass:    form
*-- Time Stamp:   08/01/07 07:45:11 PM
*
DEFINE CLASS form1 AS form


    Top = 0
    Left = 0
    DoCreate = .T.
    Caption = "Form1"
    Name = "Form1"


    ADD OBJECT grid1 AS grid WITH ;
        ColumnCount = 3, ;
        Height = 211, ;
        Left = 4, ;
        Top = 8, ;
        Width = 337, ;
        Name = "Grid1", ;
        Column1.Name = "Column1", ;
        Column2.Sparse = .F., ;
        Column2.Name = "Column2", ;
        Column3.Name = "Column3"


    ADD OBJECT text1 AS textbox WITH ;
        Enabled = .F., ;
        Height = 23, ;
        Left = 228, ;
        Top = 221, ;
        Width = 100, ;
        Name = "Text1"


    ADD OBJECT label1 AS label WITH ;
        Caption = "Total Value", ;
        Height = 17, ;
        Left = 113, ;
        Top = 223, ;
        Width = 113, ;
        Name = "Label1"


    PROCEDURE managelostfocus
        AEVENTS(laEvents,0)
        IF NOT laEvents[1].Tag == TRANSFORM(laEvents[1].Value)
           DO CASE
              CASE VARTYPE(laEvents[1].Value) == [L]
                   thisform.Text1.Value = thisform.Text1.Value +  crsTest.Fld3*IIF(laEvents[1].Value,1,-1)
                   laEvents[1].Tag = TRANSFORM(laEvents[1].Value)
              CASE VARTYPE(laEvents[1].Value) == [N]
                   thisform.Text1.Value = thisform.Text1.Value +  laEvents[1].Value - VAL(laEvents[1].Tag)
           ENDCASE
        ENDIF
    ENDPROC


    PROCEDURE managegotfocus
        AEVENTS(laEvents,0)
        laEvents[1].Tag = TRANSFORM(laEvents[1].Value)
    ENDPROC


    PROCEDURE Load
        CREATE CURSOR crsTest (Fld1 C(20), Fld2 L, Fld3 N(14,2))
        RAND(-1)
        FOR asd = 1 TO 20
            INSERT INTO crsTest VALUES (REPLICATE([a],asd), asd%2 == 0, RAND()*1000)
        NEXT
        GO TOP
    ENDPROC


    PROCEDURE Init
        thisform.Grid1.Column2.RemoveObject([Text1])
        thisform.Grid1.Column2.AddObject([Check1], [CheckBox])
        thisform.Grid1.Column2.Check1.Visible = .t.
        thisform.Grid1.Column2.Check1.Caption = []
        
        SELECT SUM(Fld3) FROM crsTest WHERE Fld2 INTO ARRAY laTest
        thisform.Text1.Value = NVL(laTest[1],0)
        BINDEVENT(thisform.Grid1.Column2.Check1,[GotFocus], thisform, [ManageGotFocus],1)
        BINDEVENT(thisform.Grid1.Column3.Text1 ,[GotFocus], thisform, [ManageGotFocus],1)

        BINDEVENT(thisform.Grid1.Column2.Check1,[InterActiveChange], thisform, [ManageLostFocus],1)
        BINDEVENT(thisform.Grid1.Column3.Text1 ,[LostFocus], thisform, [ManageLostFocus],1)
    ENDPROC


ENDDEFINE
*
*-- EndDefine: form1
**************************************************
Against Stupidity the Gods themselves Contend in Vain - Johann Christoph Friedrich von Schiller
The only thing normal about database guys is their tables.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform