Here is the table I'm working with, the primary key is an incremental ID and the table is called ‘Categories’:

CategoryID Primary Key int (4) identity seed 1, identity increment 1
CategoryName varchar (50)
Description varchar (50)

------------------------------------------------

I have created the following stored procedure to insert values into the table (the CategoryID is missing from the stored procedure as it gets created automatically).

“CREATE PROCEDURE [InsertNewCategory]

@CatName VARCHAR(50),
@Desc VARCHAR(50)

AS

BEGIN TRANSACTION

INSERT INTO [Categories]
(
CategoryName,
Description
)
VALUES
(
@CatName,
@Desc
)

COMMIT TRANSACTION”

-----------------------------------------------

I have executed the procedure in VB by using the following code:

VB Code:
  1. Dim rstSave As New ADODB.Recordset
  2. Dim mProcName As String
  3.  
  4. Screen.MousePointer = 11
  5.     If ValidateSave = True Then
  6.         If mCatID = 0 Then
  7.             mCatName = txtCatName
  8.             mDesc = txtDesc
  9.             'Procedure name with parameters
  10.             mProcName = "InsertNewCategory '" & mCatName & "', '" & mDesc & "'"
  11.                 'Executes a function in a class to execute the command
  12.                 If objSQLServer.ExecuteCommand(mProcName) = 0 Then
  13.                     MsgBox "The new record has been saved to the database"
  14.                 End If
  15.         End If
  16.     End If
  17. Screen.MousePointer = 0

The command executes perfectly but the problem I'm having is how to retrieve the CategoryID from the newly created record without having to do another check using the CategoryName and Description as criteria. Is there a way of selecting or outputting the new ID while inserting a new record?

Please help if possible, thanks.