Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Empty Recordset Problem

Status
Not open for further replies.

mrDrive

MIS
Aug 29, 2002
94
US
Hi,

I'm creating a new record for my db. Before I run my insert statement, I have to create a new ID value (primary key) for the new record. I'm trying to make this application ready for an empty db table.

Code:
dim objRS, strSQL, intNewID
strSQL = "select max(ID) + 1 as NEXTIDVAL " & _
         "from MyTable"
set objRS = Server.CreateObject("ADODB.Recordset")
objRS.Open strSQL, objConn
if objRS.eof then
	intNewID = 1
else
	intNewID = objRS("NEXTIDVAL")
end if
response.Write("intNewID = " & intNewID)
objRS.close
set objRS = nothing
set strSQL = nothing

For some reason, intNewID comes back empty even though I look for an empty recordset.

Any help would be greatly appreciated!

mD
 
When you select a Max on an empty table, a NULL will be returned. You will never receive an EOF on that query.

You might try doing something as follows:

If isnull(objRS("NEXTIDVAL") Then
intNewID = 1
Else
intNewID = objRS("NEXTIDVAL")
End If
 
Can't you setup the ID field in the table as an identity field which auto-increments whenever a new record is added?

Tony
________________________________________________________________________________
 
Just change this line of code:

Code:
strSQL = "select ISNULL(max(ID), 0) + 1 as NEXTIDVAL from MyTable"

You actually don't then need to test for EOF as you will never get an empty recordset, as wrentmore pointed out.

--James
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top