thornmastr
Programmer
i am using the recordset.addnew method to add a new record to a disconnected recordset and all works well. However, once the update batch completes, I need to get the autonumber value assigned to the record. The code which both updates the record and gets the identity is below. The value returned by the identity is always 0 which is obviously incorrect. I have heard the following as possible explanations.
1. “select @@identity” is valid only after an INSERT statement. On the face of it, this seems patently absurd.
2. Specific to ACCESS, @@Identity is valid only for A2K and above. Since I am using an A97 database, even though I am using JET 4 SP 7, A97 negates using @@Identity. In my opinion this is as absurd as reason 1 but somehow, looking at M$ it is a distinct possibility.
Given this dilemma is there a relatively simple method to get the value for the last autonumber applied to the distinct last recordset updated. Given the natural constraints of the connection, this should not be so difficult.
Comments, suggestions, criticisms are all welcome.
CODE
Public Sub Update_Residency()
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim sSQL As String, sCon As String
If Key.Text = "" Then
sSQL = "Select * from tblresidences"
Else
sSQL = "Select * from tblresidences where PKResident = " & Key
End If
Set cn = New ADODB.Connection
cn = constr ‘constr is created when program first starts and is the connection string”
cn.Open
Set rs = New ADODB.Recordset
rs.CursorLocation = adUseClient
rs.LockType = adLockBatchOptimistic
rs.Open sSQL, cn
sCon = rs.ActiveConnection
Set rs.ActiveConnection = Nothing
If Key = "" Then
rs.AddNew
End If
rs("fkstudent"
= keyinuse
rs("lastname"
= Me("lastname"
rs("firstname"
= Me("firstname"
If Me("MI"
<> "" Then
rs("MI"
= Me("mi"
End If
rs("street1"
= Me("street1"
rs("street2"
= Me("street2"
rs("city"
= Me("city"
rs("state"
= Me("state"
rs("zipcode"
= Me("zipcode"
rs("homephone"
= Me("homephone"
rs("workphone"
= Me("workphone"
rs("pager"
= Me("pager"
rs("cellphone"
= Me("cellphone"
rs("emailaddress"
= Me("emailaddress"
rs("pager"
= Me("pager"
rs("cellPhone"
= Me("cellphone"
rs.ActiveConnection = sCon
rs.UpdateBatch
rs.Close
rs.Open "select @@Identity", cn, adUseClient, adLockBatchOptimistic
Me.Key.Text = rs(0)
rs.Close
cn.Close
Set rs = Nothing
Set cn = Nothing
End Sub
END OF CODE
Thank you,
Robert Berman
Data Base consultant
Vulcan Software Services
thornmastr@earthlink.net
1. “select @@identity” is valid only after an INSERT statement. On the face of it, this seems patently absurd.
2. Specific to ACCESS, @@Identity is valid only for A2K and above. Since I am using an A97 database, even though I am using JET 4 SP 7, A97 negates using @@Identity. In my opinion this is as absurd as reason 1 but somehow, looking at M$ it is a distinct possibility.
Given this dilemma is there a relatively simple method to get the value for the last autonumber applied to the distinct last recordset updated. Given the natural constraints of the connection, this should not be so difficult.
Comments, suggestions, criticisms are all welcome.
CODE
Public Sub Update_Residency()
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim sSQL As String, sCon As String
If Key.Text = "" Then
sSQL = "Select * from tblresidences"
Else
sSQL = "Select * from tblresidences where PKResident = " & Key
End If
Set cn = New ADODB.Connection
cn = constr ‘constr is created when program first starts and is the connection string”
cn.Open
Set rs = New ADODB.Recordset
rs.CursorLocation = adUseClient
rs.LockType = adLockBatchOptimistic
rs.Open sSQL, cn
sCon = rs.ActiveConnection
Set rs.ActiveConnection = Nothing
If Key = "" Then
rs.AddNew
End If
rs("fkstudent"
rs("lastname"
rs("firstname"
If Me("MI"
rs("MI"
End If
rs("street1"
rs("street2"
rs("city"
rs("state"
rs("zipcode"
rs("homephone"
rs("workphone"
rs("pager"
rs("cellphone"
rs("emailaddress"
rs("pager"
rs("cellPhone"
rs.ActiveConnection = sCon
rs.UpdateBatch
rs.Close
rs.Open "select @@Identity", cn, adUseClient, adLockBatchOptimistic
Me.Key.Text = rs(0)
rs.Close
cn.Close
Set rs = Nothing
Set cn = Nothing
End Sub
END OF CODE
Thank you,
Robert Berman
Data Base consultant
Vulcan Software Services
thornmastr@earthlink.net