Home » Applications » Access » Select Count * (count total of records)

Select Count * (count total of records)

VB.NET


Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
 Try
 '/ access db connection - MS Access 2007/10 
 '  driver * you can define your own sql connection
 Dim ConnDB As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;
                                   Data Source=" & Server.MapPath("my_db.accdb"))

'/ open connection
    ConnDB.Open()
     Dim cmd As New OleDbCommand("select count(*) from table1 WHERE 
                                  location = 'My_Location'", ConnDB)
     Dim count As Integer = CInt(cmd.ExecuteScalar())

      If count >= 90 Then '/ if count >= do this
        lblMsg1.Text = "There is 90 or more records!"
      Else
        lblMsg1.Text = "Total number of records is " & count.ToString()
      End If

   Catch ex As Exception
     Throw New Exception(ex.Message)
     Finally
   ConnDB.close()
   End Try
End If

End Sub