Question : Having an @@Identity crisis with ADO and doing an update to SQL from Access 2010

Normally, with a stored procedure, I can return @@IDENTITY. Since I was unable to figure out how to pass binary data to a stored procedure, I am using ADO and doing a direct update.

I cannot figure out how to get @@identity / SCOPE_IDENTITY from the updated record.

If you look at the sql, it is basically SELECT where FALSE (no records). When I do the update, the value of the id field is null. If I .Requery, the results will eliminate the newly added record.

I don't want to do a select MAX(ID), as there is no guarantee (though it is highly likely) that the last record is the correct one.

If it were some semi-unique data, such as customer name, I could certainly select max(id)  where lastname = x and firstname = y. And I may end up having to do that, but I would think there has to be a way to get the ID of the newly added record via ADO.

If I use .AbsolutePosition to bookmark it, I would still not be able to reset it to that record after the .Requery, since there would still be no results.

There are numerous workarounds I can think of (getting the MAX(ID) and changing the where from 1 = 0 to > lastidused), but I am not really looking for a workaround. I am wondering if I missed something with getting the ID of a newly added record.

I would also like to know how to pass binary to a stored procedure from MS Access.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
Function AddImage(ByVal FileName As String) As String

   Dim rs As ADODB.Recordset
   Dim conn As ADODB.Connection
   Dim stm As ADODB.Stream
   Dim result As Integer
   
   Set rs = New ADODB.Recordset
   Set conn = New ADODB.Connection
   With conn
      .ConnectionString = MASTER_ODBC_CONNECTION_STRING
      .Open
      With rs
         .Open "SELECT * FROM PictureTest WHERE 1 = 0", conn, adOpenDynamic, adLockOptimistic
         Set stm = New ADODB.Stream
         stm.Type = adTypeBinary
         stm.Open
         stm.LoadFromFile FileName
      
         ' Insert the binary object into the table.
         .AddNew
         .Fields("PictureData").value = stm.Read
         .Update
         result = .Fields("ID").value
         stm.Close
         Set stm = Nothing
         
         .Close
      End With
      Set rs = Nothing
      .Close
   End With
   Set conn = Nothing
End Function

Answer : Having an @@Identity crisis with ADO and doing an update to SQL from Access 2010

You should be able to do this AFTER your insert:

Dim rsID As ADODB.Recordset

Set rsID = New ADODB.Recordset

rsID.Open "SELECT @@IDENTITY As NewID", conn

Msgbox rsID("NewID")

Obviously do this before closing your ADO connection.
Random Solutions  
 
programming4us programming4us