Using a Script to Create a Form
A common requirement for scripts is to create a Form that is displayed when a Mimic item is selected.
Example:
In this example, we will describe the script that creates the following Form:
The Form contains a Yes/No combo box, a text entry field, and OK and Cancel buttons. The text entry field is for a password, so the entry has to match the specified password. This type of Form could be used to shutdown equipment and so on.
By examining the script that is used to create the Verify Control Action Form, you can learn how to add fields, buttons, and combo boxes to Forms. The script for the Form is:
- Option Explicit
- Public Function VerifyControl(Point)
- Dim frmComboBox
- Dim frmEditBox
- Dim frmOKButton
- Dim frmCancelButton
- Dim sMasterPassword
- Dim iReturnValue
- Dim oClearSCADAPoint
- sMasterPassword = "abc"
- Form.Init "Verify Control Action"
- Form.AddGroupBox 0,0,100,8,""
- Form.AddStaticText 2,1, "Are you sure you wish to execute this control?"
- Form.AddStaticText 2,3, "Enter master password"
- Set frmComboBox = Form.AddComboBox(60,1)
- frmComboBox.Add("Yes")
- frmComboBox.Add("No")
- frmComboBox.Selection = 1 '0 is first element, 1 is second, etc.
- Set frmEditBox = Form.AddEditBox(60,3)
- frmEditBox.Password = True
- frmEditBox.MaxLength = 10
- frmEditBox.Value = ""
- Set frmOKButton = Form.AddPushButton(60,5,"OK")
- frmOKButton.Default = True
- Set frmCancelButton = Form.AddPushButton(75,5,"Cancel")
- Form.Show
- If Form.Selection = "OK" Then
- If frmComboBox.Selection <> 0 Then
- iReturnValue = MsgBox("Select ""Yes"" from the pulldown menu for this action to be executed", vbOKOnly+vbExclamation, "Action not executed")
- ElseIf frmEditBox.Value <> sMasterPassword Then
- iReturnValue = MsgBox("Incorrect password specified", vbOKOnly+vbExclamation, "Action not executed")
- Else
- Set oClearSCADAPoint = Server.FindObject(Point)
- If oClearSCADAPoint Is Nothing Then
- iReturnValue = MsgBox("Unable to find the object", vbOKOnly+vbExclamation, "Action not executed")
- Else
- oClearSCADAPoint.Interface.Override oClearSCADAPoint.Interface.CurrentValue+5
- iReturnValue = MsgBox("Action successfully performed",vbOKOnly+vbInformation, "Action performed successfully ")
- End If
- If Form.Selection = "OK" Then
- End If
- ElseIf Form.Selection = "" Then'This is when they click Esc or the X, allows them to get some feedback.
- 'We don't give feedback when they click Cancel
- iReturnValue = MsgBox("Action canceled", vbOKOnly+vbInformation, "Action not executed")
- End If
- End Function
We will now explain each section of the script so that you can understand how the script provides the various features of the Form. You can then use the same scripting principles to create your own Forms.
The script begins with Option Explicit which means the script has to define each function before it uses the functions. The name of the script function is defined as VerifyControl and (Point) defines the variable that represents the FullName value that is entered on the Pick Action Wizard (to define which database item is to be controlled).
- Option Explicit
- Public Function VerifyControl(Point)
The variables that are used by the script are defined next to Dim entries. The frm is used as the prefix for Form variables, s is the prefix for the string variable, i is the prefix for an integer variable, and o is the prefix for a database item definition. These prefixes are not compulsory, but they are useful as they allow engineers to determine the type of each variable without having to investigate.
- Dim frmComboBox
- Dim frmEditBox
- Dim frmOKButton
- Dim frmCancelButton
- Dim sMasterPassword
- Dim iReturnValue
- Dim oClearSCADAPoint
The sMasterPassword = "abc" line defines the sMasterPassword variable as being "abc". This means that the user has to enter abc as the password.
- sMasterPassword = "abc"
The settings that define the appearance of the Verify Control Action Form are defined by the following lines:
- Form.Init "Verify Control Action"
- Form.AddGroupBox 0,0,100,8,""
- Form.AddStaticText 2,1, "Are you sure you wish to execute this control?"
- Form.AddStaticText 2,3, "Enter master password"
- Set frmComboBox = Form.AddComboBox(60,1)
- frmComboBox.Add("Yes")
- frmComboBox.Add("No")
- frmComboBox.Selection = 1 '0 is first element, 1 is second, etc.
The Form.Init "Verify Control Action" sets the title of the Form. The Form.AddGroupBox 0,0,100,8, "" line defines the appearance and position of the section border that is shown within the Form. The coordinates start at the top-left corner and end at the bottom right-hand corner.
The Form.AddStaticText entries define the position and text for the text labels that appear on the Form.
- Set frmComboBox = Form.AddComboBox(60,1)
- frmComboBox.Add("Yes")
- frmComboBox.Add("No")
- frmComboBox.Selection = 1 '0 is first element, 1 is second, etc.
The Set frmComboBox = Form.AddComboBox (60,1) line defines the location of a combo box on the Form.
The frmComboBox.Add lines define the options that are available in the combo box.
The frmComboBox.Selection = 1 '0 is first element, 1 is second, etc. line sets the numbers for the combo box options. 0 is the first option, 1 is the second option. The text after the ' is commented out.
The following script is used to define the settings for the password field:
- Set frmEditBox = Form.AddEditBox(60,3)
- frmEditBox.Password = True
- frmEditBox.MaxLength = 10
- frmEditBox.Value = ""
The Form.AddEditBox (60,3) section defines that the field is a text entry field (edit box) and has the coordinates 60, 3. (The coordinates define the field's position on the box).
The frmEditBox.Password = True line sets the password field to hide the text entry and replace the characters with asterisks. This means that the password cannot be seen by other users when an engineer types in the password.
The frmEditBox.MaxLength = 10 line sets the password length to 10 characters.
The frmEditBox.Value = "" line sets the password field to be empty by default.
The following script is used to define the OK and Cancel buttons:
- Set frmOKButton = Form.AddPushButton(60,5,"OK")
- frmOKButton.Default = True
- Set frmCancelButton = Form.AddPushButton(75,5,"Cancel")
The OK button is set to be highlighted by default (= True), so that if the user presses the Enter key, the OK button is selected.
If the user selects the OK button on the Form, ClearSCADA checks to see if the Yes option has been selected in the combo box. If it has not been selected, a dialog box is displayed. The message box displays the message 'Select "Yes" from the pulldown menu for this action to be executed'. The title of the dialog box is "Action not executed". This is defined by the following section of the script:
- Form.Show
- If Form.Selection = "OK" Then
- If frmComboBox.Selection <> 0 Then
- iReturnValue = MsgBox("Select ""Yes"" from the pulldown menu for this action to be executed", vbOKOnly+vbExclamation, "Action not executed")
- If frmComboBox.Selection <> 0 Then
- If Form.Selection = "OK" Then
If the user selects the OK button on the Form and the Yes option has been selected, ClearSCADA checks to see if the correct password has also been entered.
If the incorrect password has been entered, a dialog box is displayed containing the message "Incorrect password specified". The title of the dialog box is "Action not executed".
This is defined by the following section of the script:
- ElseIf frmEditBox.Value <> sMasterPassword Then
- iReturnValue = MsgBox("Incorrect password specified", vbOKOnly+vbExclamation, "Action not executed")
If the correct password is entered, but ClearSCADA cannot locate the database item, a dialog box is displayed. The dialog box contains the message "Unable to find the object". The title of the dialog box is "Action not executed".
This is defined by the following section of the script:
- Else
- Set oClearSCADAPoint = Server.FindObject(Point)
- If oClearSCADAPoint Is Nothing Then
- iReturnValue = MsgBox("Unable to find the object", vbOKOnly+vbExclamation, "Action not executed")
If the correct password has been entered, ClearSCADA attempts to locate the database item that is represented by the Point variable. The point that this variable represents is selected on the Pick Action Wizard when the script is set up as the Mimic item's pick action. The Pick Action Wizard has to be set to reference a FullName property of an object or another property that contains the fullname of the object. In this case, we will assume that the Pick Action Wizard is set to reference the FullName property of the point.
If the database item can be found, an Override method is performed and sets the value to 5. When the override has taken place, a dialog box is displayed containing the message "Action performed successfully".
This is defined by the following section of the script:
- Else
- oClearSCADAPoint.Interface.Override oClearSCADAPoint.Interface.CurrentValue+5
- iReturnValue = MsgBox("Action successfully performed",vbOKOnly+vbInformation, "Action performed successfully ")
- End If
If the user selects the Close window button (at the top corner of the Form) or presses the Esc key, the action is canceled. A dialog box is displayed containing the message "Action canceled", and it has the title "Action not executed". There is no dialog box message if the user selects the Cancel button.
This is defined by the following section of the script:
- ElseIf Form.Selection = "" Then'This is when they click Esc or the X, allows them to get some feedback.
- 'We don't give feedback when they click Cancel
- iReturnValue = MsgBox("Action canceled", vbOKOnly+vbInformation, "Action not executed")
- End If