<!-- Softwell WinCC VBScript 20-Part Tutorial · Blog 04 · updated 2026-08-29 --> IF, ElseIf & Select Case in WinCC VBScript
Blog 04 · WinCC VBScript Tutorial · SQF Running Project

IF, ElseIf & Select Case in WinCC VBScript

Turn SQF process values into decisions. This chapter teaches the control-flow statements used for temperature status, permissives, event mapping and operator feedback before any SQL insert is attempted.

Blog 04 Learning Goal

Series: WinCC VBScript 20-Part TutorialRunning DB: SQF_DB.dbo.tblEventDifficulty: Beginner → Intermediate

Outcome

Choose between If/ElseIf and Select Case, combine process conditions with And/Or/Not, and write readable SQF status/event logic.

Learning Foundation: Decision Logic

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.

BeginnerWinCC Explorer / Classic WinCCSQF Running Project
Prerequisite

Variables, comparisons and data conversion should already be clear.

Core concept

Decision structures route process values into the correct operator message, command, alarm state or sequence branch.

SQF practical connection

Evaluate furnace temperature, fan status, process event and operating mode using If/ElseIf and Select Case.

Expected competency

Build readable branch logic with If, ElseIf, Else, And/Or/Not and Select Case without creating unsafe command logic.

IF, ElseIf & Select Case in WinCC VBScript — ArchitectureCode-rendered HTML/CSS architecture; no image file required
Process Value

Read/convert the working value

TempAct
Condition

Evaluate engineering rule

TempAct >= TempSet
Branch

Choose the matching path

If / ElseIf / Else
Action

Set message/status/request

Status = "READY"
Verify

Trace the selected branch

MsgBox / Trace

Complete WinCC VBScript Learning Path

Use Previous/Next for the recommended practical order, or open any topic below as a reference.

WinCC VB Scripting Training →
Project context: The examples assume the SQF process variables already contain current values. This chapter focuses only on VBScript decision logic; WinCC tag Read/Write is covered in Blog 07.

1. IF...THEN

Use a simple If when an action is required only when one condition is true.

If TempAct >= TempSet Then
    EventTo = "TEMPERATURE ACHIEVED"
End If

2. IF...ELSE

Use Else when exactly two outcomes are required.

If FanStatus Then
    EventTo = "FAN RUNNING"
Else
    EventTo = "FAN STOPPED"
End If

3. IF...ELSEIF...ELSE

Use ElseIf for multiple ordered ranges.

If TempAct < TempSet - 20 Then
    EventTo = "LOW TEMPERATURE"
ElseIf TempAct <= TempSet + 10 Then
    EventTo = "TEMPERATURE NORMAL"
Else
    EventTo = "HIGH TEMPERATURE"
End If

4. Nested IF

A nested If is useful when a secondary check only matters after a primary condition is satisfied.

If TempAct >= TempSet Then
    If FanStatus Then
        EventTo = "PROCESS READY"
    Else
        EventTo = "TEMP READY - FAN STOPPED"
    End If
Else
    EventTo = "TEMPERATURE NOT READY"
End If
Do not over-nest conditions. When logic becomes difficult to read, split it into a Function in Blog 06.

5. AND — All Conditions Must Be True

Industrial permissives often require multiple conditions at the same time.

If TempAct >= TempSet _
And CpAct >= CpSet _
And OilAct >= OilSet _
And JacketAct >= JacketSet _
And FanStatus Then

    EventTo = "PROCESS READY"
Else
    EventTo = "PROCESS NOT READY"
End If

6. OR and NOT

Use Or when any listed condition may trigger the branch. Use Not to invert a Boolean condition.

If TempAct > TempSet + 20 Or OilAct < OilSet Then
    EventTo = "PROCESS DEVIATION"
End If

If Not FanStatus Then
    EventTo = "FAN NOT RUNNING"
End If

7. Select Case

Use Select Case when one variable has several discrete states.

Select Case SQFNo
    Case 1
        EventFrom = "SQF-01"
    Case 2
        EventFrom = "SQF-02"
    Case 3
        EventFrom = "SQF-03"
    Case Else
        EventFrom = "UNKNOWN SQF"
End Select

8. Select Case with Ranges

Case expressions can make range-based status logic easier to scan than a long chain of ElseIf statements.

Dim TempDeviation

TempDeviation = TempAct - TempSet

Select Case TempDeviation
    Case Is < -20
        EventTo = "LOW TEMPERATURE"
    Case -20 To 10
        EventTo = "TEMPERATURE NORMAL"
    Case Else
        EventTo = "HIGH TEMPERATURE"
End Select

9. Event_From → Event_To Mapping

A true transition requires both the previous state and the current state. Do not infer Event_From from FanStatus alone.

If PreviousFanStatus <> FanStatus Then

    If PreviousFanStatus = False And FanStatus = True Then
        EventFrom = "FAN STOP"
        EventTo = "FAN START"

    ElseIf PreviousFanStatus = True And FanStatus = False Then
        EventFrom = "FAN START"
        EventTo = "FAN STOP"
    End If

End If

10. Decision Logic Syntax Cheat Sheet

Use this quick reference to choose the correct VBScript decision structure in WinCC.

Keyword / StructureUseCore Syntax
If...ThenRun code only when one condition is trueIf condition Then ... End If
If...ElseChoose between two outcomesIf condition Then ... Else ... End If
If...ElseIf...ElseEvaluate ordered multiple conditionsIf c1 Then ... ElseIf c2 Then ... Else ... End If
Nested IfRun a secondary decision only after a primary conditionIf c1 Then If c2 Then ... End If End If
AndRequire every condition to be trueIf c1 And c2 Then
OrAllow any one condition to be trueIf c1 Or c2 Then
NotInvert a Boolean conditionIf Not FanStatus Then
Select CaseMatch one expression to several valuesSelect Case x ... Case 1 ... Case Else ... End Select
Case Is / ToMatch ranges or comparison expressionsCase Is < -20 / Case -20 To 10

11. Participant Exercises

Complete these before loops and arrays.

Create Low/Normal/High Temp logic.
Create a four-condition process permissive using And.
Use Or for two process-deviation conditions.
Use Not with FanStatus.
Create Select Case for SQF 1, 2, 3.
Map Event_From and Event_To for a fan transition.

SQF Running Project Lab · Blog 04

Build the SQF process-ready decision

If TempAct >= TempSet _
And CpAct >= CpSet _
And OilAct >= OilSet _
And JacketAct >= JacketSet _
And FanStatus Then

    EventFrom = "PROCESS CHECK"
    EventTo = "PROCESS READY"
Else
    EventFrom = "PROCESS CHECK"
    EventTo = "PROCESS NOT READY"
End If
This decision later controls whether the SQF event is approved for logging/reporting.

Trainer Checkpoint

Participant should explain this chapter without copying the final program.

FAQs

Why does this tutorial use SQF_DB.dbo.tblEvent?

One stable industrial dataset lets participants learn VBScript progressively without changing examples in every chapter.

Should participants memorize the complete SQL logger now?

No. Learn the current chapter first. The complete WinCC-to-SQL program is assembled in later chapters.

Are HMIRuntime and ADODB VBScript keywords?

No. They are runtime/COM objects used by VBScript. The language keywords control how the script is structured.

Continue the WinCC VBScript 20-Part Tutorial

Use the same SQF process variables in the next chapter.

Next: Blog 05
Verified learning pathway

Discuss WinCC VB Scripting Training

Explore practical WinCC VBS, SCADA, SQL reporting and industrial automation training options.

Content reviewed: 28 August 2026

☎ Call WhatsApp ✉ Email Enquire Now