VBScript scripts
The structure of a VBS script (Module and Action), supported language features, access to tags and screen objects, and the known differences from real VBScript.
Scope of the language
VBS scripts run in the product's own VBScript engine; Windows Script Host is not required. The engine has been tested side by side with the real Windows VBScript engine (operators, conversions, strings, dates, formatting, arrays, error behavior, loops, procedures, classes). The deliberately retained differences are listed at the end of this topic.
Supported structures
| Group | Structures |
|---|---|
| Declaration | Dim, ReDim (including Preserve), Erase, Const, Option Explicit, Public / Private |
| Condition | If … Then … ElseIf … Else … End If, Select Case (Case 1, 2, Case Else) |
| Cycle | For … Next (Step), For Each, Do … Loop (While / Until), While … Wend, Exit For / Exit Do |
| Procedure | Sub, Function, Call, ByVal / ByRef, Exit Sub / Exit Function |
| Object | Set, Nothing, Is, With, Class (Property Get / Let / Set, Class_Initialize) |
| Error | On Error Resume Next, On Error GoTo 0, Err object (Number, Description, Source, Clear, Raise) |
| Writing | Line continuation (_), several statements on the same line with :, ' and Rem comments, &h hexadecimal numbers, #tarih# constants |
Built-in functions
The Functions tab in the Reference panel gives the complete list. Groups:
- Conversion:
CStr,CInt,CLng,CDbl,CBool,CDate,Round,Hex - Math:
Abs,Sqr,Sin,Log,Rnd; operators\,Mod,^ - Text:
Len,Left,Mid,InStr,Replace,Split,Join,UCase,Trim,Chr,FormatNumber - Date and time:
Now,Timer,Year,Hour,Weekday,DateAdd,DateDiff,DateSerial - Type checks:
IsEmpty,IsNull,IsNumeric,IsArray,TypeName,VarType - Array:
Array,UBound,LBound,Filter - Interaction:
MsgBox,InputBox(a centered window that matches the theme),RGB,CreateObject
Constants: MsgBox constants such as vbCrLf, vbTab, vbYes / vbNo, vbYesNo, vbQuestion and predefined colors such as vbRed.
Objects that can be created with CreateObject
| ProgID | What for | Permission |
|---|---|---|
Scripting.Dictionary | Key → value list | Not required |
System.Collections.ArrayList | Growable list | Not required |
VBScript.RegExp | Regular expression (Pattern, Test, Execute, Replace) | Not required |
ADODB.Connection, ADODB.Recordset, ADODB.Command | Database query via ODBC | Database connection |
WScript.Shell | Starting a program (Run) | Program execution |
Scripting.FileSystemObject | File and folder operations | File access |
WScript.Network | Computer and Windows user name | Not required |
If a different ProgID is given, an error occurs at run time; Studio also warns about it during compilation.
Script types
Module
Holds the procedures (Sub, Function). The procedures of all VBS modules and the module-level variable / constant declarations share a common namespace: a procedure defined in one module is called by name from another module. For this reason the same procedure name cannot be used in two modules (Studio reports an error). In the same module, the second definition silently takes effect (Studio issues a warning).
If no procedure is selected in the event or task, the procedure with the same name as the module runs; to run a different one, select it from the Procedure list. Lines outside a function run once when the project is opened.
' T-101 şerbet tankı seviye denetimi (Tanıtım_Tesisi)
Const ALT_SINIR = 40
Const UST_SINIR = 88
Function Seviye_Durumu(seviye)
If seviye < ALT_SINIR Then
Seviye_Durumu = "DÜŞÜK"
ElseIf seviye > UST_SINIR Then
Seviye_Durumu = "YÜKSEK"
Else
Seviye_Durumu = "NORMAL"
End If
End Function
Sub Tank_Seviye_Denetimi()
Dim seviye, durum
seviye = SmartTags("T101_Seviye")
durum = Seviye_Durumu(seviye)
If durum = "NORMAL" Then
SmartTags("T101_Uyari") = 0
Else
SmartTags("T101_Uyari") = 1
End If
End Sub
Action
This is code that runs from the beginning on every call. If the code contains Function Action() (or Sub Action), that one runs; otherwise the whole code is regarded as the body and can be ended with Exit Sub. No procedure is selected in an action. Helper Function procedures in an action are visible only inside that action.
Option Explicit
Function Action()
If SmartTags("Tank1_Seviye") >= 80 Then
SmartTags("Tank1_Durum") = "Yüksek"
Else
SmartTags("Tank1_Durum") = "Normal"
End If
Action = 0
End Function
VBS code written in the event
In the Run VBS code function, the code is written directly into the event (it runs like an action body). Here, Item (the object of the event; the screen for a screen event), ScreenItems, Layers, DataSet and Parent (the screen of the event) are available.
' Tıklanan nesnenin rengini değiştirir
Item.BackColor = RGB(255, 0, 0)
How tag values appear in VBS
- A Bool tag arrives in VBS as a "binary" value:
= 1,= True,If x Then,Not xandAnd/Orall work.CIntreturns 1 or 0,CStrreturns"1"or"0", andTypeNamereturnsBoolean. When writing,True,1and every number other than zero are written as 1. - The logical properties of objects (
Visible,Enabled,FontBold…) are not tags; they return a realBoolean(True/False). - Converting numbers and dates to text follows the Windows regional settings of the Live computer: if the decimal separator is a comma,
CStr(1.5)becomes"1,5"andCDbl("1,5")becomes1.5. If a fixed format is required, useDay,Month,Year,Hour… and join the parts. - Quality: the value of
HMIRuntime.Tags("Ad").QualityCodemeans: 192 good, 84 tag outside its limits, 8 no connection. - A non-existent tag or a value that cannot be written does not stop the script. If a tag that is not in the project is read, the value returned is
Empty,LastErroris filled and the warningTag bulunamadı (okuma): Adis written to the event log; for writing,Tag bulunamadı (yazma): Adis written. A value that cannot be converted to the type of the tag (e.g. text written to a numeric tag) is written to the log in the same way, and the tag does not change. To catch these cases, check the values ofTag.LastError(0 = no error) andTag.QualityCode. - Tag names are not case-sensitive.
Error handling
Only On Error Resume Next and On Error GoTo 0 are supported. Errors are checked with Err.Number.
Sub Hata_Yakalama()
Dim sonuc
On Error Resume Next
sonuc = SmartTags("Toplam_Miktar") / SmartTags("Parti_Adedi") ' adet 0 ise hata 11
If Err.Number <> 0 Then
SmartTags("Hata_Metni") = "Hata " & Err.Number & ": " & Err.Description
sonuc = 0
Err.Clear
Else
SmartTags("Hata_Metni") = ""
End If
On Error GoTo 0
SmartTags("Parti_Ortalama") = sonuc
End Sub
An unhandled error stops the script and is written to the event log in the format Modül / Prosedür, satır N: ileti (hata numarası). The program keeps running.
Database, file and program
These objects depend on the Script permissions. The database connection is stateless: each command opens and closes its own connection; there are no transactions, no temporary #tablo tables and no updates through a recordset (AddNew, Update, Delete). Records are added and changed with conn.Execute "INSERT …". WScript.Shell.Run starts the program but does not wait for it to finish. Details: Database access.
Known differences from real VBScript
| Topic | FabyaVision |
|---|---|
Execute, ExecuteGlobal, Eval, GetObject, GetRef | Not available. If used, the script stops with an error such as 'Eval' bu sürümde desteklenmiyor. |
On Error GoTo etiket, Resume | Not available; produces a syntax error. |
| Number subtypes | Not available. Numbers are kept in a single type; TypeName returns a name based on the value (Integer for 5, Double for 1.5). CSng rounds to 7 significant digits, but no Single type is created. |
Class_Terminate | Not called (it cannot be known when the object is released). |
| Names with Turkish letters | Letters such as ş, ğ and ı are accepted in variable and procedure names (real VBScript accepts only ASCII letters). |
ByRef | Works by copying the value and writing it back. |
| Variables used without Dim | A name declared at module level is global to every procedure. If you use Option Explicit, an undefined name causes the error Değişken tanımlı değil. |
Limitations and points to note
- A call can compute for at most 30 seconds; only the time the code itself runs is counted (tag writes, database access and waiting for
MsgBoxare not counted). VBS has no wait function; aDo Whileloop that checks the time difference counts as the code's own time and is interrupted. For long waits, use a cyclic task. HMIRuntime.Alarms,AlarmLogs,DataLogs,LoggingandMenuToolBarConfigare not available in this version.- For the same task or event row, a call that arrives while the previous call is still running is skipped.
- Scripts do not run in monitoring mode from a browser or the TV.