I am using Access
I want to know the maximum number from the Amount field. Will you please help me?
Printable View
I am using Access
I want to know the maximum number from the Amount field. Will you please help me?
Amount of what?
as far as i know, it's a long as the vb6 "Long" variable type (-*2,147,483,648 through 2,147,483,647), but i'm not entirely sure.
Or do you mean
Select Max(Amount) From TableName
If you mean you want to get just the maximum value from the table in Access then it would be:See www.connectionstrings.com for the appropriate connection string format.vb Code:
Dim con As New OleDbConnection("connection string here") Dim cmd As New OleDbCommand("SELECT MAX(Amount) FROM MyTable", con) con.Open() Dim result As Object = cmd.ExecuteScalar() con.Close() If result Is DBNull.Value Then 'The query returned a null value. 'That indicates that there is no maximum value because there are no records. Else Dim maxAmount As Decimal = CDec(result) 'Use maxAmount here. End If
If you already have the records in a Datatable then it would be similar but different:In either case if you know for a fact that the table contains records then you can omit the check for a null result, e.g.vb Code:
Dim result As Object = myDataTable.Compute("MAX(Amount)") If result Is DBNull.Value Then 'The query returned a null value. 'That indicates that there is no maximum value because there are no records. Else Dim maxAmount As Decimal = CDec(result) 'Use maxAmount here. End Ifvb Code:
If myDataTable.Rows.Count > 0 Then Dim maxAmount As Decimal = CDec(myDataTable.Compute("MAX(Amount)")) 'Use maxAmount here. End If