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"11. Participant Exercises
Complete these before procedures/functions.
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"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.
