I want the user to enter the numbers into 5 text boxes, then the user hits a command button, then the program is to sort the numbers and display the ascended sort into a seperate label box or simply just print on the form.
I found a way to do your thing. Perhaps it's possible to find a shorter way but I din't have much time to search.
Here is what I did.
'Make sure that the input in the textbox can only be numeric
'Therefore create a Function (fDecimalSep)
'Put a textbox on your form. Leave the name as Text1.
'Copy it, then paste another textbox onto the form.
'When VB asks if you want to create a control array, say "Yes".
Code begins:
Public Function fDecimalSep() As String
fDecimalSep = Mid$(Format(1234, "###.##", 5, 1)
End Function
Private Sub Command1_Click()
Dim Small As Single
Dim Teller As Integer
Dim I As Integer
Dim Y As Integer
For Teller = 0 To Text1.Count - 1
For I = 0 To Text1.Count - 1
If Text1(I).Text <> "" Then
Small = Text1(I).Text
For Y = 0 To Text1.Count - 1
If Text1(Y).Text <> "" Then
If Text1(Y).Text < Small Then
Small = Text1(Y).Text
End If
End If
Next Y
End If
Next I
I = 0
For I = 0 To Text1.Count - 1
If Text1(I) <> "" Then
If Small = Text1(I).Text Then
Text1(I).Text = ""
Exit For
End If
End If
Next I
'put numbers in labels
If Label1.Caption = "" Then
Label1.Caption = Small
Else
Label1.Caption = Label1.Caption & ";" & Small
End If
Next Teller
End Sub
Private Sub Text1_KeyPress(Index As Integer, KeyAscii As Integer)
Select Case KeyAscii
Case vbKey0 To vbKey9, vbKeyBack
'allowed keys -- do nothing
Case Asc(fDecimalSep)
'allows 1 comma in the textbox
If InStr(Text1(Index).Text, fDecimalSep) Then
KeyAscii = 0
End If
Case vbKeyReturn
'Enter key
KeyAscii = 0 'avoids the beep
If Index < Text1.UBound Then
Text1(Index + 1).SetFocus
Else
Text1(Text1.LBound).SetFocus
End If
Case Else
KeyAscii = 0 'discard the keystroke
End Select
End Sub
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.