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

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.

Learning Foundation: Repeated Data Processing

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.

Beginner to IntermediateWinCC Explorer / Classic WinCCSQF Running Project
Prerequisite

Decision logic, variables and conversions should be understood first.

Core concept

Loops remove repeated code while arrays keep related process values together for systematic validation, display, logging and reporting.

SQF practical connection

Process Temp, CP, Oil and Jacket values as grouped arrays and iterate through SQF records or SQL Recordsets.

Expected competency

Use For, For Each, Do While, Do Until, arrays, ReDim and Preserve with explicit termination conditions.

WinCC VBScript Loops & Arrays: FOR, Do While, ReDim — ArchitectureCode-rendered HTML/CSS architecture; no image file required
Multiple Values

Collect related process data

ProcessValue(3)
Array

Store in indexed structure

ProcessValue(0)
Loop

Iterate predictably

For i = 0 To 3
Condition

Check each value

If IsNumeric(...)
Result

Build text/log/report rows

ResultText = ...

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 →
Easy Testing Method: Every code block below is written as a complete WinCC Graphics Designer Sub OnClick(ByVal Item) action. Every main example is followed by a safe InputBox + MsgBox test with basic input validation. Use the dialog version first, then move to the WinCC/Recordset example. Database writes and equipment commands are previewed or simulated.
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.

Sub OnClick(ByVal Item)

    Dim i

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

End Sub

Easy TestEasy Test 1 — InputBox + MsgBox

Change the cycle count to see how repeated execution works.

Sub OnClick(ByVal Item)

    Dim UserInput, CountTo, i, ResultText

    UserInput = InputBox("How many loop cycles?", _
                         "Easy Test 1 - FOR...NEXT", "5")

    If Trim(UserInput) = "" Then Exit Sub

    If Not IsNumeric(UserInput) Then
        MsgBox "Please enter a numeric value.", vbExclamation, "Invalid Input"
        Exit Sub
    End If

    CountTo = CLng(UserInput)

    If CountTo < 1 Or CountTo > 100 Then
        MsgBox "Enter a value from 1 to 100.", vbExclamation, "Invalid Range"
        Exit Sub
    End If

    ResultText = ""

    For i = 1 To CountTo
        ResultText = ResultText & "Cycle " & CStr(i) & vbCrLf
    Next

    MsgBox ResultText, vbInformation, "FOR...NEXT Result"

End Sub

2. STEP

Step changes the counter increment.

Sub OnClick(ByVal Item)

    Dim i

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

End Sub

Easy TestEasy Test 2 — InputBox + MsgBox

Enter a maximum value and observe how Step 2 skips alternate numbers.

Sub OnClick(ByVal Item)

    Dim UserInput, MaxValue, i, ResultText

    UserInput = InputBox("Enter the maximum value:", _
                         "Easy Test 2 - STEP", "10")

    If Trim(UserInput) = "" Then Exit Sub

    If Not IsNumeric(UserInput) Then
        MsgBox "Please enter a numeric value.", vbExclamation, "Invalid Input"
        Exit Sub
    End If

    MaxValue = CLng(UserInput)

    If MaxValue < 0 Or MaxValue > 100 Then
        MsgBox "Enter a value from 0 to 100.", vbExclamation, "Invalid Range"
        Exit Sub
    End If

    ResultText = ""

    For i = 0 To MaxValue Step 2
        ResultText = ResultText & CStr(i) & vbCrLf
    Next

    MsgBox ResultText, vbInformation, "STEP Result"

End Sub

3. Arrays for SQF Process Values

Arrays group related items under one variable name.

