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"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 If8. 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 If9. 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.
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"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.
