views:

20

answers:

2
With rs
    .AddNew ' create a new record
    ' add values to each field in the record
    .Fields("datapath") = dpath
    .Fields("analysistime") = atime
    .Fields("reporttime") = rtime
    .Fields("lastcalib") = lcalib
    .Fields("analystname") = aname
    .Fields("reportname") = rname
    .Fields("batchstate") = bstate
    .Fields("instrument") = instrument
    .Update ' stores the new record        
End With

this is how i am adding records. is it possibel to do something lik ethis???:

With rs
    .AddNew ' create a new record
    ' add values to each field in the record
    .Fields("datapath") = dpath
    .Fields("analysistime") = atime
    .Fields("reporttime") = rtime
    .Fields("lastcalib") = lcalib
    .Fields("analystname") = aname
    .Fields("reportname") = rname
    .Fields("batchstate") = bstate
    .Fields("instrument") = instrument
     SCOPE_IDENTITY()  <----------------
    .Update ' stores the new record


End With
+2  A: 

No, there is not.

You should make an explicit INSERT statement followed by a call to SCOPE_IDENTITY in the same batch.

Quassnoi
thank you very much i really appreciate your time
I__
@quassnoi: is what guffa saying correct?
I__
@op: yes, though it's not actually about `SCOPE_IDENTITY`.
Quassnoi
are you sure guffa's answer this is OK in a multiuser environment? if so, then why dont i just use it?
I__
why doesnt this have @ before the values? http://msdn.microsoft.com/en-us/library/aa933206(SQL.80).aspx
I__
@op: you can use it, as long as you open your recordset with `adOpenStatic`. Your question was about `SCOPE_IDENTITY`.
Quassnoi
im sorry what does that mean adopenstatic?
I__
http://stackoverflow.com/questions/3533263/scope-identity-vs-rs-fields
I__
+2  A: 

After you have executed the Update command, the identity will be placed in the corresponding field in the recordset. You can read it from there.

Example:

id = .Fields("id")
Guffa
thank you very much. are you sure this is OK in a multiuser environment?
I__
@I__: Yes, you have an open connection to the table, so the value is read from the actual record rather than by calling a function.
Guffa
the answer below says: you can use it, as long as you open your recordset with adOpenStatic ---how do i know if i an opening it this way?
I__
http://stackoverflow.com/questions/3533263/scope-identity-vs-rs-fields
I__
@I__: The third parameter in the call when you open the recordset is the cursor type, and the adOpenStatic constant has the value 3, so if you use `rs.Open sql, conn, 3` then you are using a static cursor.
Guffa