Snitz Forums 2000
Snitz Forums 2000
Home | Profile | Register | Active Topics | Members | Search | FAQ
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 Community Forums
 Code Support: ASP (Non-Forum Related)
 finding last record?
 New Topic  Topic Locked
 Printer Friendly
Author Previous Topic Topic Next Topic  

Alfred
Senior Member

USA
1527 Posts

Posted - 30 August 2003 :  23:26:57  Show Profile  Visit Alfred's Homepage
I am trying to select data from the latest record in a table, but have trouble using the date field for it:
quote:
strSql = "SELECT MAX(end_date) FROM GAMES"
set intLASTGAME = my_Conn.Execute(strSql)

Is there a simpler code to just select the last record?

Alfred
The Battle Group
CREDO

Nikkol
Forum Moderator

USA
6907 Posts

Posted - 30 August 2003 :  23:30:52  Show Profile
if you have an autonumber field, do a MAX of that field.

Nikkol ~ Help Us Help You | ReadMe | 3.4.03 fixes | security fixes ~
Go to Top of Page

ruirib
Snitz Forums Admin

Portugal
26364 Posts

Posted - 30 August 2003 :  23:50:06  Show Profile  Send ruirib a Yahoo! Message
strSql = "SELECT TOP 1 End_Date FROM Games Order by End_Date DESC"

Good for Access or SQL Server. For MySQL it should be something like:

strSql = "SELECT End_Date FROM Games Order by End_Date DESC LIMIT 1"


Snitz 3.4 Readme | Like the support? Support Snitz too

Edited by - ruirib on 30 August 2003 23:52:21
Go to Top of Page

Alfred
Senior Member

USA
1527 Posts

Posted - 31 August 2003 :  00:52:27  Show Profile  Visit Alfred's Homepage
I got it now to select the latest record:
quote:
strSql = "SELECT TOP 1 End_Date FROM Games Order by End_Date DESC"
set intLASTGAME = my_Conn.Execute(strSql)

strSql = "SELECT * FROM GAMES WHERE end_date= " & intLastGame("end_date") & ""
set intNEWS = my_Conn.Execute(strSql)

response.write strSql
response.end
but when I try to get the other data from it:
quote:
strSql = "SELECT TOP 1 End_Date FROM Games Order by End_Date DESC"
set intLASTGAME = my_Conn.Execute(strSql)

strSql = "SELECT * FROM GAMES WHERE end_date= " & intLastGame("end_date") & ""
set myNEWS = my_Conn.Execute(strSql)

response.write myNEWS("TYPE")
'strSql
response.end
I get an error:
quote:
Error Type:
ADODB.Field (0x80020009)
Either BOF or EOF is True, or the current record has been deleted. Requested operation requires a current record.
/BG/forums/news.asp


Alfred
The Battle Group
CREDO

Edited by - Alfred on 31 August 2003 01:00:16
Go to Top of Page

ruirib
Snitz Forums Admin

Portugal
26364 Posts

Posted - 31 August 2003 :  03:51:01  Show Profile  Send ruirib a Yahoo! Message
Use a single statement, not two:

strSql = "SELECT TOP 1 * FROM Games Order by End_Date DESC"


When using dates in SQL expressions in Access, they need to be delimited by '#' chars. Thus your current problem.


Snitz 3.4 Readme | Like the support? Support Snitz too
Go to Top of Page

Alfred
Senior Member

USA
1527 Posts

Posted - 31 August 2003 :  12:16:22  Show Profile  Visit Alfred's Homepage
Yes.
Can one also select any number of records from the top, like for instance:
quote:
strSql = "SELECT TOP 5 * FROM Games Order by End_Date DESC"
or
quote:
strSql = "SELECT TOP 10 * FROM Games Order by End_Date DESC"

PS.: I found out by trial and error - it seems to work!

It would be nice to specify a date window, but:
I never seem to get those date delimiters right:
quote:
Error Type:
Microsoft VBScript compilation (0x800A03EA)
Syntax error
/BG/forums/news.asp, line 24, column 30
response.write "On " & myNEWS(#'"end_date"'#) & " " & myNEWS("VICTOR")
-----------------------------^

Alfred
The Battle Group
CREDO

Edited by - Alfred on 31 August 2003 14:36:10
Go to Top of Page

ruirib
Snitz Forums Admin

Portugal
26364 Posts

Posted - 31 August 2003 :  16:15:48  Show Profile  Send ruirib a Yahoo! Message
Of course the TOP N clause works. The date delimiters needed to be added as text, before the date is added and after the date is added. However the date delimiters only need to be specified when using them in a SQL statement, not in other situations, like when you just want to display the date, as the code you posted seems to be do (with sintax errors).



Snitz 3.4 Readme | Like the support? Support Snitz too
Go to Top of Page

Alfred
Senior Member

USA
1527 Posts

Posted - 31 August 2003 :  16:59:43  Show Profile  Visit Alfred's Homepage
I understand.
WHat I woul like to do is display data for say, the last 30 days.
How would I have to code this:
quote:
do while not myNEWS.EOF and while myNEWS("end_date")>now-30
which I was obviously just guessing at.

Alfred
The Battle Group
CREDO
Go to Top of Page

Nikkol
Forum Moderator

USA
6907 Posts

Posted - 31 August 2003 :  17:45:30  Show Profile
"SELECT * FROM Games WHERE End_Date > #" & DateAdd("d",-30,Now()) & "# ORDER BY End_Date DESC"

Nikkol ~ Help Us Help You | ReadMe | 3.4.03 fixes | security fixes ~

Edited by - Nikkol on 31 August 2003 17:46:15
Go to Top of Page

Alfred
Senior Member

USA
1527 Posts

Posted - 31 August 2003 :  20:22:00  Show Profile  Visit Alfred's Homepage
Thanks, Nikkol - this is super!
Go peek at my newsreel now:
http://www.ggholiday.com/bg/forums/DEFAULT.ASP

Alfred
The Battle Group
CREDO
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Topic Locked
 Printer Friendly
Jump To:
Snitz Forums 2000 © 2000-2021 Snitz™ Communications Go To Top Of Page
This page was generated in 0.4 seconds. Powered By: Snitz Forums 2000 Version 3.4.07