Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsVB SyntaxEnterprise DevelopmentDatabase AccessControlsCOMWin APICrystal ReportDeploymentGeneralGeneral 2
Related Topics
VB.NET / ASP.NETMS SQL ServerMS AccessOther Database ProductsMore Topics ...

VB Forum / Database Access / October 2007



Tip: Looking for answers? Try searching our database.

vb6.0 ADO to SQL 2k

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
WB - 19 Oct 2007 22:37 GMT
I have a command in a vb method that saves data to an sql 2k database as
follows.
   Dim rs As ADODB.Recordset
   Dim strSQL As String

   Set rs = New ADODB.Recordset

   strSQL = "SELECT * " & _
            "FROM tblAppointment a " & _
            "WHERE a.Id = " & mlngId & " AND " & _
                  "a.StoreId = " & mintStoreId

   rs.Open strSQL, gcnSEdata, , adLockPessimistic

   If mflgNew Then rs.AddNew

   With rs
       .Fields("StoreId") = mintStoreId
       .Fields("StylistId") = mlngStylistId
       .Fields("CustomerId") = mlngCustomerId
       .Fields("StartTime") = mintStart
       .Fields("EndTime") = mintEnd
       .Fields("AptDate") = mlngAptDate
       .Fields("PrimarySvc") = mstrPrimarySvc
       '.Fields("AptClassId") = mintAptClass
       .Fields("AptStatusId") = mintAptStatus
       .Fields("AptTypeId") = mintAptType
       .Fields("CreatedBy") = mlngCreatedBy
       .Fields("Confirmed") = mblnConfirmed
       .Fields("CallReminder") = mblnCallReminder
       .Fields("Notes") = mstrNotes

       .Update

       If mflgNew Then mlngId = rs.Fields("Id")
       .Close

   End With

   Exit Sub

The problem is in the last IF statement.  After the update, rsFields("Id")
doesn't return a value.  It should return the auto increment number.  Any
thoughts as to how to make it happen?

WB
Paul Clement - 22 Oct 2007 14:51 GMT
¤ I have a command in a vb method that saves data to an sql 2k database as
¤ follows.
¤     Dim rs As ADODB.Recordset
¤     Dim strSQL As String
¤
¤     Set rs = New ADODB.Recordset
¤
¤     strSQL = "SELECT * " & _
¤              "FROM tblAppointment a " & _
¤              "WHERE a.Id = " & mlngId & " AND " & _
¤                    "a.StoreId = " & mintStoreId
¤
¤     rs.Open strSQL, gcnSEdata, , adLockPessimistic
¤
¤     If mflgNew Then rs.AddNew
¤
¤     With rs
¤         .Fields("StoreId") = mintStoreId
¤         .Fields("StylistId") = mlngStylistId
¤         .Fields("CustomerId") = mlngCustomerId
¤         .Fields("StartTime") = mintStart
¤         .Fields("EndTime") = mintEnd
¤         .Fields("AptDate") = mlngAptDate
¤         .Fields("PrimarySvc") = mstrPrimarySvc
¤         '.Fields("AptClassId") = mintAptClass
¤         .Fields("AptStatusId") = mintAptStatus
¤         .Fields("AptTypeId") = mintAptType
¤         .Fields("CreatedBy") = mlngCreatedBy
¤         .Fields("Confirmed") = mblnConfirmed
¤         .Fields("CallReminder") = mblnCallReminder
¤         .Fields("Notes") = mstrNotes
¤
¤         .Update
¤
¤         If mflgNew Then mlngId = rs.Fields("Id")
¤         .Close
¤
¤     End With
¤
¤     Exit Sub
¤
¤ The problem is in the last IF statement.  After the update, rsFields("Id")
¤ doesn't return a value.  It should return the auto increment number.  Any
¤ thoughts as to how to make it happen?

For SQL Server you will have to execute a follow-up query statement to retrieve the auto increment
value:

