Returning results of max function from MS access table with VB.net
Posted on 2007-10-08
I have a simple MS access database with a list of contacts - each of which has a sequential increasing
unique contact ID number. When I run a query with the SQL string below, I get a nice table with one column (entitled Expr1) with one row (containing the max value for strContactID) .
I want to take that value and use later in my VB code. However, when I run the following VB.net code I get an error message which says
"NoSystem.InvalidOperationException was unhandled"
- "No data exists for the row/column."
I'd love some guidance on what I'm doing wrong.
Dim myOLEDBConnection As New OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;" & _
"Data Source= C:\Documents and Settings\Administrator\Desktop\Contacts.mdb")
Dim mySQL As String = "SELECT Max(strContactID) as Expr1 FROM tblContacts"
Dim myCommand As New OleDbCommand(mySQL, myOLEDBConnection)
Dim myContactReader As OleDbDataReader = myCommand.ExecuteReader()
Dim strMaxContactID As String
strMaxContactID = myContactReader.Item("Expr1")