# Run Update Query after Table Cell Edited

**URL:** <https://forum.inductiveautomation.com/t/run-update-query-after-table-cell-edited/4952>\
**Category:** Ignition\
**Created:** [August 7, 2012, 10:32pm UTC](https://forum.inductiveautomation.com/t/run-update-query-after-table-cell-edited/4952 "2012-08-07T22:32:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jkrugerud](https://avatars.discourse-cdn.com/v4/letter/j/22d042/32.png) [@jkrugerud](https://forum.inductiveautomation.com/u/jkrugerud)\
**Post date:** [August 7, 2012, 10:32pm UTC](https://forum.inductiveautomation.com/t/run-update-query-after-table-cell-edited/4952/1 "2012-08-07T22:32:29Z")

</div>

After editing a cell in a table I would like to run an update query to throw then new value into my database. How do I get the query to run on the enter key?

---

<div class="post-metadata">

**Author:** ![jkrugerud](https://avatars.discourse-cdn.com/v4/letter/j/22d042/32.png) [@jkrugerud](https://forum.inductiveautomation.com/u/jkrugerud)\
**Post date:** [August 7, 2012, 10:46pm UTC](https://forum.inductiveautomation.com/t/run-update-query-after-table-cell-edited/4952/2 "2012-08-07T22:46:51Z")

</div>

I’ve done the following but for some reason it doesn’t see this as being true:

if event.keyCode == event.VK\_ENTER:  
print “Enter Key”  
databaseTable = “[shredder].[dbo].[Shredder\_Grate\_Tracking]”  
tableData = event.source.data  
rowIndex = event.source.selectedRow  
colIndex = event.source.selectedColumn  
columnName = tableData.getColumnName(colIndex)  
id = tableData.getValueAt(rowIndex, “ID”)  
vendor = tableData.getValueAt(rowIndex, “Manufacturer”)  
rowDate = tableData.getValueAt(rowIndex, “Date Installed”)  
value = tableData.getValueAt(rowIndex, colIndex)

```
if columnName == "Cost":
	query = "UPDATE [shredder].[dbo].[Shredder_Grate_Tracking] SET COST = '%f' WHERE ID = '%i' AND VENDOR = '%s'" %(value, id, vendor)
	system.db.runUpdateQuery(query,"Shredder")
```

---

<div class="post-metadata">

**Author:** ![jkrugerud](https://avatars.discourse-cdn.com/v4/letter/j/22d042/32.png) [@jkrugerud](https://forum.inductiveautomation.com/u/jkrugerud)\
**Post date:** [August 7, 2012, 10:56pm UTC](https://forum.inductiveautomation.com/t/run-update-query-after-table-cell-edited/4952/3 "2012-08-07T22:56:18Z")

</div>

I figured it out, I put this event on the keyPressed which didn’t work so I used the keyReleased and it works good.

---

<div class="post-metadata">

**Author:** ![James.Lorenz](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/james.lorenz/32/3290_2.png) [@James.Lorenz](https://forum.inductiveautomation.com/u/James.Lorenz)\
**Post date:** [August 7, 2012, 11:08pm UTC](https://forum.inductiveautomation.com/t/run-update-query-after-table-cell-edited/4952/4 "2012-08-07T23:08:19Z")

</div>

There is an event handler for the table component called cellEdited, you don’t have to worry about trying to capture that event yourself.

You can use event.row and event.column to get both the primary key, and the table column where the value will be updated. You can then run a prepUpdate to get the new value in the database:

`columnName = event.source.data.getColumnName(event.column)
primaryKeyValue = event.source.data.getValueAt(event.row, "id")
query = "UPDATE some_table SET %s=? WHERE id=?" % columnName
system.db.runPrepUpdate(query, [event.newValue, primaryKeyValue])`

EDIT: I see you figured out a different way, nice job.

---

<div class="post-metadata">

**Author:** ![jkrugerud](https://avatars.discourse-cdn.com/v4/letter/j/22d042/32.png) [@jkrugerud](https://forum.inductiveautomation.com/u/jkrugerud)\
**Post date:** [August 7, 2012, 11:31pm UTC](https://forum.inductiveautomation.com/t/run-update-query-after-table-cell-edited/4952/5 "2012-08-07T23:31:34Z")

</div>

Oh ya, I like the cellEdit better, I found that VK\_TAB didn’t work with either keyPressed or keyRelease. Thanks
