-1

I want to write a code with multiple parameters like this :

Set param1 = qt.Parameters.Add("City Parameter", xlParamTypeVarChar) 

and be able to use either one parameter or the second. VBA obliged me to have values for both parameters, but I want to have one obligatory parameter and others optional. What should I add to the code?

7
  • Possible duplicate of VBA Function Optional parameters Commented Jul 3, 2019 at 15:30
  • Really, just using google would have been faster: learn.microsoft.com/en-us/office/vba/language/concepts/… Commented Jul 3, 2019 at 15:33
  • 1
    You need to make both parameters optional, because Optional parameters must appear at the end of the paremeter list. That said, asking about a procedure's signature without even sharing what your procedure's signature is, is a little short. Commented Jul 3, 2019 at 15:46
  • Wait is this about VBA optional parameters, or query/SQL parameters? If this has nothing to do with SQL, please remove the tag, it makes the post confusing. Commented Jul 3, 2019 at 15:49
  • @Mathieu Guindon it seems to be about Parameters for QueryTables in Excel which routinely involve SQL. Of course, I defer to OP. Commented Jul 3, 2019 at 15:55

1 Answer 1

1

Optional parameters are defined with the term Optional

Example:

Sub DoStuff(requiredParam as String, Optional optParam as String = "") 

Adding in the = "" is helpful so you can write out the logic depending on whether a value has been passed in. "" is just an example and you should choose a value that isn't liekly to be passed in.

Sign up to request clarification or add additional context in comments.

4 Comments

You can also leave the ´= something´ and use ´IsMissing()´ to check if a value was passed.
@L8n IsMissing will only work with a Variant though
@MathieuGuindon Thanks, good to know, somehow missed that since I usually use it with a variant and ´select case´ for fake overloading.
Anyway, in this specific case the ´= ""´ should not be necessary as the default value is ´""´ anyway.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.