Learning Foundation: VBScript Language Foundation
This page has one defined job in the WinCC VBScript learning path. Master this foundation before using the same concept inside larger SCADA, SQL Server and reporting scripts.
WinCC project can open in Graphics Designer and Runtime. Start with internal tags for safe testing.
Learn the syntax vocabulary used everywhere else: comments, Dim, Set, If, For, Sub, Function, object references, dot notation and runtime event procedures.
Create simple SQF test variables and message-box checks before reading live process tags.
Read a WinCC VBScript procedure line by line and identify keywords, objects, methods, arguments and event entry points.
WinCC calls your script from an event
Declare and control execution
Reference Runtime components
Perform actions on objects
Verify with safe test output
Complete WinCC VBScript Learning Path
Use Previous/Next for the recommended practical order, or open any topic below as a reference.
This page is a VBScript keyword reference. It intentionally uses only short syntax fragments. Full programs belong in the later chapters on conditions, loops, procedures, tag Read/Write, SQL and practical WinCC examples.
1. What Is a VBScript Keyword?
A VBScript keyword is a reserved word that forms part of the language grammar. Keywords define how a script declares data, makes decisions, repeats instructions, creates procedures, works with object references and handles runtime errors.
Dim, If, For, SubPart of VBScript grammar.
CStr(), CDbl(), IsNumeric()Performs an operation and is not a keyword.
HMIRuntime, Tags, Read, WriteProvided by the WinCC object model.
2. WinCC VBScript Keywords — Master Cheat Sheet
This is the main quick-reference table for the chapter.
| Category | Important keywords | Primary use in WinCC |
|---|---|---|
| Script discipline | Option Explicit | Force variables to be declared and reduce typing mistakes. |
| Declaration | Dim, Const, ReDim, Preserve | Store tag values, setpoints, status text, arrays and temporary data. |
| Object references | Set, Nothing | Work with WinCC objects, ADODB objects, Excel objects and file-system objects. |
| Decision | If, Then, ElseIf, Else, End If | Interlocks, alarm conditions, validation and status logic. |
| Multi-state decision | Select Case, Case, Case Else, End Select | Mode selection, equipment states and event codes. |
| Counted loop | For, To, Step, Next | Repeat operations across arrays, tags or fixed indexes. |
| Collection loop | For Each, In, Next | Process each item in an array or collection. |
| Conditional loop | Do, While, Until, Loop | Recordset iteration and condition-controlled repetition. |
| Loop exit | Exit For, Exit Do | Stop repetition when a limit, fault or target condition is reached. |
| Procedures | Sub, End Sub, Function, End Function, Call | Create reusable WinCC script logic. |
| Parameters | ByVal, ByRef | Control whether procedures receive a copy or can modify the caller variable. |
| Logic | And, Or, Not, Xor | Combine interlocks, permissives and alarm conditions. |
| Special values | Empty, Null, Nothing, True, False | Check initialization, database nulls, object references and Boolean states. |
| Error handling | On Error Resume Next, On Error GoTo 0 | Handle recoverable runtime errors around SQL, files, Excel or object access. |
3. Declaration & Object Keywords
These keywords prepare the data and object references used by the rest of the script.
Option Explicit
Use: Requires every variable to be declared. Application: Recommended for WinCC Global Scripts and larger VBS projects because misspelled variable names become easier to detect.
Option Explicit
Dim TempActDim
Use: Declares a variable or fixed-size array. Application: Store process values, setpoints, strings, counters and temporary calculation results.
Dim TempAct
Dim ProcessName(3)
Dim TempAct As Double.Const
Use: Declares a named value that should not change. Application: Fixed limits, status codes or configuration values.
Const HighTempLimit = 900ReDim and Preserve
Use: Resize a dynamic array; Preserve keeps its current elements. Application: Build variable-length event or report lists.
Dim Events()
ReDim Events(1)
ReDim Preserve Events(2)Set and Nothing
Use: Set assigns an object reference; Nothing clears it. Application: WinCC tag objects, ADODB connections/recordsets, Excel automation and file-system objects.
Dim TagObj
Set TagObj = HMIRuntime.Tags("Temp_Act")
' ...use TagObj...
Set TagObj = Nothing
Set for objects, not for ordinary numbers or strings.4. Decision Keywords
Decision keywords control which statements execute according to process conditions, operator commands or equipment states.
| Keyword | Use | WinCC application |
|---|---|---|
If ... Then | Test one Boolean condition. | Check alarm, permissive or command condition. |
ElseIf | Test another condition. | Classify a process value into ranges. |
Else | Fallback path. | Default status when no earlier condition is true. |
End If | Close the If block. | Required for multiline decisions. |
Select Case | Evaluate one expression against multiple cases. | Mode, state, step or event-code handling. |
Case Else | Fallback case. | Unknown or invalid mode/state. |
Syntax: If / ElseIf / Else
If TempAct > 900 Then
StatusText = "HIGH"
ElseIf TempAct >= 800 Then
StatusText = "NORMAL"
Else
StatusText = "LOW"
End If
Syntax: Select Case
Select Case Mode
Case 0
ModeText = "STOP"
Case 1
ModeText = "MANUAL"
Case 2
ModeText = "AUTO"
Case Else
ModeText = "UNKNOWN"
End Select
5. Loop Keywords
Loops reduce repetitive statements. In WinCC they are useful for arrays, repeated tag operations, report rows and SQL Recordset processing.
| Pattern | Best use | Core keywords |
|---|---|---|
| Counted loop | Known number of repetitions. | For, To, Step, Next |
| Array / collection loop | Process every item. | For Each, In, Next |
| Condition loop | Repeat while/until a condition changes. | Do, While, Until, Loop |
| Early exit | Stop loop on fault/target. | Exit For, Exit Do |
Syntax: For ... Next
For i = 0 To 3
HMIRuntime.Trace ProcessName(i) & vbCrLf
Next
Syntax: For Each
For Each TagName In TagNames
HMIRuntime.Trace CStr(TagName) & vbCrLf
Next
Syntax: Do While
Do While Not rs.EOF
' Process current SQL row
rs.MoveNext
Loop
6. Procedure Keywords
Procedures make scripts reusable and easier to maintain. Use a Sub for an action and a Function when a value must be returned.
| Keyword | Purpose | Typical WinCC use |
|---|---|---|
Sub ... End Sub | Create a procedure that does not return a value. | Write tags, update status, perform an action. |
Function ... End Function | Create a procedure that returns a value. | Validation, scaling, formatting or reusable calculations. |
Call | Explicitly invoke a procedure. | Call reusable routines. |
ByVal | Pass a copy of an argument. | Protect input parameters from modification. |
ByRef | Pass a reference to the caller variable. | Allow a procedure to update the caller variable intentionally. |
Exit Sub / Exit Function | Leave a procedure early. | Stop execution after validation or interlock failure. |
Syntax: Sub
Sub TraceValue(ByVal NameText, ByVal ValueText)
HMIRuntime.Trace NameText & " = " & CStr(ValueText) & vbCrLf
End Sub
Syntax: Function
Function IsHigh(ByVal Value)
IsHigh = (Value >= 900)
End Function
7. Logical & Special-Value Keywords
Logical keywords
| Keyword | Meaning | Example application |
|---|---|---|
And | All conditions must be true. | Start command AND all permissives OK. |
Or | At least one condition is true. | Any trip source generates a common fault. |
Not | Invert a Boolean condition. | Run only when fault is NOT active. |
Xor | True when conditions differ. | Less common; useful for mutually exclusive states. |
If StartCmd And InterlockOK And Not FaultActive Then
PermitStart = True
End If
Special-value keywords
| Keyword | Meaning | Typical check |
|---|---|---|
True / False | Boolean values. | Command, alarm or interlock state. |
Empty | Variant has not yet been initialized. | Check an unassigned variable. |
Null | Contains no valid data value. | Common when reading database fields. |
Nothing | No object reference is assigned. | Release or test object variables. |
8. Error-Handling Keywords
VBScript does not use Try...Catch. WinCC scripts commonly use the On Error statements around operations that may fail, then inspect the built-in Err object.
Syntax
On Error Resume Next
' Operation that may fail
If Err.Number <> 0 Then
HMIRuntime.Trace "Error " & CStr(Err.Number) & ": " & Err.Description & vbCrLf
Err.Clear
End If
On Error GoTo 0
On Error Resume Next to hide errors without checking Err.Number.9. Keywords vs Functions vs WinCC Objects
This distinction is important because not every familiar VBScript-looking word is a keyword.
| Example | Type | Role |
|---|---|---|
Dim, If, For, Sub | VBScript keywords | Define language structure. |
CStr(), CDbl(), IsNumeric(), Replace() | Built-in functions | Convert, test or manipulate values. |
vbCrLf, vbInformation | Built-in constants | Provide predefined values. |
HMIRuntime | WinCC Runtime object | Entry point to WinCC Runtime services. |
HMIRuntime.Tags("Temp_Act") | WinCC object access | Obtain a WinCC tag object. |
.Read, .Write, .Trace | WinCC methods | Perform WinCC-specific actions. |
10. WinCC VBScript Keyword Application Map
Use this map to decide which keyword group is relevant before writing a script.
| WinCC task | Most useful keywords | Next detailed chapter |
|---|---|---|
| Declare process variables | Option Explicit, Dim, Const | Variables & Data Conversion |
| Alarm / limit decision | If, ElseIf, Else | If, ElseIf & Select Case |
| Mode / state selection | Select Case, Case | Decision Logic |
| Process arrays / repeated tags | For, For Each, Do While | Loops & Arrays |
| Create reusable logic | Sub, Function, ByVal, ByRef | Sub & Function |
| Read / write WinCC tags | Dim, Set, decisions as required | HMIRuntime Tag Read/Write |
| SQL / Excel / file automation | Set, loops, On Error, Nothing | SQL Server CRUD |
SQF Project Keyword Recognition
The purpose here is only to identify keyword roles inside a typical WinCC fragment—not to introduce a full project program.
On Error Resume Next
Dim TempAct
Dim TagObj
Set TagObj = HMIRuntime.Tags("Temp_Act")
TempAct = TagObj.Read
If TempAct >= 900 Then
HMIRuntime.Trace "High temperature" & vbCrLf
End If
Set TagObj = Nothing
On Error GoTo 0
On Error, Resume Next, Dim, Set, If, Then, End If, Nothing and GoTo 0. HMIRuntime, Tags, Read and Trace are WinCC object-model items, not VBScript keywords.Frequently Asked Questions
What are the most important VBScript keywords for WinCC beginners?
Start with Option Explicit, Dim, Const, Set, If, ElseIf, Else, Select Case, For, Do, Sub, Function, And, Or, Not, Nothing and the On Error statements.
Is HMIRuntime a VBScript keyword?
No. HMIRuntime is a WinCC Runtime object. VBScript keywords define the language structure around it.
When should Set be used?
Use Set when assigning an object reference. Do not use it for ordinary numeric or string values.
Which keywords are best for WinCC mode or state logic?
Select Case is usually clear when one variable represents several modes or states. If...ElseIf...Else is better when conditions are based on ranges or multiple Boolean expressions.
Which keywords are used for SQL Recordset iteration?
A common pattern is Do While Not rs.EOF ... rs.MoveNext ... Loop. The detailed ADO and SQL implementation is covered in the SQL chapters.
Next: variables, operators and data conversion
After the keyword grammar is clear, continue to how VBScript stores, compares and converts WinCC process data.