Sub OnClick(ByVal Item)

    Dim ProcessName(3)
    Dim ProcessValue(3)
    Dim TempAct, CpAct, OilAct, JacketAct
    Dim ResultText

    ' Read live WinCC process tags
    TempAct = HMIRuntime.Tags("Temp_Act").Read
    CpAct = HMIRuntime.Tags("Cp_Act").Read
    OilAct = HMIRuntime.Tags("Oil_Act").Read
    JacketAct = HMIRuntime.Tags("Jacket_Act").Read

    ' Store process names in an array
    ProcessName(0) = "Temperature"
    ProcessName(1) = "CP"
    ProcessName(2) = "Oil"
    ProcessName(3) = "Jacket"

    ' Store live process values in an array
    ProcessValue(0) = TempAct
    ProcessValue(1) = CpAct
    ProcessValue(2) = OilAct
    ProcessValue(3) = JacketAct

    ResultText = ProcessName(0) & " = " & CStr(ProcessValue(0)) & vbCrLf & _
                 ProcessName(1) & " = " & CStr(ProcessValue(1)) & vbCrLf & _
                 ProcessName(2) & " = " & CStr(ProcessValue(2)) & vbCrLf & _
                 ProcessName(3) & " = " & CStr(ProcessValue(3))

    MsgBox ResultText, vbInformation, "SQF Process Arrays"

End Sub

Easy TestEasy Test 3 — InputBox + MsgBox

Enter four process values, validate them, and store them in indexed arrays.

Sub OnClick(ByVal Item)

    Dim ProcessName(3)
    Dim ProcessValue(3)
    Dim i, ResultText

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

    ProcessValue(0) = InputBox("Enter Temp_Act", _
                               "Easy Test 3 - Arrays", "825.5")
    ProcessValue(1) = InputBox("Enter Cp_Act", _
                               "Easy Test 3 - Arrays", "0.85")
    ProcessValue(2) = InputBox("Enter Oil_Act", _
                               "Easy Test 3 - Arrays", "45.0")
    ProcessValue(3) = InputBox("Enter Jacket_Act", _
                               "Easy Test 3 - Arrays", "60.0")

    For i = 0 To 3
        If Trim(ProcessValue(i)) = "" Or Not IsNumeric(ProcessValue(i)) Then
            MsgBox ProcessName(i) & " must be numeric.", _
                   vbExclamation, "Invalid Input"
            Exit Sub
        End If
        ProcessValue(i) = CDbl(ProcessValue(i))
    Next

    ResultText = ProcessName(0) & " = " & CStr(ProcessValue(0)) & vbCrLf & _
                 ProcessName(1) & " = " & CStr(ProcessValue(1)) & vbCrLf & _
                 ProcessName(2) & " = " & CStr(ProcessValue(2)) & vbCrLf & _
                 ProcessName(3) & " = " & CStr(ProcessValue(3))

    MsgBox ResultText, vbInformation, "Array Quick Test"

End Sub

4. FOR + Array

Combine a loop and arrays to eliminate repetitive statements.

Sub OnClick(ByVal Item)

    Dim ProcessName, ProcessValue
    Dim i

    ProcessName = Array("Temperature", "CP", "Oil", "Jacket")

    ProcessValue = Array( _
        HMIRuntime.Tags("Temp_Act").Read, _
        HMIRuntime.Tags("Cp_Act").Read, _
        HMIRuntime.Tags("Oil_Act").Read, _
        HMIRuntime.Tags("Jacket_Act").Read)

    For i = LBound(ProcessName) To UBound(ProcessName)
        HMIRuntime.Trace ProcessName(i) & " = " & _
                         CStr(ProcessValue(i)) & vbCrLf
    Next

End Sub

Easy TestEasy Test 4 — InputBox + MsgBox

Combine process-name and process-value arrays with one For loop.

Sub OnClick(ByVal Item)

    Dim ProcessName, ProcessValue
    Dim i, ResultText

    ProcessName = Array("Temperature", "CP", "Oil", "Jacket")

    ProcessValue = Array( _
        InputBox("Enter Temp_Act", "Easy Test 4 - FOR + Array", "825.5"), _
        InputBox("Enter Cp_Act", "Easy Test 4 - FOR + Array", "0.85"), _
        InputBox("Enter Oil_Act", "Easy Test 4 - FOR + Array", "45.0"), _
        InputBox("Enter Jacket_Act", "Easy Test 4 - FOR + Array", "60.0"))

    ResultText = ""

    For i = LBound(ProcessValue) To UBound(ProcessValue)

        If Trim(ProcessValue(i)) = "" Or Not IsNumeric(ProcessValue(i)) Then
            MsgBox ProcessName(i) & " must be numeric.", _
                   vbExclamation, "Invalid Input"
            Exit Sub
        End If

        ResultText = ResultText & ProcessName(i) & " = " & _
                     CStr(CDbl(ProcessValue(i))) & vbCrLf
    Next

    MsgBox ResultText, vbInformation, "FOR + Array Result"

