Gunnar Peipman's ASP.NET blog

ASP.NET, C#, SharePoint, SQL Server and general software development topics.

Sponsors

News

 
 
 
 
 
DZone MVB

Links

Social

You must use the dbSeeChanges option with OpenRecordSet when accessing a SQL Server table that has an IDENTITY column

One error you may get when querying SQL Server databases through MS Access is "You must use the dbSeeChanges option with OpenRecordSet when accessing a SQL Server table that has an IDENTITY column". This error appears when you open recordset that contains IDENTITY column. Usually you have to do something like this to get this error (FindTrainer query gets data from SQL Server table).


Public Function GetTrainerId(strTrainerName As String) As Integer
Dim query As QueryDef
Dim rs As Recordset

    Set query = CurrentDb.QueryDefs("FindTrainer")
    query.Parameters("pName") = strTrainerName
    
    Set rs = query.OpenRecordset()
    
    If rs.RecordCount = 0 Then
        rs.AddNew
        rs("Name") = strTrainerName
        rs.Update
        rs.MoveLast
        rs.MoveFirst
    End If
    
    GetTrainerId = rs("ID")
    
    Set rs = Nothing
    Set query = Nothing
    
End Function

To avoid this error you should use dbSeeChanges option when opening the recordset. You can see that I am using two parameters when I open recordset. First one, dbOpenDynaset, sais to Access that I need dynamic recordset and second one, dbSeeChanges, sais that there may be changes that are made in server and we need to retrieve row again after inserting or updating it.


Public Function GetTrainerId(strTrainerName As String) As Integer
Dim query As QueryDef
Dim rs As Recordset

    Set query = CurrentDb.QueryDefs("FindTrainer")
    query.Parameters("pName") = strTrainerName
    
    Set rs = query.OpenRecordset(dbOpenDynaset, dbSeeChanges)
    
    If rs.RecordCount = 0 Then
        rs.AddNew
        rs("Name") = strTrainerName
        rs.Update
        rs.MoveLast
        rs.MoveFirst
    End If
    
    GetTrainerId = rs("ID")
    
    Set rs = Nothing
    Set query = Nothing
    
End Function

After adding these two parameters to recorset opening calls the error disappeared and everything started to work normally.

Comments

Dew Drop - January 15, 2009 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop - January 15, 2009 | Alvin Ashcraft's Morning Dew

# January 15, 2009 10:33 AM

Opening two Tables as a Idenitity error | keyongtech said:

Pingback from  Opening two Tables as a Idenitity error | keyongtech

# May 11, 2009 6:05 PM

Wayne Clements said:

What can I do about doCmd.RunSQL when it demands that I use dbSeeChanges?

# September 2, 2009 9:08 PM

David Holley said:

rs.MoveFirst and rs.MoveLast is not a reliable means of obtaining the ID of the newly created record unless you have an exclusive lock on the underlying table.

# July 8, 2010 4:29 PM

Glint said:

Thanks. It removed a substantial headache from me.

# September 3, 2010 6:00 PM

Bob said:

Thanks, this was exactly what I needed!

# February 17, 2011 6:09 PM

Basit Baig said:

Thank you very much, its really help for me and things are very much smooth.

Please can you help me for this function as well, i want to execyte a Update query through this method, its runs but the result is not as per requirement.

 CurrentDb.Execute(myqry)

what is best option to use this method if we have SQL Server at backend.

# March 9, 2011 4:29 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)