By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,516 Members | 1,183 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,516 IT Pros & Developers. It's quick & easy.

Argument not Optional

P: 17
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()
End Sub

Public Function FindLargestNumber(ByVal Number1 As Double, ByVal Number2 As Double)
Number1 = Val(txtOne.Value)
Number2 = Val(txtTwo.Value)

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.

Mar 3 '10 #1
Share this Question
Share on Google+
3 Replies

Expert 5K+
P: 8,701
  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:
    Expand|Select|Wrap|Line Numbers
    1. Public Function FindLargestNumber(ByVal Number1 As Double, ByVal Number2 As Double) As Variant
    2. If Number1 = Number2 Then
    3.   FindLargestNumber = Null
    4. ElseIf Number1 < Number2 Then
    5.   FindLargestNumber = Number2
    6. Else
    7.   FindLargestNumber = Number1
    8. End If
    9. End Function
  2. Function Call:
    Expand|Select|Wrap|Line Numbers
    1. Dim varRetVal As Variant
    3. varRetVal = FindLargestNumber(Val(Me![txtOne]), (Val(Me![txtTwo]))
Mar 3 '10 #2

Expert Mod 100+
P: 2,321
In your function declaration:
Expand|Select|Wrap|Line Numbers
  1. Public Function FindLargestNumber(ByVal Number1 As Double, ByVal Number2 As Double)
  2. Number1 = Val(txtOne.Value)
  3. Number2 = Val(txtTwo.Value)
  5. End Function
Your saying that the function expects input variables Number1 and Number2.

However when your calling the function:
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdLarger_Click()
  2.   FindLargestNumber
  3. 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.
Mar 3 '10 #3

P: 17
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!

Mar 3 '10 #4

Post your reply

Sign in to post your reply or Sign up for a free account.