Tips for writing
VBScript
Here are a few helpful tips for writing scripts in
VBScript.
- You can use subroutines and procedures to keep your code readable.
- The script engine is picky about syntax, and does not give very readable error messages
if a syntax error is encountered. Double check your script to make sure that all keywords
are spelled correctly. A common mistake is to use Endif instead of End
if.
- All types are variant, which can cause problems during comparisons. For example,
If var = 1 then
End If
may fail even if var does equal 1. Instead, use
If CInt(var) = 1 then
End If
which will force the types to match. Make sure that var can never be non-numeric,
however, or you will get a type mismatch error! This technique can also be used for other
comparisons, such as dates or strings.
- You can create COM objects, including ADO objects or any custom objects that you write.
You could write a set of business rules in C++ or Visual Basic, for example, and then use
them in scripts. This is very powerful, and can greatly improve the performance of scripts
which use intensive calculations.
- Objects are created differently in VBScript than in Visual Basic, using the CreateObject
function. For example, instead of
Dim rsADO as new ADODB.Recordset
You use
Set rsADO = CreateObject("ADODB.Recordset")
- Sometimes there are subtle differences between Visual Basic functions and their VBScript
counterparts. Often, new language features show up in one before the other. For example,
Dictionaries were available in VBScript before they appeared in Visual Basic. Be sure to
check the VBScript documentation for syntax details.
Debugging Scripts
You can use the Microsoft Script Debugger to assist in
debugging scripts. This debugger is available on Microsofts web site at http://msdn.microsoft.com/scripting. By placing STOP statements in your script, you can
force the script to break into the debugger and step through the code, examine variables,
etc.
|