Search code examples
vb.netms-accessoledb

VB Get Autonum values


I use the first "Using" statement below to insert a row to a table called "Archives". This table has a primary key that is an autonum in an Access db. The second Using statement I use to retrieve the value for the autonum field, searching by the parameters that I just entered. While this works perfectly well, it just seems ugly. Is there a way to get the autonum field returned to me after the insert? (BTW - I have deleted some code from between these two statements so if it looks a little strange, that may be why.

    Using myConn As New OleDbConnection(strConnectionString),
        myInsertCommand As New OleDbCommand("INSERT INTO Archives (ArchUserName, ArchUserDomain, ArchDate, ArchRoot, ArchStatus) 
        VALUES (@strArchUser, @strArchUserDomain, @dteArchDate, @strArchRoot, @strArchStatus);", myConn)
            myInsertCommand.Parameters.Add("@strArchUser", OleDbType.VarChar, 100).Value = strArchUser
            myInsertCommand.Parameters.Add("@strArchDomain", OleDbType.VarChar, 100).Value = strArchDomain
            myInsertCommand.Parameters.Add("@dteArchDate", OleDbType.Date, 20).Value = dteArchDate
            myInsertCommand.Parameters.Add("@strArchRoot", OleDbType.VarChar, 255).Value = strArchRoot
            myInsertCommand.Parameters.Add("@strArchStatus", OleDbType.VarChar, 100).Value = strArchStatus
            myConn.Open()
            myInsertCommand.ExecuteNonQuery()
        End Using

       Dim sql As String = "SELECT ArchID
       FROM Archives
       WHERE ArchUserName = @ArchUserName
       AND ArchUserDomain = @ArchUserDomain
       AND ArchDate = @ArchDate
       AND ArchRoot = @ArchRoot"

        Using myConn As New OleDbConnection(strConnectionString),
              command As New OleDbCommand(sql, myConn)
            With command.Parameters
                .Add("@ArchUserName", OleDbType.VarChar, 50).Value = strArchUser
                .Add("@ArchUserDomain", OleDbType.VarChar, 50).Value = strArchDomain
                .Add("@ArchDate", OleDbType.Date).Value = dteArchDate
                .Add("@ArchRoot", OleDbType.VarChar, 50).Value = strArchRoot
            End With

            myConn.Open()
            strArchID = "Arch" & CStr(command.ExecuteScalar())
            strDirectoryName = "Archive" & CStr(command.ExecuteScalar())
            ReturnCode = 0
        End Using

Solution

  • Use @@Identity on the same connection immediately after the insert.

    Private Function InsertArchiveRetrieveID(strArchUser As String, strArchDomain As String, dteArchDate As Date, strArchRoot As String, strArchStatus As String) As Integer
        Dim NewID As Integer
        Using myConn As New OleDbConnection(strConnectionString),
                myInsertCommand As New OleDbCommand("INSERT INTO Archives (ArchUserName, ArchUserDomain, ArchDate, ArchRoot, ArchStatus) 
                                                                   VALUES (@strArchUser, @strArchUserDomain, @dteArchDate, @strArchRoot, @strArchStatus);", myConn)
            With myInsertCommand.Parameters
                .Add("@strArchUser", OleDbType.VarChar, 100).Value = strArchUser
                .Add("@strArchDomain", OleDbType.VarChar, 100).Value = strArchDomain
                .Add("@dteArchDate", OleDbType.Date, 20).Value = dteArchDate
                .Add("@strArchRoot", OleDbType.VarChar, 255).Value = strArchRoot
                .Add("@strArchStatus", OleDbType.VarChar, 100).Value = strArchStatus
            End With
            myConn.Open()
            myInsertCommand.ExecuteNonQuery()
            Using RetrieveNewIDCommand As New OleDbCommand("Select @@Identity From Archives,", myConn)
                NewID = CInt(RetrieveNewIDCommand.ExecuteScalar)
            End Using
        End Using
        Return NewID
    End Function
    
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim NewID = InsertArchiveRetrieveID(txtUser.Text, txtDomain.Text, DateTimePicker1.Value, txtRoot.Text, txtStatus.Text)
        Dim strArchID = "Arch" & CStr(NewID)
        Dim strDirectoryName = "Archive" & CStr(NewID)
    End Sub