<!-- Softwell WinCC VBScript 20-Part Tutorial · Blog 05 · updated 2026-08-29 --> WinCC VBScript Loops & Arrays: FOR, Do While, ReDim
Blog 05 · WinCC VBScript Tutorial · SQF Running Project

WinCC VBScript Loops & Arrays: FOR, Do While, ReDim

Reduce repetitive code by grouping and iterating SQF values. Learn fixed loops, condition-controlled loops, arrays, dynamic arrays and the Recordset loop pattern used later in SQL reporting.

WinCC VBScript · Beginner to Project Level
WinCC VBScript Loops & Arrays: FOR, Do While, ReDim

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 05 Learning Goal

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

Outcome

Loop through process values, use arrays for Temp/CP/Oil/Jacket, understand Exit For/Exit Do, and recognize the Do While Not rs.EOF SQL pattern.

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. FOR...NEXT

Use a For loop when the number of repetitions is known.

Dim i
For i = 1 To 4
    HMIRuntime.Trace "Process index = " & CStr(i) & vbCrLf
Next

Easy TestEasy Test 1 — InputBox + MsgBox

Change the cycle count to see how repeated execution works.

Dim CountTo, i, ResultText
CountTo = CInt(InputBox("How many loop cycles?", "Easy Test 1 — InputBox + MsgBox", "5"))
ResultText = ""
For i = 1 To CountTo
    ResultText = ResultText & "Cycle " & i & vbCrLf
Next
MsgBox ResultText, vbInformation, "Loop Quick Test"

2. STEP

Step changes the counter increment.

Dim i
For i = 0 To 10 Step 2
    HMIRuntime.Trace CStr(i) & vbCrLf
Next

Easy TestEasy Test 2 — InputBox + MsgBox

Change the cycle count to see how repeated execution works.

Dim CountTo, i, ResultText
CountTo = CInt(InputBox("How many loop cycles?", "Easy Test 2 — InputBox + MsgBox", "5"))
ResultText = ""
For i = 1 To CountTo
    ResultText = ResultText & "Cycle " & i & vbCrLf
Next
MsgBox ResultText, vbInformation, "Loop Quick Test"

3. Arrays for SQF Process Values

Arrays group related items under one variable name.

Dim ProcessName(3)
Dim ProcessValue(3)

ProcessName(0) = "Temperature"
ProcessName(1) = "CP"
ProcessName(2) = "Oil"
ProcessName(3) = "Jacket"

ProcessValue(0) = TempAct
ProcessValue(1) = CpAct
ProcessValue(2) = OilAct
ProcessValue(3) = JacketAct

Easy TestEasy Test 3 — InputBox + MsgBox

Uses the four SQF process values so array indexes are easy to visualize.

Dim V(3), i, ResultText
V(0) = InputBox("Enter Temp_Act", "Easy Test 3 — InputBox + MsgBox", "825.5")
V(1) = InputBox("Enter Cp_Act", "Easy Test 3 — InputBox + MsgBox", "0.85")
V(2) = InputBox("Enter Oil_Act", "Easy Test 3 — InputBox + MsgBox", "45.0")
V(3) = InputBox("Enter Jacket_Act", "Easy Test 3 — InputBox + MsgBox", "60.0")

For i = 0 To 3
    ResultText = ResultText & "Index " & i & " = " & V(i) & vbCrLf
Next

MsgBox ResultText, vbInformation, "Array Quick Test"

4. FOR + Array

Combine a loop and arrays to eliminate repetitive statements.

Dim i
For i = 0 To 3
    HMIRuntime.Trace ProcessName(i) & " = " & _
                     CStr(ProcessValue(i)) & vbCrLf
Next

Easy TestEasy Test 4 — InputBox + MsgBox

Change the cycle count to see how repeated execution works.

Dim CountTo, i, ResultText
CountTo = CInt(InputBox("How many loop cycles?", "Easy Test 4 — InputBox + MsgBox", "5"))
ResultText = ""
For i = 1 To CountTo
    ResultText = ResultText & "Cycle " & i & vbCrLf
Next
MsgBox ResultText, vbInformation, "Loop Quick Test"

5. FOR EACH

Use For Each when iterating items in a collection or array where the individual item is more important than its index.

Dim Item
Dim Names
Names = Array("Temp_Act", "Cp_Act", "Oil_Act", "Jacket_Act")

For Each Item In Names
    HMIRuntime.Trace CStr(Item) & vbCrLf
Next

Easy TestEasy Test 5 — InputBox + MsgBox

Uses the four SQF process values so array indexes are easy to visualize.

Dim V(3), i, ResultText
V(0) = InputBox("Enter Temp_Act", "Easy Test 5 — InputBox + MsgBox", "825.5")
V(1) = InputBox("Enter Cp_Act", "Easy Test 5 — InputBox + MsgBox", "0.85")
V(2) = InputBox("Enter Oil_Act", "Easy Test 5 — InputBox + MsgBox", "45.0")
V(3) = InputBox("Enter Jacket_Act", "Easy Test 5 — InputBox + MsgBox", "60.0")

For i = 0 To 3
    ResultText = ResultText & "Index " & i & " = " & V(i) & vbCrLf
Next

MsgBox ResultText, vbInformation, "Array Quick Test"

6. DO WHILE

Use Do While when repetition continues while a condition is true.

Dim Count
Count = 0

Do While Count < 4
    HMIRuntime.Trace "Count = " & CStr(Count) & vbCrLf
    Count = Count + 1
