Back

Topic

[KB306]SCADA Basic function execution from VBA

Tags: Scada Basic, Scripting, VBA

14 years ago
By RM
Options
Print
Applies to:

PcVue all versions.


Summary:

By forcing a variable in VBA code you can trigger a SCADA Basic function.

By forcing a variable in SCADA Basic you can trigger a VBA procedure.


Details:

Example 1:  Trigger a SCADA Basic function using a VBA procedure.

The process is that the VBA procedure is used to force the value of a text variable which triggers the SCADA Basic function using an event. The text variable contents is used to define the SCADA Basic program parameters.

‘*************************************************************
‘Called by event on variable @GENERAL.SCADA_BASIC.FROM_VBA
‘Launch the scada basic function according the message.
‘Format: “PROGRAM,PROCEDURE,BRANCH,ARG”
‘*************************************************************

Sub FromVBA()

  Dim sMessage as str;
  Dim hbuf as long;
  Dim Prog as str;
  Dim Proc as str;
  Dim Br as str;
  Dim Arg as str;

  sMessage = @GENERAL.SCADA_BASIC.FROM_VBA;

  if (!cmpstring(sMessage,””)) then
    return(0);
  end if

  hbuf = alloc_buffer(1000);
  put_buffer(hbuf,0,sMessage);
  seq_buffer(“BEGIN”,hbuf);
  Prog = seq_buffer(“NEXTFIELD”,hbuf,”,”,”STR”);
  Proc = seq_buffer(“NEXTFIELD”,hbuf,”,”,”STR”);
  Br = seq_buffer (“NEXTFIELD”,hbuf,”,”,”STR”);
  Arg = seq_buffer(“NEXTFIELD”,hbuf,”,”,”STR”);
  free_buffer(hbuf);

  print(“Init(FromVBA)- Prog:”,Prog,” -Proc:”,Proc,” -Br:”,Br,” -Arg:”,Arg);

  program(“FUNCTION”,Prog,Br,Proc,Arg);

  @GENERAL.SCADA_BASIC.FROM_VBA = “”;

End Sub


Example 2:  Trigger a VBA procedure from SCADA Basic.

The process is that SCADA Basic is used to force the value of a variable which triggers a VBA value change event. (Variable must be subscribed.)

‘Variable declaration

Private WithEvents vFromScadaBasic As Variable
Private Sub fvProject_StartupComplete()

  ‘Database variables initialization
  [GENERAL.SCADA_BASIC.TO_VBA] = “”

   ‘Variable subscription
  Set vFromScadaBasic = Variables(“GENERAL.SCADA_BASIC.TO_VBA”)
  vFromScadaBasic.EnableEvents = True

End Sub

 ‘———————————————–

‘Function : vFromScadaBasic_ValueChange
‘Scope : Called when the variable GENERAL.SCADA_BASIC.TO_VBA value change.
‘This variable is used to run a procedure. The order is coming from Scada Basic.
‘Format: ActionType$Arguments (arguments is optional)
‘Parameters :

‘———————————————–

Private Sub vFromScadaBasic_ValueChange()

  Dim vtArrayTmp As Variant
  Dim sTmp    As String
  Dim iPosN   As Integer

  If [GENERAL.SCADA_BASIC.TO_VBA] = “” Then Exit Sub

  vtArrayTmp = Split(vFromScadaBasic.Value, “$”)

  Select Case UCase(vtArrayTmp(0))    ‘ActionType
    Case “OPEN_EXPLORER”            ‘F2 key or click on toolbar icon
    Case “PARAMETERS”               ‘F11 key or click on toolbar icon
    Case “OPEN_GROUPMIMIC”          ‘Click on a Group mimic shortcut
    sTmp = vtArrayTmp(1)
    MOD_Navigation.OpenGroupMimicByShortcut sTmp
  End Select

  [GENERAL.SCADA_BASIC.TO_VBA] = “”

  On Error GoTo 0
  Exit Sub

 End Sub


Created on: 23 Dec 2010 Last update: 13 May 2024