Editing a Project, Person or To-Do Name

So far we have looked at creating records in and retrieving and deleting records from the database. We haven't looked at how to update records yet, however, so let's take a look at that now.

Edit ProjectsPeople Data Grid Script

Right-click on the left column Data Grid (named "ProjectsPeople") and choose Edit Script.

Locate the CloseFieldEditor Handler

You should have a CloseFieldEditor handler in the script that looks like this. When the user double-clicks on a project or person name a field is opened that allows the user to edit the text. CloseFieldEditor is a message that is sent to the Data Grid when the user presses the enter or return key and the content has changed.

Update the CloseFieldEditor Script

Replace the CloseFieldEditor command in the ProjectsPeople Data Grid group script with the following RevTalk code.

----------

Copy & Paste The Following Code

----------

on CloseFieldEditor pFieldEditor

put the dgHilitedIndex of me into theIndex

switch GetDataOfIndex(theIndex, "@table")

case "projects"

## Get current values for record from database

sqlrecord_find "projects", the uSelectedProjectID of me, theRecordA

put the result into theError

if theError is empty then

## Set 'name' property

sqlrecord_set theRecordA, "name", the text of pFieldEditor

## Update record in the database

sqlrecord_update theRecordA

put the result into theError  

end if

break

case "people"

sqlrecord_find "people", the uSelectedPersonID of me, theRecordA

put the result into theError

if theError is empty then

sqlrecord_set theRecordA, "name", the text of pFieldEditor

sqlrecord_update theRecordA

put the result into theError

end if

break

end switch

if theError is empty then

pass CloseFieldEditor

else

answer "Error saving name:" && theError & "."

end if

end CloseFieldEditor

Updating Data in the Database

Here is what the completed handler will look like. This example introduces the sqlrecord_find command (1). This command searches a database table and returns a SQL Record object. This is how we get all of the fields from the database for a project or person before upating the name. After locating the record you can update the name (2) and call sqlrecord_update (3).

Test the UI

Double-click on New Project and edit the text. Press the enter or return key to save the changes.

Verify in the Database

If you look at the records in the projects table you will see that the name has been updated.

Edit the ToDo Data Grid Script

Now let's update the code in the ToDo Data Grid. Select the ToDo Data Grid and edit the script.

Update Data Grid Group Script

Replace the CloseFieldEditor command in the ToDo Data Grid group script with the following RevTalk.

----------

Copy & Paste The Following Code

----------

on CloseFieldEditor pFieldEditor

## Get current values for record from database

sqlrecord_find "todo_items", the uSelectedID of me, theRecordA

put the result into theError

if theError is empty then

## Set 'name' property

sqlrecord_set theRecordA, "name", the text of pFieldEditor

## Update record in the database

sqlrecord_update theRecordA

put the result into theError  

end if

pass CloseFieldEditor

end CloseFieldEditor

Test the UI

Double-click on New Task and update the name. Press the return or enter key to save the changes.

Verify in the Database

The to-do record in the databae will be updated.

0 Comments

Add your comment

E-Mail me when someone replies to this comment