JayBeeOH

MS Access Database Primary Key Update Workaround

Jul 15th, 2015
390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 3.92 KB | None | 0 0
  1. '====================================================================================
  2. '     MS Access Database Primary Key Update Workaround (Massi)
  3. '====================================================================================
  4. '   By Joe Bolen
  5.  
  6. '   Description:    Addresses a problem of Microsoft's Access database table's primary
  7. '                   key during an insert not returning the new AutoNumber identity
  8. '                   generated value to a Visual Studio .NET written programs. This
  9. '                   problem occurs because MS Access does not allow multiple batch
  10. '                   query statements in one command.
  11. '
  12. '   Documentation is at:
  13. '                   App's Visual Basic .NET code is at http://pastebin.com/0NrHzLdV
  14. '                   Video tutorial at YouTube: http://www.youtube.com/user/bolenpresents
  15.  
  16. '====================================================================================
  17. '     Add AccessIDHelper.vb Class:
  18. '====================================================================================
  19.  
  20. Imports System.Data.OleDb
  21.  
  22. Public Class AccessIDHelper
  23.  
  24.    ''' <summary>
  25.    ''' Retrieves the primary key autonumber values from Access
  26.    ''' </summary>
  27.    ''' <remarks></remarks>
  28.    Public Shared Sub SetPrimaryKey(ByVal trans As OleDbTransaction, _
  29.                                    ByVal e As OleDbRowUpdatedEventArgs)
  30.  
  31.       If e.Status = UpdateStatus.Continue AndAlso _
  32.          e.StatementType = StatementType.Insert Then
  33.          ' If this is an INSERT operation...
  34.          Dim pk = e.Row.Table.PrimaryKey
  35.          ' and a primary key column exists...
  36.          If pk IsNot Nothing AndAlso pk.Count = 1 Then
  37.             Dim cmdGetIdentity As New OleDbCommand("SELECT @@IDENTITY", trans.Connection, trans)
  38.             ' Execute the post-update query to fetch new @@Identity
  39.             e.Row(pk(0)) = CInt(cmdGetIdentity.ExecuteScalar)
  40.             e.Row.AcceptChanges()
  41.          End If
  42.       End If
  43.  
  44.    End Sub
  45.  
  46. End Class
  47.  
  48. '====================================================================================
  49. '     Add to the strongly typed DataSet's partial class code behind:
  50. '====================================================================================
  51. '    Add Namespace of your DataSet Table Adapters, then for each TableAdapter add a
  52. '    partial class of the TableAdpater with the Sub _adapter_rowUpdated. This
  53. '    procedure will be called immediately after the table has been updated to the data
  54. '    source, but before the connection is closed.
  55. ' ------------------------------------------------------------------------------------
  56.  
  57. Namespace SampleDBDataSetTableAdapters
  58.  
  59.     Partial Class CustomersTableAdapter
  60.  
  61.         Private Sub _adapter_RowUpdated(sender As Object, e As OleDb.OleDbRowUpdatedEventArgs) _
  62.             Handles _adapter.RowUpdated
  63.  
  64.             ' Update Identity / AutoNumber field using AccessIDHelper class.
  65.             AccessIDHelper.SetPrimaryKey(Me.Transaction, e)
  66.  
  67.         End Sub
  68.  
  69.     End Class
  70.  
  71.     Partial Class OrdersTableAdapter
  72.  
  73.         Private Sub _adapter_RowUpdated(sender As Object, e As OleDb.OleDbRowUpdatedEventArgs) _
  74.             Handles _adapter.RowUpdated
  75.  
  76.             ' Update Identity / AutoNumber field using AccessIDHelper class.
  77.             AccessIDHelper.SetPrimaryKey(Me.Transaction, e)
  78.  
  79.         End Sub
  80.  
  81.     End Class
  82.  
  83. End Namespace
  84.  
  85. Partial Class SampleDBDataSet
  86. End Class
  87.  
  88. '====================================================================================
  89. '   For further information, see ...
  90. '
  91. '   Massi, Beth. "Using TableAdapters to Insert Related Data into an MS Access
  92. '   Database." Sharing the goodness.... May 14, 2009. http://blogs.msdn.com/b/
  93. '   bethmassi/archive/2009/05/14/using-tableadapters-to-insert-related-data-into-
  94. '   an-ms-access-database.aspx (accessed July 13, 2015).
  95. '====================================================================================
Advertisement
Add Comment
Please, Sign In to add comment