Solved

SELECT Last Record from an online Access Database

Posted on 2011-03-09
6
438 Views
Last Modified: 2012-06-27
Good Morning

I would like the following code to only return the last record in the database file. It currently displays each record to the last record. If I remove the loop it just returns the first record. My understanding is limited, but I think I need to change the SELECT to something like:

strSQL = "SELECT tblComments.S1, tblComments.S2, tblComments.S3, tblComments.Date, tblComments.EN, tblComments.CW, tblComments.Status, tblComments.SSO FROM tblComments WHERE ID_no = SELECT MAX lngRecordNo FROM tblComments"

Many thanks
<%

%>
<html>
<head>
<title>Database</title>
</head>
<body bgcolor="white" text="black">
<%
'Dimension variables
Dim adoCon 				'Holds the Database Connection Object
Dim rsdatabase			'Holds the recordset for the records in the database
Dim strSQL				'Holds the SQL query for the database



'Create an ADO connection odject
Set adoCon = Server.CreateObject("ADODB.Connection")

'Set an active connection to the Connection object using a DSN-less connection
adoCon.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("Comments.mdb")

'Set an active connection to the Connection object using DSN connection
'adoCon.Open "DSN=database"

'Create an ADO recordset object
Set rsdatabase = Server.CreateObject("ADODB.Recordset")

'Initialise the strSQL variable with an SQL statement to query the database
strSQL = "SELECT tblComments.S1, tblComments.S2, tblComments.S3, tblComments.Date, tblComments.EN, tblComments.CW, tblComments.Status, tblComments.SSO FROM tblComments;"

'Open the recordset with the SQL query 
rsdatabase.Open strSQL, adoCon

'Loop through the recordset
Do While not rsdatabase.EOF
	
	'Write the HTML to display the current record in the recordset
	Response.Write ("<br>")
	Response.Write (rsdatabase("S1"))
	Response.Write ("<br>")
	Response.Write (rsdatabase("S2"))
	Response.Write ("<br>")
	Response.Write (rsdatabase("S3"))
	Response.Write ("<br>")
	Response.Write (rsdatabase("Date"))
	Response.Write ("<br>")
	Response.Write (rsdatabase("EN"))
	Response.Write ("<br>")
	Response.Write (rsdatabase("CW"))
	Response.Write ("<br>")
	Response.Write (rsdatabase("Status"))
	Response.Write ("<br>")
	Response.Write (rsdatabase("SSO"))
	Response.Write ("<br>")


	'Move to the next record in the recordset
	rsdatabase.MoveNext

Loop

'Reset server objects
rsdatabase.Close
Set rsdatabase = Nothing
Set adoCon = Nothing
%>
</body>
</html>

Open in new window

0
Comment
Question by:Washcare
  • 3
  • 2
6 Comments
 
LVL 5

Expert Comment

by:mayankagarwal
ID: 35081989
"SELECT LAST  tblComments.S1, tblComments.S2, tblComments.S3, tblComments.Date, tblComments.EN, tblComments.CW, tblComments.Status, tblComments.SSO FROM tblComments
0
 

Author Comment

by:Washcare
ID: 35082076
Mayankagarwal

I have just tried the above and I get a 500 - Internal server error, message?

Many thanks
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 35082345
If you turn off "Show Friendly HTTP Error Messages" in your browser advanced settings then it should show you the underlying error. Most likely it is a simple syntax error somewhere.
0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

Author Comment

by:Washcare
ID: 35082421
Carl

I have just tried the above and get the same error message. I am sure its linked to changing the SELECT to include LAST.

Regards
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 35082541
Try your query as the following which basically orders the records in descending order and pulls the first one (assuming ID_no is the name iof the ID column on the table):
strSQL = "SELECT TOP 1 tblComments.S1, tblComments.S2, tblComments.S3, tblComments.Date, tblComments.EN, tblComments.CW, tblComments.Status, tblComments.SSO FROM tblComments ORDER BY ID_no DESC"

Open in new window

0
 

Author Comment

by:Washcare
ID: 35085195
Great Many thanks
0

Featured Post

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!

Question has a verified solution.

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

Suggested Solutions

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

803 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