473,606 Members | 2,877 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 11730
> 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
4893
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 was updated (inputed through asp natch'). The database is not updated everyday so that won't work but something like the database was updated on Monday 11/24/2003, I would want it to say "New job listings as of Monday, 11/24/2003" if it were...
3
2291
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 VALUE 1 How can i retrieve the VALUE of the last record of ID 2 using ASP?? In this case, the asnwer given to me should be Record 3.
6
1802
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 file (instead of x files) is created. The file created is based on the last record in datafile.txt. The program is as follows:
1
2718
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 databases: this access db, and a remote MySQL server. The MySQL tables are linked in the access db. What I have done so far is created an On Open event for a report that does the following: * prompt user for the date range * get which sources were...
1
1725
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 copy partial fields only for those records where the field = Y
3
1977
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 create. Then for each record to be created I need to find the last ControlNumber used within the selected GNumber (one GNumber may correspond to several ArticleCode), and increment it by one. Is this possible ?
17
2518
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
3320
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 certain number of them. Firstly I identify all the parent records that have the requird number of child records and insert them into the result table.
6
2659
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 order. Here is a code snippet Do While not rsNews.EOF Response.Write("<table class=""tableborder"" border=""0"" cellspacing=""0"" cellpadding""0"" width=""100%"" ") Response.Write("<tr>") Response.Write("<td class=""tabletitle""...
0
8031
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7962
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8443
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8107
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8315
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5971
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5467
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3989
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2452
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.