FIX: AutoNumber Field Is Not Incremented When Using ADO (190370)
The information in this article applies to:
- ActiveX Data Objects (ADO) 1.5
- ActiveX Data Objects (ADO) 2.0
- ActiveX Data Objects (ADO) 2.1 SP2
- ActiveX Data Objects (ADO) 2.5
- Microsoft Visual Basic Learning Edition for Windows 6.0
- Microsoft Visual Basic Professional Edition for Windows 6.0
- Microsoft Visual Basic Enterprise Edition for Windows 6.0
- Microsoft Visual C++, 32-bit Enterprise Edition 6.0
- Microsoft Visual C++, 32-bit Professional Edition 6.0
- Microsoft Visual C++, 32-bit Learning Edition 6.0
This article was previously published under Q190370 SYMPTOMS
When using the ODBC, Microsoft Access, or SQL Server OLE DB Providers the
following may appear:
- A "0" displays in the AutoNumber (or Identity) field after adding
records through the DataGrid control bound to an ActiveX Data Objects
(ADO) Data Control
- A "0" is stored in the AutoNumber (or Identity) field after adding
records to a recordset, using the AddNew method of the recordset.
This only occurs when the CursorLocation is set to "3" - adUseClient.
CAUSE
By using the client-side cursors, the OLE DB provider is unable to requery
the server for the updated record, and a "0" appears in place of the
correct value. When you requery the recordset, the correct value appears.
RESOLUTION
Here are two ways to resolve this issue:
- Use Server-side cursors (Set CursorLocation to "2" - adUseServer).
This may incur a greater performance hit, as the client requeries the
server for each record after each insert.
NOTE: This solution is not valid if your provider does not support server-side cursors, for example, the MS REMOTE provider (RDS.)
- Use the Resynch method of the ADO Data Control's underlying recordset or the ADO Data Control's own Refresh method. Call either of these methods
from the DataGrid's AfterUpdate event. Performance may be improved if the Requery is performed after adding a batch of records. If the
recordset is requeried after every insert, performance may be affected.
The method you choose depends upon your design goals. A good rule of thumb
would be for larger recordsets use the first option. For smaller, batch,
or disconnected recordsets, use the second option.
STATUS
This behavior has changed with the Jet OLE DB Provider version 4.0 and Access 2000. With this provider the autonumber field is returned for both the clientside and serverside cursors when using an Access 2000 database. The 3.51 ODBC driver for Access does not return the autonumber field for an Access 2000 database if a clientside cursor is being used.
The Jet 4.0 OLE DB Provider is available with Microsoft Data Access Components (MDAC) 2.1 and 2.5.
Modification Type: | Minor | Last Reviewed: | 3/2/2005 |
---|
Keywords: | kbbug kbDatabase kbfix kbMDACNoSweep KB190370 |
---|
|