Say you want to pop-up a dialog to the user to get some information. You could use the XDocument.UI.ShowModalDialog method, but this is Level 3, meaning you can only call it from Fully Trusted forms. You could also use managed code business logic and create a WinForm to display to the user, but that requires InfoPath SP1 functionality, not to mention that that seems like a lot of work just to throw a dialog with a input box.

VBScript to the rescue! VBScript has 2 methods which are quite useful in InfoPath, and are not available in JScript: MsgBox and InputBox

MsgBox throws a dialog with different buttons (OK, Cancel, Yes, No). (Note, InfoPath SP1 has a method which has similar functionality. XDocument.UI.Confirm)

InputBox throws a dialog with a text box. You can then take the information that the user types into the text box, and use it in your InfoPath form.

 This example shows how to call the InputBox method, and as a bonus, adds the value as a new item in a secondary data source/data connection (in this case, an XML file). You can have a dropdown lisbox control which gets its values from the secondary data source/data connection. This provides a cool way for the user to add a new item to the dropdown listbox.

First of all, you'll need to set your scripting language to VBScript. You can set this from Tools->Form Options->Advanced Tab. Do this before you start writing any script, otherwise you won't be able to change it.

Once you've set your scripting language to VBScript, now you're ready to start adding events. Add a button to your form, change the label and ID to GetName, and then click on the “Microsoft Script Editor” button (In SP1, this button is now labeled “Edit Form Code“)

In the script editor, add the following code:

Sub GetName_OnClick(eventObj)
     Dim name
     Dim oAuxDOM
     Dim oNewCustomer
     name = InputBox("Enter customer name:")

     Set oAuxDOM = XDocument.GetDOM("customers")
     ' Create a new customer element
     Set oNewCustomer = oAuxDOM.createNode(1, "customer", "")
     oNewCustomer.text = name
End Sub

You might have noticed in the script above that there is a secondary data source/data connection called “customers“ which we haven't added to our form yet. We'll add that now. Create a new file in notepad and call it customers.xml:

<?xml version="1.0" ?>
     <customer>ABC Tile Co.</customer>
     <customer>ACME Brick</customer>

Add this file to your form using the resource manager (Tools->Resource Manager in InfoPath RTM, Tools->Resource Files in SP1)

Then create a secondary data source using this resource xml file (Tools->Secondary Data Sources in InfoPath RTM, Tools->Data Connections in SP1)

Finally, add a dropdown listbox control to your form. From the properties dialog for the dropdown lisbox, choose to “Look up entries in a database, web service, or file” (Again, in SP1, this text is different, but similar) Set the Data Source dropdown to be the “Customers“ data source, and set the Entries to bind to the /customers/customer node. Leave value and display name as “.“

That's it! Preview the form and give it a try. If all goes well, every time you type something in the VBScript InputBox, it gets added to the end of your dropdown listbox. Not bad for less than 10 lines of script.