locked
Select last record from database?!???? RRS feed

  • Question

  • User-556029652 posted

    Hi there I'm having a problem trying to get the last record from my database.. I'm doing this:

    myCommand.Connection.Open()
            Dim strSQL As String = "SELECT * FROM tblDatabaseRecords ORDER BY ID DESC LIMIT 0,1"
            Dim myDataAdapter As New OleDbDataAdapter(strSQL, myConnection)
            myDataAdapter.Fill(myDataTable)
            Form1.DataGridView1.DataSource = myDataTable

    myCommand.Connection.Open()
            Dim strSQL As String = "SELECT * FROM tblDatabaseRecords ORDER BY ID DESC LIMIT 1"
    
            Dim myDataAdapter As New OleDbDataAdapter(strSQL, myConnection)
    
            myDataAdapter.Fill(myDataTable)
    
            Form1.DataGridView1.DataSource = myDataTable


    but it's saying "Syntax error in ORDER BY clause." Is there something wrong I am doing? Or can I get the last record of my database by another way?


    Thanks in advance

    Thursday, October 7, 2010 3:35 AM

Answers

  • User-1360095595 posted

    Try like this: SELECT TOP 1 * FROM tblDatabaseRecords ORDER BY ID DESC

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 7, 2010 7:42 AM

All replies

  • User-1360095595 posted

    Try like this: SELECT TOP 1 * FROM tblDatabaseRecords ORDER BY ID DESC

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 7, 2010 7:42 AM
  • User-1148431695 posted

    Hi,

    Please check if the following query works:

    select * from <your_table> where your_id_col = (select max(your_id_col) from your_table)

    I hope it helps. 

    Thursday, October 7, 2010 7:57 AM