shayne23d
asked on
getting ident_current into a vb variable
I have an insert statement that I have run through adodb.recordset.
now I am trying to get the primary key out of the database using Scope_identity() function .
I do not know how to call the value of it to vb.
I thought I could do selct statement
Select IDENT_CURRENT('orders')
intOrderID = rs.Fields("IDENT_CURRENT") .Value
but does not seem to work? any ideas?
now I am trying to get the primary key out of the database using Scope_identity() function .
I do not know how to call the value of it to vb.
I thought I could do selct statement
Select IDENT_CURRENT('orders')
intOrderID = rs.Fields("IDENT_CURRENT")
but does not seem to work? any ideas?
Of course, if you're working in VB .Net, you will need all the .Value properties since .Net doesn't support default collections/properties.
ASKER
here is the call that I am using for the database.
Dim cn As ADODB.Connection
Dim cmd As ADODB.Command
Set cn = New ADODB.Connection
cn.CONNECTIONSTRING = CONNECTIONSTRING
cn.Open
Set cmd = New ADODB.Command
cmd.ActiveConnection = cn
cmd.CommandText = strSqlInsert
cmd.CommandType = adCmdText
Set rs = New ADODB.Recordset
rs.Open cmd, , adOpenKeyset, adLockOptimistic
if I then call variable = rs("Order_ID").value I get an error
Dim cn As ADODB.Connection
Dim cmd As ADODB.Command
Set cn = New ADODB.Connection
cn.CONNECTIONSTRING = CONNECTIONSTRING
cn.Open
Set cmd = New ADODB.Command
cmd.ActiveConnection = cn
cmd.CommandText = strSqlInsert
cmd.CommandType = adCmdText
Set rs = New ADODB.Recordset
rs.Open cmd, , adOpenKeyset, adLockOptimistic
if I then call variable = rs("Order_ID").value I get an error
Oh, I see what you're asking now. So your strSqlInsert is something like
"INSERT INTO myTable .... "
and so you want to run it and find out the value of the last ID generated by that INSERT command.
I thought you were updating the recordset directly as described above.
I have two ideas:
1. You could immediate open another recordset by EXECUTEing a COMMAND against your connection with command text = "SELECT IDENT_CURRENT('TableName') " then read the value of NewRS(0).
2. You could do something like DMax("IDFieldName","TableN ame") which will retrieve the maximum value in the field.
Either of these methods might NOT return what you expect in a high-volume environment: what happens if someone else's changes append the instant between yours and you call to get the last ID? You'll get someone else's last inserted record id.
Without changing to the method I referred to earlier, where you retrieve your new records then insert them into the new recordset one at a time, I'm not sure you to get the lastest primary key generated by your specific process with absolute certainty.
"INSERT INTO myTable .... "
and so you want to run it and find out the value of the last ID generated by that INSERT command.
I thought you were updating the recordset directly as described above.
I have two ideas:
1. You could immediate open another recordset by EXECUTEing a COMMAND against your connection with command text = "SELECT IDENT_CURRENT('TableName')
2. You could do something like DMax("IDFieldName","TableN
Either of these methods might NOT return what you expect in a high-volume environment: what happens if someone else's changes append the instant between yours and you call to get the last ID? You'll get someone else's last inserted record id.
Without changing to the method I referred to earlier, where you retrieve your new records then insert them into the new recordset one at a time, I'm not sure you to get the lastest primary key generated by your specific process with absolute certainty.
My suggestion 1 above assumes you're working against a MS SQL server: IDENT_CURRENT is a SQL-specific funciton.
ASKER
I think I have come up with a solution
strSqlInsert = "DECLARE @id as int insert into orders (numb_Pages,print_Type, page_Layout, bleed_Option, paper_type, proof, ins, amount, doc_ID, customer_ID) Values('" & intNumbPages & "','" & strPrintType & "', '" & strPageLayout & "', '" & blnBleedOption & "', '" & strPaperType & "' , '" & strProof & "', '" & strInsert & "', '" & intAmount & "', '" & intDocID & "', '" & strCustID & "'" & ") select * from orders where Order_ID = @id"
let me know if you think that will work.
after getting your feedback i will accept it as the answer
thanks
shayne
strSqlInsert = "DECLARE @id as int insert into orders (numb_Pages,print_Type, page_Layout, bleed_Option, paper_type, proof, ins, amount, doc_ID, customer_ID) Values('" & intNumbPages & "','" & strPrintType & "', '" & strPageLayout & "', '" & blnBleedOption & "', '" & strPaperType & "' , '" & strProof & "', '" & strInsert & "', '" & intAmount & "', '" & intDocID & "', '" & strCustID & "'" & ") select * from orders where Order_ID = @id"
let me know if you think that will work.
after getting your feedback i will accept it as the answer
thanks
shayne
ASKER
I mean I will accept your comments as the answer and award the points to you
shayne
shayne
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Very nice. Great suggestion. I have increased the point value for the added help
shayne
shayne
Thanks! Feel free to add comments here if you need anything more with this issue.
rs.AddNew
rs("field1") = "SomeValue"
rs("field2") = "SomeOtherValue"
...
rs.Update
SomeVariable = rs("PrimaryKeyField")
Thus, there is no need to try to call directly to SQL to get the ID of the just-added record.