<!-- 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.

WinCC VBScript · Beginner to Project Level
IF, ElseIf & Select Case in WinCC VBScript

Follow the chapters in sequence; every tutorial reuses the same SQF furnace project.

Running project: SQF_DB.dbo.tblEvent · SQF_No · ChargeNo · Event_From · Event_To · Temp_Set/Temp_Act · Cp_Set/Cp_Act · Oil_Set/Oil_Act · Jacket_Set/Jacket_Act · Fan_Status
View all 20 tutorial chapters

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.

Easy Testing Method: Every main code example on this page is followed by a copy-ready InputBox + MsgBox test. Use the dialog version first to understand the result, then move to the original WinCC/SQL code. Database writes and equipment commands are previewed or simulated unless the original example is already intended as a lab action.
Project context: Every example is connected to SQF_DB.dbo.tblEvent. The final script reads WinCC tags, validates/converts values, creates an ADO connection, inserts one row and writes SQL_Status back to WinCC.

1. IF...THEN

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

If TempAct >= TempSet Then
    HMIRuntime.Trace "Temperature achieved" & vbCrLf
End If

Easy TestEasy Test 1 — InputBox + MsgBox

Change the InputBox values to force different IF/ELSE outcomes.

Dim TempSet, TempAct, FanInput, FanStatus, ResultText
TempSet = CDbl(InputBox("Enter Temp_Set", "Easy Test 1 — InputBox + MsgBox", "850"))
TempAct = CDbl(InputBox("Enter Temp_Act", "Easy Test 1 — InputBox + MsgBox", "825"))
FanInput = InputBox("Fan_Status: enter 1 for RUNNING, 0 for STOPPED", "Easy Test 1 — InputBox + MsgBox", "1")
FanStatus = (FanInput = "1")

If TempAct >= TempSet And FanStatus Then
    ResultText = "TEMPERATURE READY + FAN RUNNING"
ElseIf TempAct >= TempSet Then
    ResultText = "TEMPERATURE READY, FAN STOPPED"
Else
    ResultText = "TEMPERATURE BELOW SETPOINT"
End If

MsgBox ResultText & vbCrLf & _
       "Temp_Set = " & TempSet & vbCrLf & _
       "Temp_Act = " & TempAct, vbInformation, "Condition Quick Test"

2. IF...ELSE

Use Else when exactly two outcomes are required.

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

Easy TestEasy Test 2 — InputBox + MsgBox

Change the InputBox values to force different IF/ELSE outcomes.

Dim TestValue, ResultText
TestValue = CDbl(InputBox("Enter a test value", "Easy Test 2 — InputBox + MsgBox", "75"))
If TestValue >= 80 Then
    ResultText = "HIGH"
ElseIf TestValue >= 50 Then
    ResultText = "NORMAL"
Else
    ResultText = "LOW"
End If
MsgBox "Input = " & TestValue & vbCrLf & "Result = " & ResultText, vbInformation, "Condition Quick Test"

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

Easy TestEasy Test 3 — InputBox + MsgBox

Change the InputBox values to force different IF/ELSE outcomes.

Dim TempSet, TempAct, FanInput, FanStatus, ResultText
TempSet = CDbl(InputBox("Enter Temp_Set", "Easy Test 3 — InputBox + MsgBox", "850"))
TempAct = CDbl(InputBox("Enter Temp_Act", "Easy Test 3 — InputBox + MsgBox", "825"))
FanInput = InputBox("Fan_Status: enter 1 for RUNNING, 0 for STOPPED", "Easy Test 3 — InputBox + MsgBox", "1")
FanStatus = (FanInput = "1")

If TempAct >= TempSet And FanStatus Then
    ResultText = "TEMPERATURE READY + FAN RUNNING"
ElseIf TempAct >= TempSet Then
    ResultText = "TEMPERATURE READY, FAN STOPPED"
Else
    ResultText = "TEMPERATURE BELOW SETPOINT"
End If

