I've just started teaching myself database access with vb.net and I've been using this as a reference. My MS Access database is a little more complex than the example my reference goes through since I've set up a relational database. I have two tables; Players and, Villages. Each Player can have one more more villages (1 to many relationship), this relationship is tied between the player ID which is the unique key in the Players table and used in the Villages table to define the owner of the village. Here's how I add a new player to the database;
Public Sub NewPlayer(ByVal playerName As String) Dim playerID As Integer = getLastIdent("Players") + 1 Dim tmpnum As Integer = playerID Dim dsNewRow As DataRow Dim Sql As String Sql = "SELECT * FROM Players" da = New OleDb.OleDbDataAdapter(Sql, con) da.Fill(ds, "Players") Dim cb As New OleDb.OleDbCommandBuilder(da) dsNewRow = ds.Tables("Players").NewRow() dsNewRow.Item("ID") = playerID dsNewRow.Item("NameP") = playerName dsNewRow.Item("Coins") = 0 ds.Tables("Players").Rows.Add(dsNewRow) da.Update(ds, "Players") Call NewVillage(playerName, playerID) End Sub The getLastIdent subroutine was my attempt at finding the last value used in the player ID field;
Public Function getLastIdent(ByVal tblname As String) Dim sql As String = "SELECT @@IDENTITY FROM " & tblname Dim cmd As New OleDb.OleDbCommand(Sql, con) Return cmd.ExecuteScalar() End Function But this doesn't work at all (returns 0 no matter what). The players can actually be created without a hitch (the incorrect ID number seems to fix itself at some point with the database correctly incrementing the AutoNumber. However to assign a village to the new player I run NewVillage;
Public Sub NewVillage(ByVal playerName As String, ByVal playerID As Integer) Dim numVils As Integer = getVilCount() Dim xpos As Integer Dim ypos As Integer xpos = CInt(Rnd()*50) ypos = CInt(Rnd()*50) Dim dsNewRow As DataRow Dim Sql As String Sql = "SELECT * FROM Villages" da = New OleDb.OleDbDataAdapter(Sql, con) da.Fill(ds, "Villages") Dim cb As New OleDb.OleDbCommandBuilder(da) dsNewRow = ds.Tables("Villages").NewRow() dsNewRow.Item("ID") = numVils + 1 dsNewRow.Item("NameV") = playerName & "'s Village" dsNewRow.Item("Xpos") = xpos dsNewRow.Item("Ypos") = xpos dsNewRow.Item("Owner") = playerID ds.Tables("Villages").Rows.Add(dsNewRow) da.Update(ds, "Villages") End Sub This is where the whole thing collapses. The playerID that's passed to the subroutine isn't the correct value that's associated with the player I've just created (in face there isn't any player by that name since it always tries playerID = 1 and the AutoNumber starts higher than that due to deleted failed rows).
So how can I go about getting the true value of the ID from the players table? Is there some call I can make that forces the AutoNumber to update and then I can recheck it?