Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Replacing value in Grid
Message
From
21/07/2023 09:06:05
 
 
To
20/07/2023 18:37:06
Luis Santos
Biglevel-Soluções Informáticas, Lda
Portugal
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
01686818
Message ID:
01686820
Views:
54
Seconding the take from Borislav, that code is complicated for a simple task.
Also in Grid.Controlsource is either an alias.field or an object.property.

A grid with only field name as Controlsource is easiest.
Situations with linked tables exist and need alias.field and comments.
No control of record pointer in the code and the replace command will be
replace FI.REF in (lcCursorname) ...
which also raises eyebrows.

First step for me would be to check if Grid(s) can be built with unaliased ControlSource(s),
then perhaps simplify the code via single line SQL update in automatic 2. area.

Weakness of _TALLY already mentioned, lots of invariant code inside loop:
Even if the loop cannot be switched to single update statement,
constant assignment or if should be done outside of the loop
or perhaps switched to isnull() on record level,
while checking table field type only once and not on each record.

HTH
thomas


>Hi,
>
>I have this code to replace value on specific column in my grid, but sometimes nothing happen.
>
  Form, loGrid, lcCursorName, lnRow, lcControlSource, lcNewValue, lcReplaceCmd
>
>loForm = SFT  && Get a reference to the active form (form with focus)
>loGrid = loForm.pageframe1.page1.cont1.grid1  && Replace "pageframe1", "page1", "cont1", and "grid" with the actual names of the controls
>
>* Get the Cursor/Table name from the Grid's RecordSource property
>lcCursorName = loGrid.RecordSource
>
>IF USED(lcCursorName) AND !EMPTY(lcCursorName) AND TYPE("lcCursorName") = "C"
>    * Find the column index by ControlSource
>    lnCol = 0
>    FOR lnIndex = 1 TO loGrid.ColumnCount
>        lcControlSource = loGrid.Columns(lnIndex).ControlSource
>        IF lcControlSource = "FI.REF"
>            lnCol = lnIndex
>            EXIT  && Exit the loop once "REF" column is found
>        ENDIF
>    ENDFOR
>
>    IF lnCol > 0
>        * Loop through the rows and modify the data in the underlying cursor
>        FOR lnRow = 1 TO _TALLY
>            IF VARTYPE(EVAL(lcControlSource)) = "C"  && Check if the field is a string (character)
>                lcNewValue = "Novaref"  && Replace with your desired new value
>                lcReplaceCmd = 'REPLACE ' + lcControlSource + ' IN (lcCursorName) WITH (lcNewValue) FOR RECNO() = lnRow AND EMPTY(EVAL("' + lcControlSource + '"))'
>                &lcReplaceCmd  && Execute the REPLACE command
>            ENDIF
>        ENDFOR
>    ELSE
>        ? "Column 'REF' not found in the grid."
>    ENDIF
>ELSE
>    ? "Invalid or empty cursor name in the grid's RecordSource."
>ENDIF
>
>* Refresh the grid to reflect the changes
>loGrid.Refresh()
>
>READ EVENTS
>
>
>someone could help me!
>Thanks
>Luis
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform