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.
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