End Sub

5. FOR EACH

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

Sub OnClick(ByVal Item)

    Dim TagNames
    Dim TagName
    Dim TagValue

    TagNames = Array("Temp_Act", "Cp_Act", "Oil_Act", "Jacket_Act")

    For Each TagName In TagNames
        TagValue = HMIRuntime.Tags(CStr(TagName)).Read

        HMIRuntime.Trace CStr(TagName) & " = " & _
                         CStr(TagValue) & vbCrLf
    Next

End Sub

Easy TestEasy Test 5 — InputBox + MsgBox

Enter comma-separated process names and iterate them with For Each.

Sub OnClick(ByVal Item)

    Dim UserInput, Names
    Dim ProcessName
    Dim ResultText

    UserInput = InputBox( _
        "Enter process names separated by commas:", _
        "Easy Test 5 - FOR EACH", _
        "Temperature,CP,Oil,Jacket")

    If Trim(UserInput) = "" Then Exit Sub

    Names = Split(UserInput, ",")
    ResultText = ""

    For Each ProcessName In Names
        ResultText = ResultText & Trim(CStr(ProcessName)) & vbCrLf
    Next

    MsgBox ResultText, vbInformation, "FOR EACH Result"

End Sub

6. DO WHILE

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

Sub OnClick(ByVal Item)

    Dim Count

    Count = 0

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

End Sub

Easy TestEasy Test 6 — InputBox + MsgBox

Enter the cycle count and execute the repetition with Do While.

Sub OnClick(ByVal Item)

    Dim UserInput, CountTo
    Dim Count, ResultText

    UserInput = InputBox("How many loop cycles?", _
                         "Easy Test 6 - DO WHILE", "5")

    If Trim(UserInput) = "" Then Exit Sub

    If Not IsNumeric(UserInput) Then
        MsgBox "Please enter a numeric value.", vbExclamation, "Invalid Input"
        Exit Sub
    End If

    CountTo = CLng(UserInput)

    If CountTo < 1 Or CountTo > 100 Then
        MsgBox "Enter a value from 1 to 100.", vbExclamation, "Invalid Range"
        Exit Sub
    End If

    Count = 1
    ResultText = ""

    Do While Count <= CountTo
        ResultText = ResultText & "Cycle " & CStr(Count) & vbCrLf
        Count = Count + 1
    Loop

    MsgBox ResultText, vbInformation, "DO WHILE Result"

End Sub

7. DO UNTIL

Do Until continues until its condition becomes true.

Sub OnClick(ByVal Item)

    Dim Count

    Count = 0

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

    HMIRuntime.Trace "Loop finished at Count = " & _
                     CStr(Count) & vbCrLf

End Sub

Easy TestEasy Test 7 — InputBox + MsgBox

Enter the cycle count and execute the repetition with Do Until.

Sub OnClick(ByVal Item)

    Dim UserInput, CountTo
    Dim Count, ResultText

    UserInput = InputBox("How many loop cycles?", _
                         "Easy Test 7 - DO UNTIL", "5")

    If Trim(UserInput) = "" Then Exit Sub

    If Not IsNumeric(UserInput) Then
        MsgBox "Please enter a numeric value.", vbExclamation, "Invalid Input"
        Exit Sub
    End If

    CountTo = CLng(UserInput)

    If CountTo < 1 Or CountTo > 100 Then
        MsgBox "Enter a value from 1 to 100.", vbExclamation, "Invalid Range"
        Exit Sub
    End If

    Count = 1
    ResultText = ""

    Do Until Count > CountTo
        ResultText = ResultText & "Cycle " & CStr(Count) & vbCrLf
        Count = Count + 1
    Loop

    MsgBox ResultText, vbInformation, "DO UNTIL Result"

