Results 1 to 7 of 7

Thread: Max Value

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2008
    Posts
    18

    Thumbs up Max Value

    Hey guys, new to the forums and new to visual basic
    only been on visual basic for about 3 days now and im
    stuck finding the highest value I've tryed a
    few ways but It seems that it doesnt like to include
    "Input2" and "input3" for example if I put

    3 in Input 1

    2 in Input 2

    4 in Input 3

    It will show 3 as the highest input and not 4 :/
    anyways guys here is the code thanks in advanced



    Private Sub Command1_Click()
    Dim Max As Long
    Dim Sum As Long

    Sum = Val(input1.Text) + (input2.Text) + (input3.Text)
    Label4.Caption = Sum


    If Val(input1.Text) > Val(input2.Text) Then
    Max = input1.Text
    ElseIf Val(input1.Text) > Val(input3.Text) Then
    Max = input1.Text
    ElseIf Val(input3.Text) > Val(input1.Text) Then
    Max = input3.Text
    ElseIf Val(input2.Text) > Val(input1.Text) Then
    Max = input2.Text
    ElseIf Val(input2.Text) > Val(input3.Text) Then
    Max = input2.Text
    End If
    Label5.Caption = Max
    End Sub

  2. #2
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: Max Value

    Instead of comparing the textboxes with each other, how about comparing them with the highest value so far...

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Nov 2008
    Posts
    18

    Re: Max Value

    Ermm anychance of explaing how? I'm quiet new to VB

  4. #4
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: Max Value

    It sounds too much like homework for me to post any code, sorry. Look at what you have, using ElseIf won't work you need to use separate If's for each comparison. You also only need to make 2 comparisons (if x > y...) to determine the highest out of 3 values.

  5. #5
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Max Value

    Code:
       Max = Val(input1.Text)
       If Val(input2.Text) > Max Then Max = Val(input2.Text)
       If Val(input3.Text) > Max Then Max = Val(input3.Text)
       Label5.Caption = Max
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  6. #6
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: Max Value

    Quote Originally Posted by anhn
    Code:
       Max = Val(input1.Text)
       If Val(input2.Text) > Max Then Max = Val(input2.Text)
       If Val(input3.Text) > Max Then Max = Val(input3.Text)
       Label5.Caption = Max
    When in doubt, AnHn delivers.

    The highest value is always the first value that arrives until another one arrives that exceeds it.

    The lowest value is always the first value that arrives until another one arrives that undercuts it.
    Doctor Ed

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Nov 2008
    Posts
    18

    Re: Max Value

    Thanks for the code man worked great

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