Quote:
Originally posted by maqmaq
How can I make the cell of a dbgrid active and ready for the input by a user when addnew record button is clicked, when addnew activated new row will be added but the cursor will not be ready unless user click on this cell, I tried setfocus but it did not work, Any idea will be appriciated
Thanks
First: why not just set the properties in the dbGrid so that Allow Add New = TRUE? But if you want an "ADD NEW" button then highlighting the new record depends somewhat on how you add the new record. If you are using an SQL INSERT command and you have an auto-number key field and the new record will be included in your Data object's recrodset as the last record then: after the SQL statement is executed refresh your data object then use DATA1.Recordset.MoveLast method. If you are using Data1.AddNew, then set default values(if you do not have default values set in the Table Properties of the Database) and use Data1.Update. Then use Data1.Recordset.MoveLast. Remember that the dbGrid control is a reflection of the Data Control. What I would suggest is creating a frame with controls corresponding to the fields that you want the user to enter and making the frame visible in the click event of the "ADD NEW" command button. Force navigation through the fields by trapping for arrows and <ENTER> keys. Add command buttons to the frame that "ADD NOW" "CANCEL" and "ADD NOW ENTER ANOTHER"....The advantage here is that you can add code to validate the data before it enters the database. You can, of course, enter this same error checking into the Data control's corresponding events. Or a best answer solution would be to encapsulate the error checking into a module or class and have the events that might trigger an error check all of the data in question. The advantage to a dbGrid is that it is a very quick and easy way to display, edit and add data. The downside is that it is very hard to wrestle control from. Depending on the scale of your app, most of the time if you want control you need to take control through simple objects and preclude all possible user mistakes. Use of interfaces with error checking help keep data the way it needs to be.