473,405 Members | 2,185 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

get last record from database without looping

how to get the last record from database without looping?

Whenever the user need to insert a new record to the database, it just
increment the id field by one from the last record.

I tried objRS.MoveLast, but it wont work.

Jul 19 '05 #1
1 11697
> how to get the last record from database without looping?

There isn't really any concept of "last." If you have a column that
indicates order without ambiguity, you can use one of these:

SELECT MAX(column) FROM table

SELECT TOP 1 * FROM table ORDER BY column DESC

Also see http://www.aspfaq.com/2499
Whenever the user need to insert a new record to the database, it just
increment the id field by one from the last record.
You're killing scalability and concurrency if you're expecting to select the
max(id), add 1, and use that as part of your insert. IDENTITY (SQL Server)
and AUTOINCREMENT (Access) are much better suited for this than rolling your
own.

On the other hand, if you're using IDENTITY or AUTOINCREMENT and are just
trying to retrieve the last value inserted, see http://www.aspfaq.com/2174
I tried objRS.MoveLast, but it wont work.


So wait, you're adding a record, and using objRS? Please read
http://www.aspfaq.com/2191

In the future, please tell us what database and version you're using,
include code samples as well as a more explicit description of the problem.
"wont work" doesn't give us much to go on. Do you get an error message? If
so, what is it? Or, how is it behaving that you think is wrong? How do you
think it should behave?

(I've read your message five times and I still have no idea if you're adding
rows or retrieving rows.)

--
Aaron Bertrand
SQL Server MVP
http://www.aspfaq.com/
Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Dan Matthews | last post by:
Hey everybody, I have an 2000 Access database that stores job listings from potential employers for a school. I would like to be able to display the date on a webpage the last time the database...
3
by: Daniel Ng | last post by:
Thanks for the 2 guys how hep to to solve to count the number of recount in forum. I'm done wif that. Got 1 more question.. Example Record 1: ID 2 VALUE 2 Record 2: ID 3 VALUE 3 Record 3: ID 2...
6
by: vasilijepetkovic | last post by:
Hello All, I have a problem with the program that should generate x number of txt files (x is the number of records in the file datafile.txt). Once I execute the program (see below) only one...
1
by: Justin Koivisto | last post by:
I am trying to create a report that displays a name of an advertising source and count of the number of times it was hit between certain date ranges. The data is split between two different...
1
by: David | last post by:
Hi, I have a continuous form based on a query. Lets say the form displays 6 records. I also have a button against each record which sets a field to Y or N for each record. I am trying to...
3
by: NV | last post by:
I've a table called GReg with the fields GNumber, ControlNumber, ArticleCode, ProdNumber and Date. In a form I select the ArticleCode from one combo box, and I define the number of records to...
17
by: michel.ank | last post by:
Hi, I'm using the class PrintLines and my last record of page aren't with the borders. Somebody can help me? Thanks,
2
by: Catch_22 | last post by:
Hi, I have a stored procedure that has to extract the child records for particular parent records. The issue is that in some cases I do not want to extract all the child records only a...
6
by: Christo | last post by:
I have this script for showing news on a page, but i want it to only show the last 10 records, as in the 10 records that were added to the database last. the script shows the entries in descending...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.