gw_logo_08.gif (1982 bytes)  
Last edit: 2009-05-04 Graham Wideman

ModelRight

Getting started with ModelRight scripting

<< Introduction to ModelRight scripting and automation

ModelRight's Object Model: Background  >>

Contents

Choice of two programmability approaches

ModelRight provides two main programmability approaches:

1. Built-in VBScript scripting environment 

ModelRight includes Microsoft's Visual Basic Scripting (VBS) environment, which is at version 5.6 at this writing. Scripts can be created, edited and run within ModelRight, and are generally used to manipulate the current database model or its graphics, or to inspect the database model and produce some kind of text output, perhaps a report, or perhaps SQL statements to be run on the database.

You may already be familiar with the Visual Basic for Applications (VBA) environment found in Microsoft products like Access, Excel and Word.  The VBS environment is similar in concept:

However, there are significant differences between VBA and VBS.  In general VBS is suited to smaller-scaled functionality for less-demanding tasks. For example, in VBS one works with only one module at a time, which is suited to small tasks, but less suited to large and complicated bodies of code. Programming is somewhat simplified by virtue of all variables being of type "variant", but function parameters are untyped and thus not checked for type.

There are a variety of ways that the integration of VBS into ModelRight has been made specific to the needs of ModelRight. However the language itself, its general functions and features (not specific to ModelRight) are as documented here at Microsoft Developer Network: VBScript.

2. Automation using external programs

In addition to the built-in VBS programmability, ModelRight also installs an API through which to manipulate ModelRight models/diagrams from an external program, and this API is accessed via COM Automation. Consequently, such external programs can be written in your choice of language, including VBA from Access or Excel; VB, C# or C++ from .NET; Delphi, Python, PHP or any language which can create an Automation client.

This approach is especially powerful where ModelRight modeling activities are part of a larger database application workflow, as suggested in the introduction.  For example:

In other scenarios, automation using an external program may be resorted to after a VBScript project gets too complex or cumbersome.

What ModelRight features can be programmed?

ModelRight provides your code with the capability to read and manipulate its model of the database itself (tables, columns, relationships etc), and its model of the diagrams of the database (particular layouts of table graphics and relationship graphics on a page; annotations; and formatting of these).

Programmability not currently available:

Accessing and navigating ModelRight's objects

To write useful code for ModelRight, you will need to become familiar with ModelRight's object model -- that is to say the objects that ModelRight exposes for your code to interact with. The general strategy is very similar to working with VBA to manipulate objects in Word or Excel, and in ModelRight goes something like this:

Exact details (or how to find out the exact details) are described in sections below. But first, a look at the user interface for programming, so that you can use it to explore ModelRight's objects.

ModelRight's programming UI  and "Hello world" example

Script Explorer

In the ModelRight application, you can browse, create or select scripts in the Script Explorer panel. If this is not visible, you can summon it from the main menu: Window > Script Explorer. Assuming you have a model file open, you should see something like the screenshot here.  To try this out, either start a new model, or make a copy of another model file, perhaps one of the samples that comes with ModelRight.

Script Explorer shows three or more top-level folders, which entail three different kinds of scripts:

Creating and saving a script

Create a new script by right-clicking on the User Defined item. You can name the new script item that appears. 

Script Editor

As you select (or create) scripts in the Script Explorer, the text of the script appears in an editor in the Property Browser panel. (To open: Window > Property Browser).  To get started, here's a script that can be pasted into the script editor:

'--------------------------
Sub Evaluate_OnLoad()
'--------------------------
  Set Context = CreateObject("SCF.ScriptContext")
  Set Doc = Context.ScriptDocument

  Doc.WriteLine("Hello!")
  Doc.WriteLine("VBS version: " & ScriptEngineMajorVersion & _
                "." & ScriptEngineMinorVersion)
  Doc.WriteLine("MyFunc: " & MyFunc)
end sub

'--------------------------
function MyFunc
'--------------------------
  MyFunc = "Hi from MyFunc"
end function

The screencap below shows some of the key features of the Script editor panel.

Main points:

Pressing the "Execute the script" button also causes the display to change like this:

Here you see:

Tip: With buttons and other features that address two different scripting languages and stages there's room for some confusion. Be sure to identify which features are for running VBScript, and which are for running SQL.


Document Status
Date (reverse) Who Description
     
2009-04-01 to 05-04 GW Revisions
2008-07-10 GW Original