I really think you should reconsider the design. Dropping and re-creating
the index and the field every time a record is deleted seems like a very bad
idea to me. With that caveat, though, here's how it might be done. Note that
this assumes that the table does not participate in any relationships. If it
does, you'll have to drop and re-create those, too.
Module Module1
Sub Main()
Dim strConnection As String = _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"User ID=Admin;" & _
"Data Source=c:\usenet\test.mdb;" & _
"Persist Security Info=False"
Dim cnn As System.Data.OleDb.OleDbConnection = _
New System.Data.OleDb.OleDbConnection(strConnection)
Dim cmd As System.Data.OleDb.OleDbCommand = _
New System.Data.OleDb.OleDbCommand( _
"DROP INDEX PrimaryKey ON tblTest", cnn)
Try
cnn.Open()
cmd.ExecuteNonQuery()
Console.WriteLine("Index dropped OK!")
cmd.CommandText = _
"DELETE * FROM tblTest WHERE TestID = " & _
"(SELECT MIN(TestID) FROM tblTest)"
cmd.ExecuteNonQuery()
Console.WriteLine("Record deleted OK!")
cmd.CommandText = "ALTER TABLE tblTest DROP COLUMN TestID"
cmd.ExecuteNonQuery()
Console.WriteLine("Column dropped OK!")
cmd.CommandText = _
"ALTER TABLE tblTest ADD COLUMN TestID " & _
"INT NOT NULL IDENTITY"
cmd.ExecuteNonQuery()
Console.WriteLine("Column added OK!")
cmd.CommandText = _
"CREATE INDEX PrimaryKey ON " & _
"tblTest (TestID) WITH PRIMARY"
cmd.ExecuteNonQuery()
Console.WriteLine("Index created OK!")
Catch ex As Exception
Console.WriteLine(ex.Message)
Finally
cnn.Close()
Console.ReadLine()
End Try
End Sub
End Module