Home   |   Asp.Net 2.0   |   .Net Framework 2.0   |   IIS 6.0   |   Sql Server 2005   |   Visual Basic 2005   |   c# 2005   |   VS 2005   |   Visual Source Safe 2005

MS Dynamics CRM 3.0

SharePoint Portal Server 2003
SharePoint Server 2007
Dynamics NAV
Dynamics CRM
SharePoint Designer 2007
SharePoint Portal Server 2001
Windows SharePoint Services
Windows SharePoint Services 3.0
Project Server 2003
Project Server 2007
Dynamics – Point of Sale
Dynamics AX
Dynamics GP
Dynamics Retail Management System (RMS)
Dynamics SL
SQL Server 2000
Visual Basic .NET 2003
Visual C# .NET 2003
Visual C++ .NET 2003
Visual C++ 2005
Visual SourceSafe 6.0
Windows Server 2003
Windows Server 2003
Outlook 2003
ADO.NET 1.1
ASP.NET 1.0
Visual Studio Team Foundation Server
Visual Studio 2005 Team Edition
Windows Internet Explorer 7
BizTalk Server 2000
BizTalk Server 2002
BizTalk Server 2004
BizTalk Server 2006
Visual Studio 6.0
Access 2000
Access 2002
Access 2003
Access 2007
Access 97
Collaboration Data Objects 2.0
Commerce Server 2002
Content Management Server 2001
Commerce Server 2007
Content Management Server 2002
Data Access Components 2.7
Data Access Components 2.8
DirectX 9.0b
Office Small Business Accounting 2006
Accounting 2007
ActiveSync 4.1
Class Server 2.0
Groove 2007
Windows Vista
Outlook 2007
OneNote 2003
OneNote 2007
Office X for Mac
Zune software
Zune Live
Zoo Tycoon 2
Flight Simulator 2002
Dungeon Siege II

Cervo Technologies
The Right Source to Outsource

Oracle Database FAQS

Sharepoint Portal Server KB

Outlook 2007 Knowledge Base Articles

Visual Studio 6.0 Knowledge Base Articles

The Jet OLE DB version 4.0 provider supports the SELECT @@Identity query that allows you to retrieve the value of the auto-increment field generated on your connection. Auto-increment values used on other connections to your database do not affect the...


The Jet OLE DB version 4.0 provider supports the SELECT @@Identity query that allows you to retrieve the value of the auto-increment field generated on your connection. Auto-increment values used on other connections to your database do not affect the results of this specialized query. This feature works with Jet 4.0 databases but not with older formats.

MORE INFORMATION

The following code demonstrates using the SELECT @@Identity to retrieve the value of the newly inserted auto-increment field. The code snippet also includes code to create the table for the query.
Dim cnDatabase As ADODB.Connection
Dim rsNewAutoIncrement As ADODB.Recordset
Dim strConn As String
Dim strSQL As String
Dim strPathToMDB As String
   
strPathToMDB = "C:\NewJet4.MDB"
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
           "Data Source=" & strPathToMDB & ";"
Set cnDatabase = New ADODB.Connection
    cnDatabase.Open strConn
    
    strSQL = "CREATE TABLE AutoIncrementTest " & _
             "(ID int identity, Description varchar(40), " & _
             "CONSTRAINT AutoIncrementTest_PrimaryKey PRIMARY KEY (ID))"
    cnDatabase.Execute strSQL, , adCmdText + adExecuteNoRecords
    
    strSQL = "INSERT INTO AutoIncrementTest " & _
             "(Description) VALUES ('AutoIncrement Test')"
    cnDatabase.Execute strSQL, , adCmdText + adExecuteNoRecords
    
    strSQL = "SELECT @@Identity"
    Set rsNewAutoIncrement = New ADODB.Recordset
    rsNewAutoIncrement.Open strSQL, cnDatabase, adOpenForwardOnly, _
                            adLockReadOnly, adCmdText
    MsgBox "New Auto-increment value is: " & rsNewAutoIncrement(0).Value

    rsNewAutoIncrement.Close
    Set rsNewAutoIncrement = Nothing
    strSQL = "DROP TABLE AutoIncrementTest"
    cnDatabase.Execute strSQL, , adCmdText + adExecuteNoRecords
    cnDatabase.Close
Set cnDatabase = Nothing
				