MsgBox ResultText & vbCrLf & _
       "Temp_Set = " & TempSet & vbCrLf & _
       "Temp_Act = " & TempAct, vbInformation, "Condition Quick Test"

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 = True Then
        HMIRuntime.Trace "Process ready" & vbCrLf
    Else
        HMIRuntime.Trace "Temperature ready, fan not running" & vbCrLf
    End If
End If

Easy TestEasy Test 4 — InputBox + MsgBox

Change the InputBox values to force different IF/ELSE outcomes.

Dim TempSet, TempAct, FanInput, FanStatus, ResultText
TempSet = CDbl(InputBox("Enter Temp_Set", "Easy Test 4 — InputBox + MsgBox", "850"))
TempAct = CDbl(InputBox("Enter Temp_Act", "Easy Test 4 — InputBox + MsgBox", "825"))
FanInput = InputBox("Fan_Status: enter 1 for RUNNING, 0 for STOPPED", "Easy Test 4 — InputBox + MsgBox", "1")
FanStatus = (FanInput = "1")

If TempAct >= TempSet And FanStatus Then
    ResultText = "TEMPERATURE READY + FAN RUNNING"
ElseIf TempAct >= TempSet Then
    ResultText = "TEMPERATURE READY, FAN STOPPED"
Else
    ResultText = "TEMPERATURE BELOW SETPOINT"
End If

MsgBox ResultText & vbCrLf & _
       "Temp_Set = " & TempSet & vbCrLf & _
       "Temp_Act = " & TempAct, vbInformation, "Condition Quick Test"
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 = True Then
    EventTo = "PROCESS READY"
Else
    EventTo = "PROCESS NOT READY"
End If

Easy TestEasy Test 5 — InputBox + MsgBox

Change the InputBox values to force different IF/ELSE outcomes.

Dim TempSet, TempAct, FanInput, FanStatus, ResultText
TempSet = CDbl(InputBox("Enter Temp_Set", "Easy Test 5 — InputBox + MsgBox", "850"))
TempAct = CDbl(InputBox("Enter Temp_Act", "Easy Test 5 — InputBox + MsgBox", "825"))
FanInput = InputBox("Fan_Status: enter 1 for RUNNING, 0 for STOPPED", "Easy Test 5 — InputBox + MsgBox", "1")
FanStatus = (FanInput = "1")

If TempAct >= TempSet And FanStatus Then
    ResultText = "TEMPERATURE READY + FAN RUNNING"
ElseIf TempAct >= TempSet Then
    ResultText = "TEMPERATURE READY, FAN STOPPED"
Else
    ResultText = "TEMPERATURE BELOW SETPOINT"
End If

MsgBox ResultText & vbCrLf & _
       "Temp_Set = " & TempSet & vbCrLf & _
       "Temp_Act = " & TempAct, vbInformation, "Condition Quick Test"

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
    HMIRuntime.Trace "Process deviation" & vbCrLf
End If

If Not FanStatus Then
    HMIRuntime.Trace "Fan is not running" & vbCrLf
End If

Easy TestEasy Test 6 — InputBox + MsgBox

Change the InputBox values to force different IF/ELSE outcomes.

Dim TempSet, TempAct, FanInput, FanStatus, ResultText
TempSet = CDbl(InputBox("Enter Temp_Set", "Easy Test 6 — InputBox + MsgBox", "850"))
TempAct = CDbl(InputBox("Enter Temp_Act", "Easy Test 6 — InputBox + MsgBox", "825"))
FanInput = InputBox("Fan_Status: enter 1 for RUNNING, 0 for STOPPED", "Easy Test 6 — InputBox + MsgBox", "1")
FanStatus = (FanInput = "1")

If TempAct >= TempSet And FanStatus Then
    ResultText = "TEMPERATURE READY + FAN RUNNING"
ElseIf TempAct >= TempSet Then
    ResultText = "TEMPERATURE READY, FAN STOPPED"
Else
    ResultText = "TEMPERATURE BELOW SETPOINT"
