To further demonstrate how writing a user-defined function can help you work around the limitations of VBScript components, I've created the InputBoxEx function in Listing A. This function extends the capability of the InputBox function so that InputBoxEx not only lets users input data in a dialog box but also validates the data they enter.
InputBoxEx has four arguments, three of which are InputBox arguments (prompt, title, and default). The fourth argument, func, is a parameter that evaluates to a function name defined in the same VBScript file. InputBoxEx calls this function during its execution to provide a user-defined behavior.
When you use the Function statement to declare a function that has arguments, you don't specify the argument values because you must specify them each time you call the function. Instead, you specify the variables representing the arguments. For example, you declare InputBoxEx as
Function InputBoxEx(strPrompt, strTitle, strDefault, _ strFunc)
but you call it using argument values such as
InputBoxEx("Enter numbers only", "InputBoxEx", _ "Numbers only", "NumbersOnly")
InputBoxEx uses two built-in functions (InputBox and Eval) within a While...Wend loop to accomplish its tasks of getting and validating users' input. InputBoxEx also takes advantage of the user-defined function Quote. First, InputBoxEx uses the InputBox function to obtain the user's input and assigns InputBox's return value to the str variable. InputBoxEx then passes the return value to the user-defined function that the strFunc argument specifies. You can have this used-defined function perform whatever task you need; however, the function you pass through strFunc can take only one string argument. In InputBoxEx, the used-defined function, called NumbersOnly, checks whether the str variable is a numeric string. The string argument NumbersOnly takes is
Before InputBoxEx uses the NumbersOnly function to validate the data, InputBoxEx formats the input for evaluation during runtime. This formatting is necessary because you already specified the name of the function (NumbersOnly) as a variable (strFunc), so you can't use code such as
for evaluation. Instead, you need to construct a string containing the validation function's name and parameter. The code to construct the string uses the Quote function, which simply encloses the string in quotes.
After InputBoxEx formats the executable string, it passes the string to Eval. The string ends up being Eval("NumbersOnly(str)"). When NumbersOnly executes, it calls the built-in IsNumeric function to determine whether the string is a number. NumbersOnly returns a Boolean value. If the value is True, the user's input is valid (i.e., it's a number) and InputBoxEx continues to execute. If the value is False, the user's input is invalid (i.e., it's not a number) and InputBoxEx prompts the dialog box to reappear. InputBoxEx doesn't start executing again until the user clicks OK after entering new input.
As I mentioned earlier, InputBoxEx uses the Eval function to format and evaluate the user's input at runtime. Only VBScript 5.0 provides this function. If you're using an earlier version of VBScript, you can upgrade your system by downloading VBScript 5.0 at http://www.microsoft.com/ msdownload/vbscript/scripting.asp. Other alternatives are to rewrite this code with JScript (all versions have runtime evaluation capability) or write a script-based COM component.
As a final note, consider that Eval, in this context, is a smarter version of the Call statement. You can use Eval to call functions indirectly. More important, unlike the Call statement, Eval lets you manage return values, as the InputBoxEx function shows.