Find last entry

  • Thread starter Thread starter nag1_uk
  • Start date Start date
N

nag1_uk

Guest
How can I find the last entry that was made in the database? I have an auto number as one of the fields.

What I am wanting to do eventually is find the last 15 entries, then ouput the data to a graph. Or even better have a input box to say hw many entries are used to create the graph.

Thanks

Mike Cole
 
Make a query. sort descending on your autonumber field. then put "15" in the box next to the summation sign. In design view, that is.

Fuga.
 
Select top 15 BLAH from TABLE order by BLAH desc

that will get the last 15 entries. of course, replace blah with your field name(s).

if you want to get the last ID added as soon as you add the record then right after you do your APPEND query, run this

SELECT @@IDENTITY
 
Can it be used in Access too?
 
Last edited:
if you want to get the last ID added as soon as you add the record then right after you do your APPEND query, run this

SELECT @@IDENTITY

In the attached database, I created a table tblClient with two fields:
[ID] (autonumber, primary key); [Client] text

and two queries:
Query1:-
INSERT INTO tblClient ( Client )
VALUES ([forms]![frmGetIDAdded]!txtClient);

Query2:-
SELECT @@IDENTITY;


When I opened the form, entered a client name, and clicked on the command button to run Query1 and Query2,
Code:
Private Sub Command0_Click()
 
   DoCmd.OpenQuery "Query1"  ' an append query
   DoCmd.OpenQuery "Query2"  ' SELECT @@IDENTITY
   
End Sub
Query1 ran fine but Query2 always returned 0.


Could you tell us how to run SELECT @@IDENTITY in Access?
 

Attachments

PHP:
Private Sub Command0_Click()
 
  
  Dim SQL As String, conn As Connection, LastID As Integer, RS As Recordset
  Set conn = CurrentProject.Connection
  
  SQL = "Query1 " & Me.txtClient
  conn.Execute (SQL)
  
  SQL = "Query2 " ' SELECT @@IDENTITY
  Set RS = conn.Execute(SQL)
  LastID = RS(0)
  Debug.Print LastID
 
   RS.Close
   Set RS = Nothing
   conn.Close
   Set conn = Nothing
   
End Sub
 
..........
SQL = "Query2 " ' SELECT @@IDENTITY
Set RS = conn.Execute(SQL)
LastID = RS(0)
Debug.Print LastID

Thanks. Worked like a charm! Real cool!
 
what's not clear is that I changed QUERY1. I removed the criteria that it checks the form automatically. Instead I gave it a parameter @txtName for the criteria. Then I had to pass the form var to the query.
 

Users who are viewing this thread

Back
Top Bottom