Help with DataGrid Control
Posted on 2002-06-23
I am using VB6, MDAC 2.5, and my project references Microsoft ActiveX Data Objects 2.0 Library. (I get errors with 2.5)
I am setting up some forms which I would like to look like Access tables or Excel spreadsheets to display data from databases. I am therefore using DataGrid controls which use ADO Data controls as their data source to get this look. I am having a lot of trouble with both these controls however so if anyone can give me a source which discusses how to properly program these controls, or a source of sample code, I would greatly appreciate it.
The question I would really like to be answered here though is this. When a user enters a value in a cell in the DataGrid, I would like to validate its data type myself before the DataGrid control does it, so that I can return my own error message and cancel the recordset update if the value entered is not compatible with the field type in which it is entered. I prefer to return my own error messages rather than the cryptic error messages that the DataGrid control returns. The problem is I cannot access the value entered in the cell by the user in any of the events that are triggered by the user entering a value. For example, in the event
Private Sub DataGrid1_BeforeColUpdate(ByVal ColIndex As Integer, OldValue As Variant, Cancel As Integer)
the values OldValue, DataGrid1.Columns(ColIndex).Text, and DataGrid1.Columns(ColIndex).Value all contain the same value, the value that is being replaced with the new value the user entered. They are all the old value. I cannot get the new value before Cell update is complete.
Interestingly enough, this is not true if I programmatically add values to cells in a newly added row, like in a paste operation I implemented. The paste function just uses
DataGrid1.Columns(i).Value = strValue
to enter the values into the cells and the new cell values are validated in event DataGrid1_BeforeUpdate(). If any of the values cannot be converted to the type of the field, a custom message is posted, the update is canceled, and the user can then fix it. The event, and therefore the validation for the row, will continue to fire each time the user tries to leave the row until all the bad values are fixed. I would just like to do this for pre-existing rows and cells.
So please tell me, HOW CAN I ACCESS THE NEW VALUE THE USER ENTERED BEFORE THE UPDATE EVENT IS COMPLETE??