Thanks to this newly added functionality, you can see the newly added auto-increment values in your client-side ActiveX Data Objects (ADO) recordsets in ADO 2.1 and later. When you submit the new row to the Jet provider by calling Update or UpdateBatch (depending on your choice of LockType), the ADO cursor engine generates an INSERT INTO query to create the new row in the table. If the recordset contains an auto-increment field, ADO will also generate a SELECT @@Identity query to retrieve the value generated for that auto-increment field. The following code demonstrates this feature:
Dim cnDatabase As ADODB.Connection
Dim rsNewAutoIncrement As ADODB.Recordset
Dim strConn As String
Dim strSQL As String
Dim strPathToMDB As String
    
strPathToMDB = "C:\NewJet4.MDB"
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
          "Data Source=" & strPathToMDB & ";"
    Set cnDatabase = New ADODB.Connection
    cnDatabase.Open strConn
    
    strSQL = "CREATE TABLE AutoIncrementTest " & _
             "(ID int identity, Description varchar(40), " & _
             "CONSTRAINT AutoIncrementTest_PrimaryKey PRIMARY KEY (ID))"
    cnDatabase.Execute strSQL, , adCmdText + adExecuteNoRecords

    strSQL = "SELECT ID, Description FROM AutoIncrementTest"
    Set rsNewAutoIncrement = New ADODB.Recordset
    rsNewAutoIncrement.CursorLocation = adUseClient
    rsNewAutoIncrement.Open strSQL, cnDatabase, adOpenStatic, _
                            adLockOptimistic, adCmdText
    rsNewAutoIncrement.AddNew
    rsNewAutoIncrement("Description").Value = "AutoIncrement Test"
    rsNewAutoIncrement.Update
    MsgBox "New Auto-increment value is: " & rsNewAutoIncrement(0).Value

    rsNewAutoIncrement.Close
    Set rsNewAutoIncrement = Nothing
    strSQL = "DROP TABLE AutoIncrementTest"
    cnDatabase.Execute strSQL, , adCmdText + adExecuteNoRecords
    cnDatabase.Close
Set cnDatabase = Nothing
				
You can create a new Jet 4.0 database using Microsoft Access 2000 or using the ADOX library that is included with MDAC 2.1. To use this library in your Visual Basic project, create a reference to Microsoft ADO Ext. 2.1 for DDL and Security. You can then use code like the following to create a new Jet 4.0 database:
Dim strPathToMDB As String
Dim catNewDatabase As ADOX.Catalog
    
    strPathToMDB = "C:\NewJet4.MDB"
    If Dir(strPathToMDB) <> "" Then
        Kill strPathToMDB
    End If
    strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
              "Data Source=" & strPathToMDB & ";"
    Set catNewDatabase = New ADOX.Catalog
    catNewDatabase.Create strConn
    Set catNewDatabase = Nothing
				
To determine the format of your Microsoft Access database, check the dynamic "Jet OLEDB:Engine Type" property in the Connection object's Properties collection. The property will return a value of 5 for Jet 4.x databases. The following code snippet demonstrates using the property:
Dim cnDatabase As ADODB.Connection
Dim strConn As String
Dim strPathToMDB As String
    
strPathToMDB = "C:\NewJet4.MDB"
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
          "Data Source=" & strPathToMDB & ";"
Set cnDatabase = New ADODB.Connection
    cnDatabase.Open strConn
    If cnDatabase.Properties("Jet OLEDB:Engine Type").Value = 5 Then
        MsgBox "Jet 4.0 database"
    Else
        MsgBox "Not a Jet 4.0 database"
    End If
    cnDatabase.Close
Set cnDatabase = Nothing
				

REFERENCES

Microsoft Data Access SDK; search on: "Provider-Defined Properties"; topic: "DBPROPSET_JETOLEDB_DBINIT"


APPLIES TO
Microsoft OLE DB Provider for Jet 4.0
Microsoft Data Access Components 2.1 Service Pack 2
Microsoft Data Access Components 2.5
Microsoft Data Access Components 2.6
Microsoft Data Access Components 2.7
Microsoft ActiveX Data Objects 2.1 Service Pack 2
Microsoft ActiveX Data Objects 2.5
Microsoft ActiveX Data Objects 2.6
Microsoft ActiveX Data Objects 2.7
Microsoft Visual Basic 5.0 Professional Edition
Microsoft Visual Basic 6.0 Professional Edition
Microsoft Visual Basic 5.0 Enterprise Edition
Microsoft Visual Basic 6.0 Enterprise Edition

Keywords: 
kbinfo kbdatabase kbjet KB232144

Copyright © 2004 - 2007 Gridview.org, Inc. All rights reserved. Powered by Smart Web Content Management System