Solved

Retrieve last record in field SQL query Select statement

Posted on 2010-09-14
6
846 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 7

Assisted Solution

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

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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 143

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 42

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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
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…

756 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