Results 1 to 4 of 4

Thread: Maximum Number from the table field

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Location
    Bangladesh
    Posts
    106

    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?

  2. #2
    Hyperactive Member hassa046's Avatar
    Join Date
    May 2001
    Location
    Venlo, The Netherlands
    Posts
    336

    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.
    Better to regret things you did, than those you didn't
    International Intelligence

  3. #3
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Maximum Number from the table field

    Or do you mean

    Select Max(Amount) From TableName

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Dim con As New OleDbConnection("connection string here")
    2. Dim cmd As New OleDbCommand("SELECT MAX(Amount) FROM MyTable", con)
    3.  
    4. con.Open()
    5.  
    6. Dim result As Object = cmd.ExecuteScalar()
    7.  
    8. con.Close()
    9.  
    10. If result Is DBNull.Value Then
    11.     'The query returned a null value.
    12.     'That indicates that there is no maximum value because there are no records.
    13. Else
    14.     Dim maxAmount As Decimal = CDec(result)
    15.  
    16.     'Use maxAmount here.
    17. 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:
    1. Dim result As Object = myDataTable.Compute("MAX(Amount)")
    2.  
    3. If result Is DBNull.Value Then
    4.     'The query returned a null value.
    5.     'That indicates that there is no maximum value because there are no records.
    6. Else
    7.     Dim maxAmount As Decimal = CDec(result)
    8.  
    9.     'Use maxAmount here.
    10. 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:
    1. If myDataTable.Rows.Count > 0 Then
    2.     Dim maxAmount As Decimal = CDec(myDataTable.Compute("MAX(Amount)"))
    3.  
    4.     'Use maxAmount here.
    5. End If
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width