none
Adding a check box in a grid for deleting records RRS feed

  • Question

  • Hello all

    I have added a checkbox to grid column in the following way:

    • Select the column
    • Click on checkbox
    • Click on the column
    • Change the column property to sparse=.f.
    • Change the column CurrentControl to Check1
    • Change the column to readonly=.f.
    • Assign the controlsource for the check box to field DELETEIT

    When I run the form and click the check box, I have an error "Error with check1 - Value : Data type mismatch"

    What I am trying to achieve is this:

    • When I click on the checkbox, I want the field DELETEIT to be populated with a Y which means when I run a piece of code it will delete all the records I have checkboxed.

    Can anyone please explain what I am missing in relation to adding the check box and making it work?

    The grid is populated with a table, I'm using VFP9 with SP2

    Thank you

    Wednesday, November 28, 2018 3:45 PM

Answers

  • Ok I solved it.

    It appears that the grids recordsource did not contain the name of the table and that the field name DELETEIT should been the controlsource for both the column and the check1 checkbox.

    Thanks to those who posted here
    Thursday, November 29, 2018 5:01 PM

All replies

  • Checkboxes can either be numeric or logical, but not character. That is, you can 1/0 or .T./.F. Make the field logical and all will be well.

    Tamar

    Wednesday, November 28, 2018 9:32 PM
    Moderator
  • What method are you using to fetch data in grid? I mean what command line? SQL or just USE? RecordSourceType=?

    ShahidSQL

    Thursday, November 29, 2018 7:40 AM
  • Thank you for your replies guys.

    Tamar: The field called DELETEIT is Logical but I'm still getting the error.

    ShahidSQL: It's straight forward USE with a free table called PRODUCTS

    Thursday, November 29, 2018 12:18 PM
  • Both is possible, what you bind to the grid column can be a field from a workarea, it doesn't matter if that workarea is populated by a USE or a query which results in a cursor or a view. In the end all of these are DBFs, some have their own DBF file, some are in TMP files, but all are DBFs.

    The grid is sensitive to changes in the underlying workare, so no matter what is the recordsource, it

    a) should exist before the grid inits
    b) should stay used, not be overwritten.

    Even just overwriting a cursor with SQL INTO grid.recordsource this means the old cursor will be dropped before the new cursor exists, a short time span the grid is without datasource and drops all its columns and extra info, next moment it can become totally blank or just reconstruct the number of columns in the renewed recordsouce.

    An action possible not causeing that effect is a REQUERY(grid.recordsource) which can be used for view cursors or cursoradapter cursors. USE another DBF qand your grid is destroyed, SELECT  something FROM a.DBF INTO grid.recordsource and the grid reconstructs. The only other way other than a view or cursoradapter is a readwrite cursor you ZAP and repopulate. That way the binding stays intact, just the data changes and that's of course possible, otherwise a grid would be pointless.

    In your case the problem is simpler though, the DELETEIT field is not a LOGIC field or not an INT only 0 or 1, I'd recommend setting this to a LOGIC data type, you can't have a CHAR(1) with 'Y' OR 'N', the checkbox already is the control that turns the technical display values .T. and .F. to a checked or unchecked box, that's the norm. The checkbox is not as versatile as a texstbox you can bind to almost any field type, checkboxes are meant for logic fields and at max change between the three states checked, unchecked and greyed out checked, when the value is .NULL,, alternative is 0,1 and .NULL. in an nullable int field, but you can't configure this to your liking and have 'Y' and 'N'. IIRC MS Access allows some such sutomization, but not checkboxes in VFP.

    You can set the style to grpahical (button), but then the change is subtle, it once was the 3D look of pressed or not presed 3D button, but With Win8 the norm changed to flat styles and that changed this look to make less differntiated and that makes less sense. The graphical button style shows a caption, but the caption doesn't change with the value, so you'd need to do that with your own checkbox class and let it react to the current value in its change events.

    Bye, Olaf.

    • Edited by OlafDoschke Thursday, November 29, 2018 12:23 PM
    Thursday, November 29, 2018 12:19 PM
  • Checkbox returns numeric value 1 or 0 not .T. or .F. I think ...

    ShahisSQL

    Thursday, November 29, 2018 1:19 PM
  • Olaf: "The field called DELETEIT is Logical but I'm still getting the error."

    This has been set by modifying the structure to Logical.

    I have assigned the checkbox to the above field.

    The purpose of this as mentioned is just for the user to be able to checkbox the record in the row where the record is so I can set that or multiple records to .T. and run a piece of code after that deletes all the records that are .T.

    I hope that makes sense and I am sure this is something simple I am missing here.

    Thursday, November 29, 2018 4:08 PM
  • Ok I solved it.

    It appears that the grids recordsource did not contain the name of the table and that the field name DELETEIT should been the controlsource for both the column and the check1 checkbox.

    Thanks to those who posted here
    Thursday, November 29, 2018 5:01 PM
  • When you bind a grid, you set the grid column.controlsource, not controlsources of the individual controls, they inherit that from the grid columns. Only setting the checkbox.controlsource won't bind it correctly.

    You can bind to field name only, that's not a problem. It's recommended to be verbose in all cases to have no ambiguous situations, but that alone shouldn't be a problem. Field names also are prioritized over variable names.

    Bye, Olaf.


    • Edited by OlafDoschke Thursday, November 29, 2018 7:15 PM
    Thursday, November 29, 2018 7:05 PM
  • Thank you Olaf
    Thursday, November 29, 2018 11:01 PM