Solved

Retrieve last record in field SQL query Select statement

Posted on 2010-09-14
6
833 Views
Last Modified: 2012-05-10
I’m trying to write a SELECT statement that brings back the last record of a field (“RECORD”).

Would it look like this?:

Recordset1.Source = “SELECT TOP 1 * FROM JAX ORDER BY RECORD DESC”

Any assistance is greatly appreciated.

Thank you.
0
Comment
Question by:arendt73
6 Comments
 
LVL 7

Assisted Solution

by:tlovie
tlovie earned 150 total points
ID: 33677450
yes
0
 
LVL 41

Accepted Solution

by:
pcelba earned 200 total points
ID: 33677584
This really depends on the order definition.

SELECT TOP 1 * FROM JAX ORDER BY RECORD DESC

retrieves the last record based on the values of RECORD column. Is it correct?

If you need the most recently entered value then you would need to order the query by som etimestamp.

If you need the value which is stored in the physically last DBF record then you should use RECNO() for ordering (this query will work just on DBF file not on SQL Server):

SELECT TOP 1 RECNO(), * FROM JAX ORDER BY 1 DESC

Also the Recordset has MoveLast method, so you could use it ...
0
 

Author Comment

by:arendt73
ID: 33682302
I get the following error when using: SELECT TOP 1 * FROM JAX ORDER BY RECORD DESC

Microsoft OLE DB Provider for ODBC Drivers error '80040e10'

[Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 1.

/statusreports/jax/results-test.asp, line 13

When I mean the last record, I mean the latest entry into the RECORD field.
<%

Dim Recordset1

Dim Recordset1_numRows



Set Recordset1 = Server.CreateObject("ADODB.Recordset")

Recordset1.ActiveConnection = "Driver={Microsoft Access Driver (*.mdb)};DBQ=c:\inetpub\wwwroot\statusreports\db\status.mdb"



Recordset1.Source = "SELECT TOP 1 * FROM JAX ORDER BY RECORD DESC"



Recordset1.CursorType = 0

Recordset1.CursorLocation = 2

Recordset1.LockType = 1

Recordset1.Open()



Recordset1_numRows = 0

%>

Open in new window

0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 150 total points
ID: 33682335
if you get that error, it means that RECORD is not a column in the table...
of course, you might try to do this, presuming that RECORD is a reserved keyword:


Recordset1.Source = "SELECT TOP 1 * FROM JAX ORDER BY [RECORD] DESC"

Open in new window

0
 
LVL 41

Expert Comment

by:pcelba
ID: 33682862
If you are entering growing value into the RECORD column then above query should return the latest value.

Access 2007 does not require RECORD to be enclosed in brackets, so what was the problem?
0
 

Author Comment

by:arendt73
ID: 33682890
I forgot to change the name of the field.  My apologizes.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now