title: Select MAX ID Access database
body: <p>I'm developing a School management System in vb.net with access db.
  while accepting fees the receipt must be unique but starts from 1 in every new session
  My using MAX ID query like</p>
  <pre><code> str = &quot;SELECT MAX([receipt_id]) FROM [Transaction] WHERE [session]=&quot; &amp; Home.Label2.Text
      cmd = New OleDb.OleDbCommand(str, cn)
      da.SelectCommand = cmd
      da.Fill(ds, &quot;Transaction&quot;)
      Dim lastid As Integer
      If cmd.ExecuteScalar() Is DBNull.Value Then
          lastid = 0 + 1
      Else
          lastid = Convert.ToInt32(cmd.ExecuteScalar()) + 1
      End If
  
      receipt_no.Text = lastid
  </code></pre>
  <p>But the problem is it returns last id instead of max from that session.
  Please can you help me out.</p>
  <p>Here is the explanation my query is</p>
  <pre><code>SELECT MAX([receipt_id]) FROM [Transaction] WHERE [session]=2018-2019
  </code></pre>
  <p>The output will be 0 then 1 then 2 but when I fire</p>
  <pre><code>SELECT MAX([receipt_id]) FROM [Transaction] WHERE [session]=2019-2020
  </code></pre>
  <p>The output is 0 then 2 It returns max from the table transaction not considering where clause.</p>