2 Replies - 918 Views - Last Post: 17 November 2012 - 09:18 AM Rate Topic: -----

#1 jlessard93  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 08-October 12

Finding Second Smallest Number

Posted 13 November 2012 - 07:25 PM

What I am trying to do is find the second smallest number as inputted from the user. Also, the button used to display when I can display the second smallest number can only appear after two entries have been made. What i have so far is a weird code that works sometimes if I input numbers from Large to small but not small to large. I am rather stuck but this is what I have so far.
Dim Smallest As Integer
Dim SecondSmallest As Integer
Dim Counter As Integer
Option Explicit
Private Sub Form_Load()
    DisplayButton.Enabled = False
End Sub
Private Sub DisplayButton_Click()
    Picture1.Print SecondSmallest; "is the second smallest number"
End Sub

Private Sub EnterButton_Click()
    Dim N As Integer

    N = NumberBox.Text
    If Counter < 2 Then
        Counter = Counter + 1
        SecondSmallest = N
        Smallest = N
    ElseIf Counter >= 2 Then
        DisplayButton.Enabled = True
        If N <= SecondSmallest Then
            SecondSmallest = Smallest
            Smallest = N
        Else
            Smallest = N
        End If
     ElseIf N > Smallest And N < SecondSmallest Then
            SecondSmallest = N
    
    End If
    Counter = Counter + 1
    Picture1.Cls
End Sub


Is This A Good Question/Topic? 0
  • +

Replies To: Finding Second Smallest Number

#2 maj3091  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 300
  • View blog
  • Posts: 1,776
  • Joined: 26-March 09

Re: Finding Second Smallest Number

Posted 14 November 2012 - 01:47 AM

Have a look at your main IF statement as a starting point.

The second ElseIf, looks as though it shouldn't be there, but should be inside the first Elseif, otherwise it's unlikely to get executed.

So something like:

If Counter < 2 then
 ' Do something
Else
 ' Do Something else
End if

Was This Post Helpful? 0
  • +
  • -

#3 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: Finding Second Smallest Number

Posted 17 November 2012 - 09:18 AM

A couple of simplifications: set your enabled property to false in design mode rather than in the load event. Use a label to display your value rather than the picture control.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1