Default Values for Parameters in a Sub

Posted on 2004-04-15
Last Modified: 2010-05-02
Hello Experts!

Can't really believe i'm asking this question cos it seems so obvious, but for some reason i cant find the correct syntax!

How do i set a default for a parameter in a sub?


Private Sub Example(MyVariable as Integer, MySecondVariable as Integer)

and i want to put
Example , 2

If no Parameter is entered, how would i set MyVariable to 1 for example?

At the moment i'm just getting an error saying argument not optional if i leave it blank!
Cheers in advance,

Question by:Bobeo
  • 2
LVL 48

Expert Comment

ID: 10836337
Private Sub Example(MyVariable as Integer,optional  MySecondVariable as Integer = 2)
LVL 48

Accepted Solution

Mikal613 earned 50 total points
ID: 10836345
Private Sub Example(MyVariable as Integer,optional  MySecondVariable as Integer = 1)
LVL 19

Expert Comment

ID: 10836372
Private Sub Example(Optional MyVariable As Integer = 1, Optional MySecondVariable As Integer = 1)



Author Comment

ID: 10836405
Thanks Mikal613, just what i needed!

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now