Double up your query to return the last ID that was inserted:
Code:'Declare the object to store the last inserted ID Dim id As Integer = -1 'Declare the connection object Dim con As OleDbConnection 'Wrap code in Try/Catch Try 'Set the connection object to a new instance 'TODO: Change "My Connection String Here" with a valid connection string con = New OleDbConnection("My Connection String Here") 'Create a new instance of the command object Using cmd As OleDbCommand = New OleDbCommand("Insert into Guias([Remetente],[Destinatário]) Values (@remetente, @destinatario); SELECT @@Identity AS [id];", con) 'Parameterize the query With cmd.Parameters .AddWithValue("@remetente", ComboBox1.Text) .AddWithValue("@destinatario", ComboBox2.Text) End With 'Open the connection con.Open() 'Use ExecuteScalar to return a single value id = Convert.ToInt32(cmd.ExecuteScalar()) 'Close the connection con.Close() End Using Catch ex As Exception 'Display the error Console.WriteLine(ex.Message) Finally 'Check if the connection object was initialized If con IsNot Nothing Then If con.State = ConnectionState.Open Then 'Close the connection if it was left open(exception thrown) con.Close() End If 'Dispose of the connection object con.Dispose() End If End Try




Reply With Quote
