Topic: How to edit a record when using SQL Query on a button

Hello,
this is the first time I have used a SQL Query button when displaying table records in a TableGrid. I press the button and the result of the SQL Query is displayed in the table with the headings all fine. I place a button on the form to edit the selected record but when I click the button, I get the message "No record selected for editing". I have created the edit button the same as I would for any other normal Tablegrid. So there is a difference between a normal table and when a SQL Query is used. Can somebody please help me so I can press the button to edit the highlighted record.
Thanks in advance for any help.
Regards,
David

2 (edited by derek 2017-12-01 23:50:11)

Re: How to edit a record when using SQL Query on a button

Hi David,
Been a while - hope all is well.
I've used sql query buttons quite a bit and the only thing I can suggest you try is to add the following bit of code in your script, right at the very end (between the concluding 'begin' and 'end') - and obviously changing the form and tablegrid names to suit your application.

begin
  Form1.TableGrid1.dbPopupMenu.Items[0].Enabled := False;   
end.

If that doesn't work, can you attach your project (or a cut down version if it's business sensitive) and I'll try and have a look.
Regards,
Derek.

Re: How to edit a record when using SQL Query on a button

Hello Derek,
nice to hear from you. All is well here and I hope same for you. Its the middle of the night for you so you must really enjoy programming. I am working on a school project with my son so it is attached here but it is 16Mb zipped (remove image so file is small enough now). I did remove the image and resend if you it can't be sent as it is. Once you run it, please click the "Quick Select" button and then the "Medical Issues" button. I would like to be able to have the edit form appear and edit the "Medical issues" field. You will see that the other fields on the edit form are read only. Created in version 4. Hope it all makes sense. Thanks again and nice hearing from you.
Regards,
David

Post's attachments

Attachment icon AnimalCollection.zip 353.57 kb, 393 downloads since 2017-12-02 

Re: How to edit a record when using SQL Query on a button

Hello again Derek, I also had to remove the SQLite.db file as it was 6Mb and the system wouldn't accept that size file so sorry but you will need to enter a record or two and put some text in the "Medical Issue" field of a record.
Thanks, David.

Re: How to edit a record when using SQL Query on a button

In order to edit a record, you must select the id, add the id in your select and it will be fine. There is a post about it somewhere explaining how to hide the id column in the grid.

Re: How to edit a record when using SQL Query on a button

Hello tcoton,
thank you. I just found http://myvisualdatabase.com/forum/viewtopic.php?id=362 which must be the one you mean. It works. Only thing is that a field I change in the record is one that appears on the Tablegrid and once I save the record. The updated field entry does not appear on the Tablegrid. I need to press the SQL Query button again to make the grid show the updated data. I tried using dbUpdate on both on closing the form and after click on the save button but neither worked.
Thanks again for your help.
Regards, David

7 (edited by derek 2017-12-02 02:08:28)

Re: How to edit a record when using SQL Query on a button

Hi David, Thierry,
Please find attached what I think / hope is a fixed version of your project.
From what I could tell,
1.  it didn't (for some reason or other) like formquickselect.tablegrid1 so I just deleted it and recreated it and it was okay.
2.  as Thierry pointed out, the sqlquery needs to have the record id in the 'selection', even if you then do not display it in the grid (to hide it, at the end of the sqlquery column headings, use 'delete_col').
3.  the memo field in formmedical was set to readonly.
And I think that was all that needed changing.
My earlier suggestion of adding Form1.TableGrid1.dbPopupMenu.Items[0].Enabled := False;  to the script wasn't necessary after all.
You could have also defined formquickselect.tablegrid1 in the usual way (with select columns etc) so that when formquickselect is first called, you see ALL records (rather than a blank screen as it currently is which can be a bit disconcerting).  At that point, you then click on your 3 buttons to perform your various sqlquery calls to filter the data.
Hope this moves you on.
Regards,
Derek.

Post's attachments

Attachment icon radsoft.zip 366.75 kb, 447 downloads since 2017-12-02