End Sub

8. EXIT FOR and EXIT DO

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

Sub OnClick(ByVal Item)

    Dim ProcessName, ProcessValue
    Dim i, Count

    ProcessName = Array("Temperature", "CP", "Oil", "Jacket")

    ProcessValue = Array( _
        HMIRuntime.Tags("Temp_Act").Read, _
        HMIRuntime.Tags("Cp_Act").Read, _
        HMIRuntime.Tags("Oil_Act").Read, _
        HMIRuntime.Tags("Jacket_Act").Read)

    ' EXIT FOR example
    For i = LBound(ProcessValue) To UBound(ProcessValue)

        If Not IsNumeric(ProcessValue(i)) Then
            HMIRuntime.Trace ProcessName(i) & _
                             " is not numeric." & vbCrLf
            Exit For
        End If

        If CDbl(ProcessValue(i)) < 0 Then
            HMIRuntime.Trace ProcessName(i) & _
                             " is negative." & vbCrLf
            Exit For
        End If
    Next

    ' EXIT DO example
    Count = 0

    Do While Count < 10
        Count = Count + 1

        If Count = 4 Then
            HMIRuntime.Trace "Exit Do at Count = 4" & vbCrLf
            Exit Do
        End If
    Loop

End Sub

Easy TestEasy Test 8 — InputBox + MsgBox

Choose a stop point and compare Exit For with Exit Do.

Sub OnClick(ByVal Item)

    Dim UserInput, StopAt
    Dim i, Count, ResultText

    UserInput = InputBox("Stop the loops at which cycle?", _
                         "Easy Test 8 - EXIT FOR / EXIT DO", "4")

    If Trim(UserInput) = "" Then Exit Sub

    If Not IsNumeric(UserInput) Then
        MsgBox "Please enter a numeric value.", vbExclamation, "Invalid Input"
        Exit Sub
    End If

    StopAt = CLng(UserInput)

    If StopAt < 1 Or StopAt > 10 Then
        MsgBox "Enter a value from 1 to 10.", vbExclamation, "Invalid Range"
        Exit Sub
    End If

    ResultText = "EXIT FOR:" & vbCrLf

    For i = 1 To 10
        ResultText = ResultText & "Cycle " & CStr(i) & vbCrLf

        If i = StopAt Then Exit For
    Next

    ResultText = ResultText & vbCrLf & "EXIT DO:" & vbCrLf
    Count = 1

    Do While Count <= 10
        ResultText = ResultText & "Cycle " & CStr(Count) & vbCrLf

        If Count = StopAt Then Exit Do

        Count = Count + 1
    Loop

    MsgBox ResultText, vbInformation, "Exit Loop Result"

End Sub

9. ReDim and Preserve

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

Sub OnClick(ByVal Item)

    Dim Events()
    Dim i

    ReDim Events(1)

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

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

    For i = LBound(Events) To UBound(Events)
        HMIRuntime.Trace "Events(" & CStr(i) & ") = " & _
                         CStr(Events(i)) & vbCrLf
    Next

End Sub

Easy TestEasy Test 9 — InputBox + MsgBox

Add three event strings while expanding a dynamic array with ReDim Preserve.

Sub OnClick(ByVal Item)

    Dim Events()
    Dim ResultText

    ReDim Events(0)
    Events(0) = InputBox("Enter Event 1", _
                         "Easy Test 9 - ReDim", "CHARGE START")

    ReDim Preserve Events(1)
    Events(1) = InputBox("Enter Event 2", _
                         "Easy Test 9 - ReDim Preserve", "HEATING")

    ReDim Preserve Events(2)
    Events(2) = InputBox("Enter Event 3", _
                         "Easy Test 9 - ReDim Preserve", "TEMP ACHIEVED")

    ResultText = "Events(0) = " & Events(0) & vbCrLf & _
                 "Events(1) = " & Events(1) & vbCrLf & _
                 "Events(2) = " & Events(2)

    MsgBox ResultText, vbInformation, "ReDim Preserve Result"

End Sub

10. SQL Recordset Pattern

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

