Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

VB ADO: Getting Identity Field after Addnew

Oh Kay...

I have a table on DB2 with 2 fields ID and Name. ID field in an Identity
field. When I add a new record to the Table (via ADO) I need to be able
to get the ID of the record I just added. The code below is and example
of how I can do it in SQL Server. UDB returns 0 for the lID? Can anyone
tell me how I can get the ID. If I do a .Requery I a can get it by moving
to the last record. But that is only if it is sorted by ID.

Dim rsRec as Recordset
Dim lID as long
Dim sSQL as String
Dim db2 as Connection

sSQl = "Select * From NameTable Order by Name"
rsRec.Open sSQL,db2,adOpenDynamic,adLockOptimistic

rsRec.AddNew
rsRec![Name] = "Steve"
rsRec.Update

lID = rsRec![ID]
[773 byte] By [Frost] at [2007-11-9 18:52:08]
# 1 Re: VB ADO: Getting Identity Field after Addnew
Are you using the DB2 provider? I'm not sure if I will even work then. Alot
of the ADO databoundish techniques don't seem to work with DB2.
See my discussion with Kevin on ADO and DB2 - http://news.dev-archive.com/cgi-bin/dnewsweb.exe?cmd=article&group=database.db2.general&item=249&utag=

"Frost" <stevef@pedco.com> wrote:
>
>Oh Kay...
>
>I have a table on DB2 with 2 fields ID and Name. ID field in an Identity
>field. When I add a new record to the Table (via ADO) I need to be able
>to get the ID of the record I just added. The code below is and example
>of how I can do it in SQL Server. UDB returns 0 for the lID? Can anyone
>tell me how I can get the ID. If I do a .Requery I a can get it by moving
>to the last record. But that is only if it is sorted by ID.
>
>Dim rsRec as Recordset
>Dim lID as long
>Dim sSQL as String
>Dim db2 as Connection
>
>sSQl = "Select * From NameTable Order by Name"
>rsRec.Open sSQL,db2,adOpenDynamic,adLockOptimistic
>
>rsRec.AddNew
> rsRec![Name] = "Steve"
>rsRec.Update
>
>lID = rsRec![ID]
>
>
>
MarkN at 2007-11-11 23:59:52 >
# 2 Re: VB ADO: Getting Identity Field after Addnew
Frost I will be doing some heavy Stored Proc stuff later this week.
And I have figured out how to return data, although only string data. Requery
is really a backward way, does UDB have an equivalent to the @@IDENTITY field
in SQL. I'll check at work tomorrow if I have time, tomorrows a SQL Server
day. (I support UDB and SQL, mostly on NT)

KlK, MCSE
klk@knudsons.com

"MarkN" <mnuttall@nospam.com> wrote:
>
>Are you using the DB2 provider? I'm not sure if I will even work then.
Alot
>of the ADO databoundish techniques don't seem to work with DB2.
>See my discussion with Kevin on ADO and DB2 - http://news.dev-archive.com/cgi-bin/dnewsweb.exe?cmd=article&group=database.db2.general&item=249&utag=
>
>
>"Frost" <stevef@pedco.com> wrote:
>>
>>Oh Kay...
>>
>>I have a table on DB2 with 2 fields ID and Name. ID field in an Identity
>>field. When I add a new record to the Table (via ADO) I need to be able
>>to get the ID of the record I just added. The code below is and example
>>of how I can do it in SQL Server. UDB returns 0 for the lID? Can anyone
>>tell me how I can get the ID. If I do a .Requery I a can get it by moving
>>to the last record. But that is only if it is sorted by ID.
>>
>>Dim rsRec as Recordset
>>Dim lID as long
>>Dim sSQL as String
>>Dim db2 as Connection
>>
>>sSQl = "Select * From NameTable Order by Name"
>>rsRec.Open sSQL,db2,adOpenDynamic,adLockOptimistic
>>
>>rsRec.AddNew
>> rsRec![Name] = "Steve"
>>rsRec.Update
>>
>>lID = rsRec![ID]
>>
>>
>>
>
kevin knudson at 2007-11-12 0:00:45 >
# 3 Re: VB ADO: Getting Identity Field after Addnew
Hi Frost,

You will have to set the dynamic property of ADO recordset as follows

rsRec.Properties("Update Resync") = adResyncInsert

Then when you insert, your rs will be automatically updated with the inserted
Identity value. Then your coding will work

M Tanveer

