Thanks for the suggestion.
I'm finding that addnew event is only called once -- before the user types.
The dataGrid.addNewMode is indeed dbgAddNewCurrent. But it never gets
called again with Mode= dbgAddNewPending. I have been able to catch this
value in places like recordset_willChangeRecord, but I can't seem to update
the recordset there. I get an error message about re-entrant events.
[quick experiment]
OK, I've tried the same thing with client side cursor (static cursor). Now
I can update the recordset within the recordset_WillChangeRecord event. So,
I seem to now have the solution:
Delcare recordset with "With Events" (or use a recordset control). Create a
recordset_WillChangeRecord handler, that checks for "adReason =
adRsnUpdate". Then, if precordset.EditMode = adEditAdd, set the hidden
field in the recordset. I still have a problem (unrelated I think) when the
user clicks of the datagrid to somewhere else on the form, which leaves the
recordset with changes pending. I think I can solve that with a LostFocus
event handler though. Or, in code:
Dim WithEvents rs As ADODB.Recordset
Private Sub rs_WillChangeRecord(ByVal adReason As ADODB.EventReasonEnum,
ByVal cRecords As Long, adStatus As ADODB.EventStatusEnum, ByVal pRecordset
As ADODB.Recordset)
' Handle case where user has inserted a new row into the grid. Must fill in
the
' HiddenField before the table can be updated.
If adReason = adRsnUpdate Then ' Only care when recordset is about to
update:
' stuff in the hidden field now:
If pRecordset.EditMode = adEditAdd Then pRecordset!HiddenField =
hiddenvalue
Else
adStatus = adStatusUnwantedEvent ' Don't bother me with this one
again.
End If
End Sub
I was sure I had a good reason for using a server side cursor. If this is
the case, can anyone verify/explain why this would not work with a server
side cursor (keyset type)?
Thanks again to all of those who responded (and those who have not yet
responded!)
Quote:
> When you do the addnew event, check the add new mode. If it's current,
the
> user hasn't typed anything, if it's pending then they have. Try only
> putting the information in the row after the the mode becomes pending.
I
> haven't had any problems getting the information into the row. It's
getting
> it to update right away that I'm having problems.
> Suzette
> > I've been hassling with this DataGrid (V6 sp5) problem for days now!
> There
> > must be a better way.
> > I have a grid which allows users to insert new rows into a SQL Server
> table.
> > The table actually has the visible fields in the grid, as well as a
field
> > which must be given a value programmatically. I've tried various
> > combinations if sticking the field in a hidden column in the grid, and
> > deleting the column from the grid altogether (but still in the
recordset).
> > I have tried using the grid's OnAddNew event to stuff the value into the
> > hidden field (tried both stuffing it into the hidden column, or directly
> > into the recordset). Either way I do this, the recordset decides that
it
> > has been changed., and wants to update if the user moves off of it.
> (which
> > is not appropriate when the user didn't type anything).
> > I have not been able to figure out an event hook to prevent this. There
> are
> > scads of events fired from the DataGrid, but none of them seem to be in
> the
> > right place at the right time to do something like .CancelUpdate.
> > Can someone please suggest a way to do this? Am I asking too much from
> the
> > DataGrid control?
> > Thanks very much for any guidance here.