Writing custom scripts for the Scheduler

The Scheduler provides Script Authoring Experts that generate a script that is executed when an entry triggers. These Experts are available for common tasks such as toggling a digital point or generating a report. You can also create custom scripts for tasks that you need to perform.

This section shows sample code that may be helpful when writing custom scripts for use with the Scheduler. For additional examples, use the VBE to view the code for the Experts, buttons, and forms supplied with DeltaV Operate. Refer to Writing Scripts for detailed information on writing VBA scripts.

Script Authoring Expert Sample Code

The following properties store information that is passed between a Script Authoring Expert, the Scheduler, and DeltaV Operate:

  • ProcedureObject.

  • EventProcedure.

  • Property1 through Property10.

Only the following two properties have a specific purpose:

  • ProcedureObject – the Timer or Event entry to which the script will be attached. X For example, Event5 or Timer 3.

  • EventProcedure – the name of the event type that will trigger the script. For example, OnTrue.

Property1 through Property10 are available to the Expert for operational parameters specific to the Expert. Initially, these parameters are passed from the Expert to the Scheduler, and do not appear in the Scheduler until after the script has been generated. For example, if the user re-enters the Expert, the Scheduler passes them back to the Expert, which can then use them to populate a form.

The following example shows the code behind the Toggle Digital Point Script Authoring Expert.

Private Sub ToggleDigitalPointWizard_Click()
    Dim CurrentObj As Object
    Dim szEventName As String
    
    On Error GoTo ErrorHandler
    
    Application.DeActivateWorkspaceUI True, False

'Prevent this button from being used in a Picture document 
    If InStr(1, Application.ActiveDocument.Type, "FIXSchedule",_
    vbTextCompare) = 0 Then
        Application.ActivateWorkspaceUI
        MsgBox "This button is not for use.  Use the button _
        from the Command Tasks category."
        Exit Sub
    End If

'Pass the timer or event object's ProcedureObject and 
'EventProceduure to the form 
    Set CurrentObj = ToggleDigitalPointWizard.ProcedureObject
    szEventName = ToggleDigitalPointWizard.EventProcedure

'The database tag is in Property1.  Enter the tag in the 
'form field 
frmToggleDigitalPointCommand.ExpressionEditor1.EditText = 
ToggleDigitalPointWizard.Property1

'Call a subroutine in the form that enters the ProcedureObject 
'and EventProcedure as global variables so that the script in 
'the form can act on them 
    Call 
frmToggleDigitalPointCommand.GetSchedulerValues(CurrentObj, 
szEventName)
    frmToggleDigitalPointCommand.bCancel = False

'Open the form 
    frmToggleDigitalPointCommand.Show
    
'Pass the database tag back to the scheduler 
    If frmToggleDigitalPointCommand.bCancel = False Then
        ToggleDigitalPointWizard.Property1 = 
frmToggleDigitalPointCommand.ExpressionEditor1.EditText
    End If

    Application.ActivateWorkspaceUI
    Exit Sub
    
ErrorHandler:
    HandleError

End Sub

The following example shows the code behind the Generate Report Script Authoring Expert.

Private Sub GenerateReportWizard_Click()
    Dim CurrentObj As Object
    Dim szEventName As String
    
    On Error GoTo ErrorHandler

    Application.DeActivateWorkspaceUI True, False
    
'Prevent this button from being used in a Picture document 
    If InStr(1, Application.ActiveDocument.Type, "FIXSchedule", 
vbTextCompare) = 0 Then
        Application.ActivateWorkspaceUI
        MsgBox "This button is not for use.  Use the button _
        from the Command Tasks category."
        Exit Sub
    End If
    
'Pass the timer or event object's ProcedureObject and 
'EventProceduure to the form 
    Set CurrentObj = GenerateReportWizard.ProcedureObject
    szEventName = GenerateReportWizard.EventProcedure

'The Crystal Report name is in Property1.  Enter the report name 
'in the form field 
    frmGenerateReportCommand.txtReport.Text = 
GenerateReportWizard.Property1

'Call a subroutine in the form that enters the ProcedureObject 
'and EventProcedure as global variables so that the script in 
'the form can act on them 
    Call frmGenerateReportCommand.GetSchedulerValues_
    (CurrentObj, szEventName)
    frmGenerateReportCommand.bCancel = False

'Open the form 
    frmGenerateReportCommand.Show
    
'Pass the Crystal Report name back to the scheduler 
    If frmGenerateReportCommand.bCancel = False Then
        GenerateReportWizard.Property1 = 
frmGenerateReportCommand.txtReport
    End If

    Application.ActivateWorkspaceUI
    Exit Sub
    
ErrorHandler:
    HandleError

End Sub

Form Script Sample Code

The following example shows code that collects the name of the picture the user wants to display when the event is triggered, generates the script, and creates the event. The Procedures object is a collection of scripts. Before you generate the script, you need to get the index number for the next script using the GetEventHandlerIndex method. Once you generate the string that will become the script, use the AddEventHandler method to add the event.

Private CurrentObject As Object
Private EventName As String

Public Sub GetCurrentObjectValues(obj, Str)
    Set CurrentObject = obj
    EventName = Str
End Sub

Private Sub CommandButton1_Click()

'get file name entered by user 
Dim FileName As String
FileName = TextBox1.Value

Dim WorkingObj As Object
Set WorkingObj = CurrentObject.Procedures

'Get the index for the procedure 
Dim Index As Long
Dim Found As Long
WorkingObj.GetEventHandlerIndex EventName, Index, Found

'Generate code 
Dim StringCode As String
StringCode = "Dim doc As Object" & Chr(13) _
                & "Dim PicturePath As String" & Chr(13) _
                & "PicturePath = ""c:\fix32\pic\"" & FileName" & _
                Chr(13) & "Set doc = _ 
                Application.Documents.Open(PicturePath)" & Chr(13)

'Add the event 
WorkingObj.AddEventHandler EventName, StringCode, Index

'Close the form 
End

End Sub

Button Script Sample Code

The following example shows code for a button object called MyExpert that gets the timer or event object, the event trigger name, and picture name, if any, from the Add Event dialog box. It passes this information in to the form using the GetCurrentObjectValues procedure, and then displays the form.

Private Sub MyExpert_Click()

    Dim CurrentObj As Object
    Dim EventName As String

    'Set procedure object, event name, and file name 
    Set CurrentObj = MyExpert.ProcedureObject
    EventName = MyExpert.EventProcedure
    MyExpert.Property1 = frmOpenPicture.TextBox1.Value

    'Pass the object and event name into the form 
    Call frmOpenPicture.GetCurrentObjectValues(CurrentObj,_
    EventName)
    frmOpenPicture.Show
    frmOpenPicture.Hide
End Sub

Generated Script Sample Code

The following example shows the script that would be generated if you ran this Expert on the Timer7 object, using the OnTimeOut event.

Private Sub Timer7_OnTimeOut(ByVal lTimerId As Long)
Dim doc As Object
Dim PicturePath As String
PicturePath = "c:\fix32\pic\" & FileName
Set doc = Application.Documents.Open(PicturePath)
End Sub