Argument not Optional

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rnashting
    New Member
    • Mar 2010
    • 17

    Argument not Optional

    Good Morning! I am still very new to VBA, and I'm getting an error that I can't figure out with a basic code. Here's what I have at this point:

    Option Compare Database
    Option Explicit

    Private Sub cmdLarger_Click ()
    FindLargestNumb er
    End Sub

    Public Function FindLargestNumb er(ByVal Number1 As Double, ByVal Number2 As Double)
    Number1 = Val(txtOne.Valu e)
    Number2 = Val(txtTwo.Valu e)

    End Function


    This is only the beginning of the code for a project, but when I click on the command button, I get "argument not optional". I think it might be how I've declared the function, but I'm not sure what I'm doing wrong. Any help you can provide would be amazing.

    TJ
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    1. It appears as though you wish to pass Values in the two Text Boxes to evaluate and see which, if any, is larger. In that case, the simplest approach would be:
      Code:
      Public Function FindLargestNumber(ByVal Number1 As Double, ByVal Number2 As Double) As Variant
      If Number1 = Number2 Then
        FindLargestNumber = Null
      ElseIf Number1 < Number2 Then
        FindLargestNumber = Number2
      Else
        FindLargestNumber = Number1
      End If
      End Function
    2. Function Call:
      Code:
      Dim varRetVal As Variant
      
      varRetVal = FindLargestNumber(Val(Me![txtOne]), (Val(Me![txtTwo]))

    Comment

    • TheSmileyCoder
      Recognized Expert Moderator Top Contributor
      • Dec 2009
      • 2322

      #3
      In your function declaration:
      Code:
      Public Function FindLargestNumber(ByVal Number1 As Double, ByVal Number2 As Double)
      Number1 = Val(txtOne.Value)
      Number2 = Val(txtTwo.Value)
      
      End Function
      Your saying that the function expects input variables Number1 and Number2.

      However when your calling the function:
      Code:
      Private Sub cmdLarger_Click()
        FindLargestNumber
      End Sub
      You are NOT providing any input variables. And thats what your error message is telling you. That the variable (or argument) is not optional.

      Comment

      • rnashting
        New Member
        • Mar 2010
        • 17

        #4
        Thanks!

        Thanks for your help! I'm still not sure I grasp the calling of the arguments concept, but I see how the code you gave me works, and I'm going to try it with a few different examples to gain practice!

        TJ

        Comment

        Working...