|
-
Mar 17th, 2007, 11:01 AM
#1
Thread Starter
Lively Member
Maximum Number from the table field
I am using Access
I want to know the maximum number from the Amount field. Will you please help me?
-
Mar 17th, 2007, 11:10 AM
#2
Hyperactive Member
Re: Maximum Number from the table field
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.
-
Mar 17th, 2007, 11:19 AM
#3
Re: Maximum Number from the table field
Or do you mean
Select Max(Amount) From TableName
-
Mar 17th, 2007, 07:34 PM
#4
Re: Maximum Number from the table field
If you mean you want to get just the maximum value from the table in Access then it would be:
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
See www.connectionstrings.com for the appropriate connection string format.
If you already have the records in a Datatable then it would be similar but different:
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 If
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:
If myDataTable.Rows.Count > 0 Then
Dim maxAmount As Decimal = CDec(myDataTable.Compute("MAX(Amount)"))
'Use maxAmount here.
End If
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|