Sub OnClick(ByVal Item)

    Dim rs
    Dim ChargeNo

    ' Create an in-memory ADO Recordset for loop practice.
    ' In the SQL lesson, the same rs object is opened by a SELECT query.
    Set rs = CreateObject("ADODB.Recordset")

    rs.Fields.Append "ChargeNo", 200, 50
    rs.Open

    rs.AddNew
    rs.Fields("ChargeNo").Value = "CHARGE-001"
    rs.Update

    rs.AddNew
    rs.Fields("ChargeNo").Value = "CHARGE-002"
    rs.Update

    rs.AddNew
    rs.Fields("ChargeNo").Value = "CHARGE-003"
    rs.Update

    rs.MoveFirst

    Do While Not rs.EOF

        If IsNull(rs.Fields("ChargeNo").Value) Then
            ChargeNo = "(NULL)"
        Else
            ChargeNo = CStr(rs.Fields("ChargeNo").Value)
        End If

        HMIRuntime.Trace ChargeNo & vbCrLf

        rs.MoveNext
    Loop

    rs.Close
    Set rs = Nothing

End Sub

Easy TestEasy Test 10 — InputBox + MsgBox

Simulate the Recordset MoveNext/EOF pattern without connecting to SQL Server.

Sub OnClick(ByVal Item)

    Dim UserInput, ChargeList
    Dim i, ResultText

    UserInput = InputBox( _
        "Enter charge numbers separated by commas:", _
        "Easy Test 10 - Recordset Loop Simulation", _
        "CHARGE-001,CHARGE-002,CHARGE-003")

    If Trim(UserInput) = "" Then Exit Sub

    ChargeList = Split(UserInput, ",")
    i = LBound(ChargeList)
    ResultText = ""

    ' This simulates rs.MoveNext / rs.EOF without a database.
    Do While i <= UBound(ChargeList)
        ResultText = ResultText & Trim(CStr(ChargeList(i))) & vbCrLf
        i = i + 1
    Loop

    MsgBox ResultText, vbInformation, "Recordset Loop Simulation"

End Sub
This example creates an in-memory ADO Recordset so the Do While Not rs.EOF pattern can be tested safely. SQL Server connection and SELECT-query Recordsets 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

Sub OnClick(ByVal Item)

    Dim Names, Values
    Dim TempAct, CpAct, OilAct, JacketAct
    Dim i

    TempAct = HMIRuntime.Tags("Temp_Act").Read
    CpAct = HMIRuntime.Tags("Cp_Act").Read
    OilAct = HMIRuntime.Tags("Oil_Act").Read
    JacketAct = HMIRuntime.Tags("Jacket_Act").Read

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

    For i = LBound(Names) To UBound(Names)
        HMIRuntime.Trace Names(i) & " = " & _
                         CStr(Values(i)) & vbCrLf
    Next

End Sub

Easy TestEasy Test 11 — InputBox + MsgBox

Enter and validate the four SQF process values, then display them through one array loop.

Sub OnClick(ByVal Item)

    Dim Names, Values
    Dim i, ResultText

    Names = Array("Temperature", "CP", "Oil", "Jacket")

    Values = Array( _
        InputBox("Enter Temp_Act", "Easy Test 11 - SQF Array Lab", "825.5"), _
        InputBox("Enter Cp_Act", "Easy Test 11 - SQF Array Lab", "0.85"), _
        InputBox("Enter Oil_Act", "Easy Test 11 - SQF Array Lab", "45.0"), _
        InputBox("Enter Jacket_Act", "Easy Test 11 - SQF Array Lab", "60.0"))

    ResultText = ""

    For i = LBound(Values) To UBound(Values)

        If Trim(Values(i)) = "" Or Not IsNumeric(Values(i)) Then
            MsgBox Names(i) & " must be numeric.", _
                   vbExclamation, "Invalid Input"
            Exit Sub
        End If

        ResultText = ResultText & Names(i) & " = " & _
                     CStr(CDbl(Values(i))) & vbCrLf
    Next

    MsgBox ResultText, vbInformation, "SQF Array Lab Result"

End Sub
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