Loop

Easy TestEasy Test 6 — InputBox + MsgBox

Change the cycle count to see how repeated execution works.

Dim CountTo, i, ResultText
CountTo = CInt(InputBox("How many loop cycles?", "Easy Test 6 — InputBox + MsgBox", "5"))
ResultText = ""
For i = 1 To CountTo
    ResultText = ResultText & "Cycle " & i & vbCrLf
Next
MsgBox ResultText, vbInformation, "Loop Quick Test"

7. DO UNTIL

Do Until continues until its condition becomes true.

Dim Count
Count = 0

Do Until Count = 4
    Count = Count + 1
Loop

Easy TestEasy Test 7 — InputBox + MsgBox

Change the cycle count to see how repeated execution works.

Dim CountTo, i, ResultText
CountTo = CInt(InputBox("How many loop cycles?", "Easy Test 7 — InputBox + MsgBox", "5"))
ResultText = ""
For i = 1 To CountTo
    ResultText = ResultText & "Cycle " & i & vbCrLf
Next
MsgBox ResultText, vbInformation, "Loop Quick Test"

8. EXIT FOR and EXIT DO

Exit statements stop a loop early when an exceptional or completion condition is detected.

Dim i
For i = 0 To 3
    If ProcessValue(i) < 0 Then
        HMIRuntime.Trace "Invalid process value" & vbCrLf
        Exit For
    End If
Next

Easy TestEasy Test 8 — InputBox + MsgBox

Change the cycle count to see how repeated execution works.

Dim CountTo, i, ResultText
CountTo = CInt(InputBox("How many loop cycles?", "Easy Test 8 — InputBox + MsgBox", "5"))
ResultText = ""
For i = 1 To CountTo
    ResultText = ResultText & "Cycle " & i & vbCrLf
Next
MsgBox ResultText, vbInformation, "Loop Quick Test"

9. ReDim and Preserve

Dynamic arrays can be resized at Runtime. Preserve keeps existing elements while changing the upper bound.

Dim Events()
ReDim Events(1)
Events(0) = "CHARGE START"
Events(1) = "HEATING"

ReDim Preserve Events(2)
Events(2) = "TEMP ACHIEVED"

Easy TestEasy Test 9 — InputBox + MsgBox

Uses the four SQF process values so array indexes are easy to visualize.

Dim V(3), i, ResultText
V(0) = InputBox("Enter Temp_Act", "Easy Test 9 — InputBox + MsgBox", "825.5")
V(1) = InputBox("Enter Cp_Act", "Easy Test 9 — InputBox + MsgBox", "0.85")
V(2) = InputBox("Enter Oil_Act", "Easy Test 9 — InputBox + MsgBox", "45.0")
V(3) = InputBox("Enter Jacket_Act", "Easy Test 9 — InputBox + MsgBox", "60.0")

For i = 0 To 3
    ResultText = ResultText & "Index " & i & " = " & V(i) & vbCrLf
Next

MsgBox ResultText, vbInformation, "Array Quick Test"

10. SQL Recordset Pattern

Later, ADO Recordsets are normally processed until EOF becomes true.

Do While Not rs.EOF
    HMIRuntime.Trace CStr(rs.Fields("ChargeNo").Value) & vbCrLf
    rs.MoveNext
Loop

Easy TestEasy Test 10 — InputBox + MsgBox

Change the cycle count to see how repeated execution works.

Dim CountTo, i, ResultText
CountTo = CInt(InputBox("How many loop cycles?", "Easy Test 10 — InputBox + MsgBox", "5"))
ResultText = ""
For i = 1 To CountTo
    ResultText = ResultText & "Cycle " & i & vbCrLf
Next
MsgBox ResultText, vbInformation, "Loop Quick Test"
This is a preview only. Database connection and Recordset creation are covered in Blogs 16–18.

11. Participant Exercises

Complete these before procedures/functions.

Create arrays for four setpoints.
Create arrays for four actual values.
Print all process names with For.
Stop the loop if a value is negative.
Create a dynamic Events array with ReDim Preserve.
Explain why SQL Recordsets use Do While Not EOF.

SQF Running Project Lab · Blog 05

Loop through the four SQF process actual values

Dim Names, Values, i
Names = Array("Temperature", "CP", "Oil", "Jacket")
Values = Array(TempAct, CpAct, OilAct, JacketAct)

For i = 0 To 3
    HMIRuntime.Trace Names(i) & " = " & CStr(Values(i)) & vbCrLf
Next

Easy TestEasy Test 11 — InputBox + MsgBox

Uses the four SQF process values so array indexes are easy to visualize.

Dim V(3), i, ResultText
V(0) = InputBox("Enter Temp_Act", "Easy Test 11 — InputBox + MsgBox", "825.5")
V(1) = InputBox("Enter Cp_Act", "Easy Test 11 — InputBox + MsgBox", "0.85")
V(2) = InputBox("Enter Oil_Act", "Easy Test 11 — InputBox + MsgBox", "45.0")
V(3) = InputBox("Enter Jacket_Act", "Easy Test 11 — InputBox + MsgBox", "60.0")

For i = 0 To 3
    ResultText = ResultText & "Index " & i & " = " & V(i) & vbCrLf
Next

MsgBox ResultText, vbInformation, "Array Quick Test"
The loop replaces four nearly identical Trace/validation statements and prepares participants for Recordset iteration.

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 06
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