Wrox Home  
Search P2P Archive for: Go

  Return to Index  

asp_databases thread: Finding the last record in a database and incrementing by one


Message #1 by "Brian Leary" <brianleary@h...> on Mon, 28 Oct 2002 22:25:49
Hi,
I am using a ASP with a microsoft access database and I have a page that 
creates a unique ID then stores it in the database. What I really want to 
do is to first always look at the last record in my database and increment 
by one, then pass that new value as the unique ID. Can someone give me an 
idea how I can go about doing this ? 
Thank you
Brian
Message #2 by "Peter Foti (PeterF)" <PeterF@S...> on Mon, 28 Oct 2002 17:27:51 -0500
Hmm... not a very efficient method.  However, if you REALLY want to do it
this way....

I'll assume your ID field is called myID.

SELECT myID FROM myTable ORDER BY myID DESC

The first record in the recordset contains the highest value in the ID
field.  Note, this is a very inefficient method, and I don't recommend doing
it this way.  Why not just use an AutoNumber?

Regards,
Peter


-----Original Message-----
From: Brian Leary [mailto:brianleary@h...]
Sent: Monday, October 28, 2002 10:26 PM
To: ASP Databases
Subject: [asp_databases] Finding the last record in a database and
incrementing by one


Hi,
I am using a ASP with a microsoft access database and I have a 
page that 
creates a unique ID then stores it in the database. What I 
really want to 
do is to first always look at the last record in my database 
and increment 
by one, then pass that new value as the unique ID. Can someone 
give me an 
idea how I can go about doing this ? 
Thank you
Brian
Message #3 by Greg Griffiths <greg2@s...> on Mon, 28 Oct 2002 23:19:04 +0000
you could use the MAX keyword or just use an Autonumber field.

SELECT MAX(custid) FROM customers

At 22:25 28/10/02 +0000, you wrote:
>Hi,
>I am using a ASP with a microsoft access database and I have a page that
>creates a unique ID then stores it in the database. What I really want to
>do is to first always look at the last record in my database and increment
>by one, then pass that new value as the unique ID. Can someone give me an
>idea how I can go about doing this ?
>Thank you
>Brian


Message #4 by "Ken Schaefer" <ken@a...> on Tue, 29 Oct 2002 11:25:20 +1100
What's wrong with using an autonumber?

www.adopenstatic.com/experiments/fastestautonumber.asp
has the code you need to;
a) insert the record
b) get the autonumber of the newly inserted record (gaurenteed to be the
correct value)

Cheers
Ken

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
From: "Brian Leary" <brianleary@h...>
Subject: [asp_databases] Finding the last record in a database and
incrementing by one


: I am using a ASP with a microsoft access database and I have a page that
: creates a unique ID then stores it in the database. What I really want to
: do is to first always look at the last record in my database and increment
: by one, then pass that new value as the unique ID. Can someone give me an
: idea how I can go about doing this ?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Message #5 by "Brian Leary" <brianleary@h...> on Tue, 29 Oct 2002 15:01:47
Hi  - Thanks for the response. I will try using an autonumber but 
hopefully there is a way to format the autonumber so it starts at a 
certain number (say 2000). I'm not sure if this is possible, but I will 
take a look and see. 
Thanks again
Brian



 Hmm... not a very efficient method.  However, if you REALLY want to do it
this way....

I'll assume your ID field is called myID.

SELECT myID FROM myTable ORDER BY myID DESC

The first record in the recordset contains the highest value in the ID
field.  Note, this is a very inefficient method, and I don't recommend 
doing
it this way.  Why not just use an AutoNumber?

Regards,
Peter


-----Original Message-----
From: Brian Leary [mailto:brianleary@h...]
Sent: Monday, October 28, 2002 10:26 PM
To: ASP Databases
Subject: [asp_databases] Finding the last record in a database and
incrementing by one


Hi,
I am using a ASP with a microsoft access database and I have a 
page that 
creates a unique ID then stores it in the database. What I 
really want to 
do is to first always look at the last record in my database 
and increment 
by one, then pass that new value as the unique ID. Can someone 
give me an 
idea how I can go about doing this ? 
Thank you
Brian
Message #6 by "Ken Schaefer" <ken@a...> on Wed, 30 Oct 2002 11:50:24 +1100
Why do you need a fixed number?

Cheers
Ken

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
From: "Brian Leary" <brianleary@h...>
Subject: [asp_databases] RE: Finding the last record in a database and inc
rementing by one


: Hi  - Thanks for the response. I will try using an autonumber but
: hopefully there is a way to format the autonumber so it starts at a
: certain number (say 2000). I'm not sure if this is possible, but I will
: take a look and see.
: Thanks again
: Brian

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Message #7 by "Brian Leary" <brianleary@h...> on Wed, 30 Oct 2002 22:24:07
Only because the person who I built the application for wants a fixed 
number. I can do it easy enough in Access if I use the autonumber like you 
said and then have another field that always is 2000 (based on a 
variable), then a third field that take both fields together and uses it 
as the unique id (I would probably need to do this through query builder). 
This is probably the easiest way based on what they want.
Thanks for helping me !!
Brian

> Why do you need a fixed number?

Cheers
Ken

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
From: "Brian Leary" <brianleary@h...>
Subject: [asp_databases] RE: Finding the last record in a database and inc
rementing by one


: Hi  - Thanks for the response. I will try using an autonumber but
: hopefully there is a way to format the autonumber so it starts at a
: certain number (say 2000). I'm not sure if this is possible, but I will
: take a look and see.
: Thanks again
: Brian

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Message #8 by "Samir Chitkara , Gurgaon" <chitkaras@g...> on Thu, 31 Oct 2002 18:05:28 +0530
hi!
fire the following query & increment the value by 1
select max(ID) as newid from table1

samir



-----Original Message-----
From: Brian Leary [mailto:brianleary@h...]
Sent: Tuesday, October 29, 2002 3:56 AM
To: ASP Databases
Subject: [asp_databases] Finding the last record in a database and
incrementing by one


Hi,
I am using a ASP with a microsoft access database and I have a page that 
creates a unique ID then stores it in the database. What I really want to 
do is to first always look at the last record in my database and increment 
by one, then pass that new value as the unique ID. Can someone give me an 
idea how I can go about doing this ? 
Thank you
Brian

  Return to Index