"kevin knudson" <klk@knudsons.com> wrote:
>
>Frost I will be doing some heavy Stored Proc stuff later this week.
>And I have figured out how to return data, although only string data. Requery
>is really a backward way, does UDB have an equivalent to the @@IDENTITY
field
>in SQL. I'll check at work tomorrow if I have time, tomorrows a SQL Server
>day. (I support UDB and SQL, mostly on NT)
>
>KlK, MCSE
>klk@knudsons.com
>
>"MarkN" <mnuttall@nospam.com> wrote:
>>
>>Are you using the DB2 provider? I'm not sure if I will even work then.
> Alot
>>of the ADO databoundish techniques don't seem to work with DB2.
>>See my discussion with Kevin on ADO and DB2 - http://news.dev-archive.com/cgi-bin/dnewsweb.exe?cmd=article&group=database.db2.general&item=249&utag=
>>
>>
>>"Frost" <stevef@pedco.com> wrote:
>>>
>>>Oh Kay...
>>>
>>>I have a table on DB2 with 2 fields ID and Name. ID field in an Identity
>>>field. When I add a new record to the Table (via ADO) I need to be able
>>>to get the ID of the record I just added. The code below is and example
>>>of how I can do it in SQL Server. UDB returns 0 for the lID? Can anyone
>>>tell me how I can get the ID. If I do a .Requery I a can get it by moving
>>>to the last record. But that is only if it is sorted by ID.
>>>
>>>Dim rsRec as Recordset
>>>Dim lID as long
>>>Dim sSQL as String
>>>Dim db2 as Connection
>>>
>>>sSQl = "Select * From NameTable Order by Name"
>>>rsRec.Open sSQL,db2,adOpenDynamic,adLockOptimistic
>>>
>>>rsRec.AddNew
>>> rsRec![Name] = "Steve"
>>>rsRec.Update
>>>
>>>lID = rsRec![ID]
>>>
>>>
>>>
>>
>
M Tanveer at 2007-11-12 0:01:47 >
# 4 Re: VB ADO: Getting Identity Field after Addnew
It does - IDENTITY_VAL_LOCAL(). Here is a link. http://www-4.ibm.com/cgi-bin/db2www/data/db2/udb/winos2unix/support/document.d2w/report?fn=db2v7irdb2ir19.htm

I'm not sure how well getting this (or @@IDENTITY) works. It returns the
most recently assigned value. You could get some elses. I remember this
being an issue with SQL Server. If this is the case, the only safe way is
to retrieve the record by the application/candidate/logical key(what really
makes the record unique) after insert.

"kevin knudson" <klk@knudsons.com> wrote:
>
>Frost I will be doing some heavy Stored Proc stuff later this week.
>And I have figured out how to return data, although only string data. Requery
>is really a backward way, does UDB have an equivalent to the @@IDENTITY
field
>in SQL. I'll check at work tomorrow if I have time, tomorrows a SQL Server
>day. (I support UDB and SQL, mostly on NT)
>
>KlK, MCSE
>klk@knudsons.com
>
>"MarkN" <mnuttall@nospam.com> wrote:
>>
>>Are you using the DB2 provider? I'm not sure if I will even work then.
> Alot
>>of the ADO databoundish techniques don't seem to work with DB2.
>>See my discussion with Kevin on ADO and DB2 - http://news.dev-archive.com/cgi-bin/dnewsweb.exe?cmd=article&group=database.db2.general&item=249&utag=
>>
>>
>>"Frost" <stevef@pedco.com> wrote:
>>>
>>>Oh Kay...
>>>
>>>I have a table on DB2 with 2 fields ID and Name. ID field in an Identity
>>>field. When I add a new record to the Table (via ADO) I need to be able
>>>to get the ID of the record I just added. The code below is and example
>>>of how I can do it in SQL Server. UDB returns 0 for the lID? Can anyone
>>>tell me how I can get the ID. If I do a .Requery I a can get it by moving
>>>to the last record. But that is only if it is sorted by ID.
>>>
>>>Dim rsRec as Recordset
>>>Dim lID as long
>>>Dim sSQL as String
>>>Dim db2 as Connection
>>>
>>>sSQl = "Select * From NameTable Order by Name"
>>>rsRec.Open sSQL,db2,adOpenDynamic,adLockOptimistic
>>>
>>>rsRec.AddNew
>>> rsRec![Name] = "Steve"
>>>rsRec.Update
>>>
>>>lID = rsRec![ID]
>>>
>>>
>>>
>>
>
MarkN at 2007-11-12 0:02:47 >
# 5 Re: VB ADO: Getting Identity Field after Addnew
>rsRec.Properties("Update Resync") = adResyncInsert

