Solved

Retrieve last record in field SQL query Select statement

Posted on 2010-09-14
6
842 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 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Suggested Solutions

Read about achieving the basic levels of HRIS security in the workplace.
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
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…

829 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