MS Access autonumber

Hoilknild

New Member
Does anyone know how to get the last value entered into a table with the primary key set to autonumber? With an ASP .NET page.<BR><BR>Thank you<BR>AaronI haven't found an elegant way to do this with access, but the following works.<BR><BR>Dim objCmd As New oleDBCommand _<BR>("SELECT MAX(Ref) FROM Scores where rID = @rID",conn)<BR><BR>Dim objParam As oleDbParameter<BR><BR>objParam = objCmd.Parameters.Add("@rID", oleDbType.Char)<BR><BR>objParam.Direction = ParameterDirection.InPut<BR><BR>objParam.Value = http://aspmessageboard.com/archive/index.php/ViewState("strID") <BR>objAdapter.SelectCommand = objCmd<BR>objAdapter.Fill(objDataSet,"RecID")<BR>objCmd.Connection.Close()<BR><BR>intRecID = objDataSet.Tables("RecID").Rows(0)(0)<BR><BR>Here, I'm using the "Get Max Record" function of Access. The ref field is the autonumbered field in my database table. I get it and put it into a database table named "RecID". I then set the variable intRecID equal to the first (and only) value in this dataset. This is the autonumbered value from the database table for the last record added.<BR><BR>Since more than one user may be on the database, I use a WHERE qualifier in the Select statement which is the user's ID code. This code is used as an identifier of those records that belong to a certain user, so it's a field in every entry in the DB table. You may not be using anything like this, but it's one extra safety step to ensure that you are picking the correct last-record-entered.<BR><BR>Good luck<BR>TomttStill looking for a better solution to the Max() function.<BR><BR>If you are only going to use the Max() function:<BR>oCommand = New OleDbCommand("SELECT Max(ID) as MaxID from D_Courses", oConnection)<BR><BR>' add parameters here (and in sql above)<BR>oCommand.Connection.Open() <BR>IDArray(j) = oCommand.ExecuteScalar() <BR>oCommand.Connection.Close()<BR><BR>Aaron<BR>
 
Back
Top