You've used this with DB2? What version(s) of ADO?
MarkN at 2007-11-12 0:03:58 >
# 6 Re: VB ADO: Getting Identity Field after Addnew
MarkN wrote:

> It does - IDENTITY_VAL_LOCAL(). Here is a link. http://www-4.ibm.com/cgi-bin/db2www/data/db2/udb/winos2unix/support/document.d2w/report?fn=db2v7irdb2ir19.htm
>
> I'm not sure how well getting this (or @@IDENTITY) works. It returns the
> most recently assigned value. You could get some elses. I remember this
> being an issue with SQL Server. If this is the case, the only safe way is
> to retrieve the record by the application/candidate/logical key(what really
> makes the record unique) after insert.

Initially, the IDENTITY_VAL_LOCAL only worked within the same transaction, ie before you commit, in which case you should get your own value.
As of v7 fixpack 4, you can use the function after committing the transaction. I don't know whether you would get someone else's value -- I would have thought the
"LOCAL" part of IDENTITY_VAL_LOCAL implies it's local to your connection. Anyone from IBM comment?

--greg

>
>
> "kevin knudson" <klk@knudsons.com> wrote:
> >
> >Frost I will be doing some heavy Stored Proc stuff later this week.
> >And I have figured out how to return data, although only string data. Requery
> >is really a backward way, does UDB have an equivalent to the @@IDENTITY
> field
> >in SQL. I'll check at work tomorrow if I have time, tomorrows a SQL Server
> >day. (I support UDB and SQL, mostly on NT)
> >
> >KlK, MCSE
> >klk@knudsons.com
> >
> >"MarkN" <mnuttall@nospam.com> wrote:
> >>
> >>Are you using the DB2 provider? I'm not sure if I will even work then.
> > Alot
> >>of the ADO databoundish techniques don't seem to work with DB2.
> >>See my discussion with Kevin on ADO and DB2 - http://news.dev-archive.com/cgi-bin/dnewsweb.exe?cmd=article&group=database.db2.general&item=249&utag=
> >>
> >>
> >>"Frost" <stevef@pedco.com> wrote:
> >>>
> >>>Oh Kay...
> >>>
> >>>I have a table on DB2 with 2 fields ID and Name. ID field in an Identity
> >>>field. When I add a new record to the Table (via ADO) I need to be able
> >>>to get the ID of the record I just added. The code below is and example
> >>>of how I can do it in SQL Server. UDB returns 0 for the lID? Can anyone
> >>>tell me how I can get the ID. If I do a .Requery I a can get it by moving
> >>>to the last record. But that is only if it is sorted by ID.
> >>>
> >>>Dim rsRec as Recordset
> >>>Dim lID as long
> >>>Dim sSQL as String
> >>>Dim db2 as Connection
> >>>
> >>>sSQl = "Select * From NameTable Order by Name"
> >>>rsRec.Open sSQL,db2,adOpenDynamic,adLockOptimistic
> >>>
> >>>rsRec.AddNew
> >>> rsRec![Name] = "Steve"
> >>>rsRec.Update
> >>>
> >>>lID = rsRec![ID]
> >>>
> >>>
> >>>
> >>
> >
Greg Nash at 2007-11-12 0:04:57 >
# 7 Re: VB ADO: Getting Identity Field after Addnew
You can use the code you have, but when you open the ado Recorset use this
line instead:

rsRec.Open sSQL,db2,adOpenKeyset,adLockOptimistic

adOpenKeyset will allow you to get the Identity of the record you just entered.

"Frost" <stevef@pedco.com> wrote:
>
>Oh Kay...
>
>I have a table on DB2 with 2 fields ID and Name. ID field in an Identity
>field. When I add a new record to the Table (via ADO) I need to be able
>to get the ID of the record I just added. The code below is and example
>of how I can do it in SQL Server. UDB returns 0 for the lID? Can anyone
>tell me how I can get the ID. If I do a .Requery I a can get it by moving
>to the last record. But that is only if it is sorted by ID.
>
>Dim rsRec as Recordset
>Dim lID as long
>Dim sSQL as String
>Dim db2 as Connection
>
>sSQl = "Select * From NameTable Order by Name"
>rsRec.Open sSQL,db2,adOpenDynamic,adLockOptimistic
>
>rsRec.AddNew
> rsRec![Name] = "Steve"
>rsRec.Update
>
>lID = rsRec![ID]
>
>
>
Chad at 2007-11-12 0:05:58 >