I need to create a form with 2 text boxes to receive numbers as input and 1 command button that displays a message box containing the larger of the two numbers. To do so, I need to write code in the Click event of the command to call a user-defined function named FindLargestNumber. Pass it 2 arguments(textboxvalues) and display the result in a message box. Write code in the FindLargestNumber function to determine the larger number and return result to the calling procedure(command button Click event).

The code is working, but when I enter in two numbers, only the first character of each string of numbers is compared. What happends is when I compare "667" and "7", "7" is returned as the highest number. I can't figure out what's wrong. Please help!!

Option Compare Database
Dim number1 As Double
Dim number2 As Double

Private Sub cmd1_Click()
 MsgBox "The largest number is " & FindLargestNumber(Me.text1, Me.text2)
End Sub
Public Function FindLargestNumber(number1, number2) As Integer
  If number1 > number2 Then
     FindLargestNumber = number1
  ElseIf number2 > number1 Then
     FindLargestNumber = number2
  End If
End Function

You are mixing up doubles and integers, also use val(me.text1) etc. like:

Option Compare Database
Dim number1 As Double
Dim number2 As Double
Private Sub cmd1_Click()
 MsgBox "The largest number is " & FindLargestNumber(val(Me.text1), val(Me.text2))
End Sub
Public Function FindLargestNumber(number1, number2) As Double
  If number1 > number2 Then
     FindLargestNumber = number1
  ElseIf number2 > number1 Then
     FindLargestNumber = number2
  End If
End Function

Ok it's working. Thanks for your help. I appreciate it!

The above code is incorrect. If you use

If number1 > number2 Then
    FindLargestNumber = number1
ElseIf number2 > number1 Then
    FindLargestNumber = number2
End If

and you call it with

FindLargestNumber(12,12)

it will not return a value. Use

If number1 > number2 Then
    FindLargestNumber = number1
Else
    FindLargestNumber = number2
End If
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.