End If

MsgBox ResultText & vbCrLf & _
       "Temp_Set = " & TempSet & vbCrLf & _
       "Temp_Act = " & TempAct, vbInformation, "Condition Quick Test"

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

Easy TestEasy Test 7 — InputBox + MsgBox

Read/query preview using the SQF running project.

Dim ChargeNo
ChargeNo = InputBox("Enter a Charge Number to understand this query", "Easy Test 7 — InputBox + MsgBox", "CHG-001")
If ChargeNo = "" Then
    MsgBox "Test cancelled.", vbInformation, "Quick Test"
Else
    MsgBox "Query input = " & ChargeNo & vbCrLf & _
           "Run the SQL above against SQF_DB.dbo.tblEvent to see the actual rows.", _
           vbInformation, "SQL Quick Test"
End If

8. Select Case with Ranges

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

Select Case TempAct
    Case Is < 800
        EventTo = "LOW"
    Case 800 To 870
        EventTo = "NORMAL"
    Case Else
        EventTo = "HIGH"
End Select

Easy TestEasy Test 8 — InputBox + MsgBox

Read/query preview using the SQF running project.

Dim ChargeNo
ChargeNo = InputBox("Enter a Charge Number to understand this query", "Easy Test 8 — InputBox + MsgBox", "CHG-001")
If ChargeNo = "" Then
    MsgBox "Test cancelled.", vbInformation, "Quick Test"
Else
    MsgBox "Query input = " & ChargeNo & vbCrLf & _
           "Run the SQL above against SQF_DB.dbo.tblEvent to see the actual rows.", _
           vbInformation, "SQL Quick Test"
End If

9. Event_From → Event_To Mapping

The SQF table contains explicit transition fields. Build them from meaningful state changes rather than logging the same state every scan.

If FanStatus = True Then
    EventFrom = "FAN STOP"
    EventTo = "FAN START"
Else
    EventFrom = "FAN START"
    EventTo = "FAN STOP"
End If

Easy TestEasy Test 9 — InputBox + MsgBox

Change the InputBox values to force different IF/ELSE outcomes.

Dim TestValue, ResultText
TestValue = CDbl(InputBox("Enter a test value", "Easy Test 9 — InputBox + MsgBox", "75"))
If TestValue >= 80 Then
    ResultText = "HIGH"
ElseIf TestValue >= 50 Then
    ResultText = "NORMAL"
Else
    ResultText = "LOW"
End If
MsgBox "Input = " & TestValue & vbCrLf & "Result = " & ResultText, vbInformation, "Condition Quick Test"

10. 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 = True Then
    EventFrom = "PROCESS CHECK"
    EventTo = "PROCESS READY"
Else
    EventFrom = "PROCESS CHECK"
    EventTo = "PROCESS NOT READY"
End If

Easy TestEasy Test 10 — InputBox + MsgBox

Change the InputBox values to force different IF/ELSE outcomes.

Dim TempSet, TempAct, FanInput, FanStatus, ResultText
TempSet = CDbl(InputBox("Enter Temp_Set", "Easy Test 10 — InputBox + MsgBox", "850"))
TempAct = CDbl(InputBox("Enter Temp_Act", "Easy Test 10 — InputBox + MsgBox", "825"))
FanInput = InputBox("Fan_Status: enter 1 for RUNNING, 0 for STOPPED", "Easy Test 10 — InputBox + MsgBox", "1")
FanStatus = (FanInput = "1")

If TempAct >= TempSet And FanStatus Then
    ResultText = "TEMPERATURE READY + FAN RUNNING"
ElseIf TempAct >= TempSet Then
    ResultText = "TEMPERATURE READY, FAN STOPPED"
Else
    ResultText = "TEMPERATURE BELOW SETPOINT"
End If

MsgBox ResultText & vbCrLf & _
       "Temp_Set = " & TempSet & vbCrLf & _
       "Temp_Act = " & TempAct, vbInformation, "Condition Quick Test"
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