Trying to get the Identity right after .Update using SQLOLEDB

sosjames11

New Member
Joined
Jun 23, 2010
Messages
6
I'm just wondering if anyone knows why I have to execute the query twice in the code below to get the Identity?
Example:
Set rs = Cn.Execute("SELECT @@Identity as NewID", , adCmdText)
x = rs.Fields("NewID").Value '''returns null
Set rs = Cn.Execute("SELECT @@Identity as NewID", , adCmdText) '''Must have this twice Don't know why 0_o.
x = rs.Fields("NewID").Value '''returns 6 after the second execute
If anyone knows a better way I'm open to try a few things, the code below does what I want but I don't understand why. I'm also hoping this sample might help someone else.


Code:
Public Const SQLCnStr As String = "Provider=SQLOLEDB; Server=hpcSQL01;Database=MTS;User Id=MTS; Password=MTS"
Sub SaveNCMR()



    Dim Cn As ADODB.Connection
    Dim rs As ADODB.Recordset
    
    Set Cn = New ADODB.Connection
    Set rs = New ADODB.Recordset
    
    Cn.ConnectionString = SQLCnStr
    Cn.Open
    
    NCMRnum = Sheets("NCMR Form").Range("I3")
    
    With rs
        .ActiveConnection = Cn
        .LockType = adLockOptimistic
        .Source = "SELECT * FROM QM_NCMR Where [NCMR Number] = '" & NCMRnum & "'"
        .Open
            If rs.EOF Then
                .AddNew
                .Fields("Created").Value = Now
            End If
            .Fields("Modified").Value = Now
            .Fields("Shop Order").Value = Sheets("NCMR Form").Range("C6")
            .Fields("Department").Value = Sheets("NCMR Form").Range("C8")
            .Fields("Operator").Value = Sheets("NCMR Form").Range("C10")
            .Fields("Process Item").Value = Sheets("NCMR Form").Range("C12")
            .Fields("Raw Lot Num").Value = Sheets("NCMR Form").Range("C14")
            .Fields("Raw Item Num").Value = Sheets("NCMR Form").Range("C16")
            .Fields("Finished Lot Num").Value = Sheets("NCMR Form").Range("C18")
            .Fields("Finished Item Num").Value = Sheets("NCMR Form").Range("C20")
            .Fields("Customer").Value = Sheets("NCMR Form").Range("F8")
            .Fields("Customer Part Num").Value = Sheets("NCMR Form").Range("F10")
            .Fields("Cust Order Num").Value = Sheets("NCMR Form").Range("F12")
            .Fields("Item Description").Value = Sheets("NCMR Form").Range("F14")
            .Fields("Comments").Value = Sheets("NCMR Form").Range("C23")
            .Fields("Defect Code").Value = Sheets("NCMR Form").Range("I8")
            .Fields("Defect Desc").Value = Sheets("NCMR Form").Range("I10")
            .Fields("Qty").Value = Sheets("NCMR Form").Range("I12")
            .Fields("MachineID").Value = Sheets("NCMR Form").Range("I14")
            .Fields("Spool Num").Value = Sheets("NCMR Form").Range("I16")
            .Fields("Cust Due Date").Value = Sheets("NCMR Form").Range("I18")
Go2Update:
            .Update
    End With
    If Sheets("NCMR Form").Range("I3") = "" Then
        Set rs = Cn.Execute("SELECT @@Identity as NewID", , adCmdText) '''Only works the first time the rs is updated.
        Set rs = Cn.Execute("SELECT @@Identity as NewID", , adCmdText) '''Must have this twice Don't know why 0_o.
        Sheets("NCMR Form").Range("I3") = rs.Fields("NewID").Value
    End If
    
    Call SendNCR
    
    rs.Close
    Cn.Close
    
    Set rs = Nothing
    Set Cn = Nothing
End Sub
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top