SELECT SCOPE_IDENTITY() AS [SCOPE_IDENTITY]

Paul
~~~~
Microsoft MVP (Visual Basic)
wb - 22 Oct 2007 18:11 GMT
I have not used the SCOPE_IDENTITY command before.  Do I simply place the
statement as you have written it after the UPDATE command?

WB

> ¤ I have a command in a vb method that saves data to an sql 2k database as
> ¤ follows.
[quoted text clipped - 52 lines]
> ~~~~
> Microsoft MVP (Visual Basic)
Richard Mueller [MVP] - 22 Oct 2007 23:03 GMT
You retrieve the value of SCOPE_IDENTITY after adding the record to the
table (after the update). See this link:

http://msdn2.microsoft.com/en-us/library/ms190315.aspx

Signature

Richard Mueller
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net
--

>I have not used the SCOPE_IDENTITY command before.  Do I simply place the
>statement as you have written it after the UPDATE command?
[quoted text clipped - 58 lines]
>> ~~~~
>> Microsoft MVP (Visual Basic)
WB - 23 Oct 2007 21:59 GMT
The return value is NULL, and it should be the identity value.  Here is my
code after the rs.update line.

       .Update
       .Close

       If mflgNew Then
           strSQLident = "SELECT SCOPE_IDENTITY() AS [SCOPE_IDENTITY]"
           rs.Open strSQLident, gcnSEdata

           mlngId = rs.Fields("SCOPE_IDENTITY")
           .Close
       End If

> You retrieve the value of SCOPE_IDENTITY after adding the record to the
> table (after the update). See this link:
[quoted text clipped - 63 lines]
> >> ~~~~
> >> Microsoft MVP (Visual Basic)
Paul Clement - 24 Oct 2007 14:41 GMT
¤ The return value is NULL, and it should be the identity value.  Here is my
¤ code after the rs.update line.
¤
¤         .Update
¤         .Close
¤
¤         If mflgNew Then
¤             strSQLident = "SELECT SCOPE_IDENTITY() AS [SCOPE_IDENTITY]"
¤             rs.Open strSQLident, gcnSEdata
¤
¤             mlngId = rs.Fields("SCOPE_IDENTITY")
¤             .Close
¤         End If
¤

See if the below article helps. I believe it applies whether you use SCOPE_IDENTITY or @@IDENTITY:

PRB: ADO Not Returning @@IDENTITY Value After AddNew
http://support.microsoft.com/kb/q195224/

Paul
~~~~
Microsoft MVP (Visual Basic)
Mark J. McGinty - 28 Oct 2007 14:16 GMT
>I have a command in a vb method that saves data to an sql 2k database as
> follows.
[quoted text clipped - 42 lines]
>
> WB

If you open the recordset using a keyset cursor (and assuming the identity
column is part of the primary key) the field will contain its new value
after calling update.  It must be a server side cursor (CursorLocation =
adUseServer, otherwise the CursorType will revert to static.)

Alternatively, a batch cursor will retrieve server-generated values in new
records after UpdateBatch is called.  (This might also depend on the
identity being the PK -- for me, in practice, that is nearly always the case
anyway.)

And regardless of cursor type, calling Requery will retrieve them, though
you'll lose your record pointer, and overhead of the base query is incurred.

Using SCOPE_IDENTITY() or @@IDENTITY, as was suggested in other posts, can
only be done when you construct an UPDATE SQL statement and execute it in a
query batch, that returns rows, it doesn't really help much with calling the
ADO recordest Update method.

Note that in at least some cases, it is clear that ADO internally uses
@@IDENTITY to populate the identity column's field's value, rather than
reading it from the table.  This is a problem if the table being inserted
fires a trigger that inserts a row in secondary table, in which case,
@@IDENTITY returns the identity value of the new row in the secondary table,
and thus the identity value in the field is wrong -- a very disconcerting
little anomaly, when you are left to discover it the hard way!  :-)

-Mark
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.