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. Variables in the SQF Project
A variable is a named storage location used while a script runs. VBScript variables are Variants, so the value assigned determines how the variable behaves.
Dim Conn, SQL, TM
Dim SQFNo, Charge, EventFrom, EventTo
Dim TempSet, TempAct, CpSet, CpAct
Dim OilSet, OilAct, JacketSet, JacketAct, FanStatus
Easy TestEasy Test 1 — InputBox + MsgBox
Beginner-safe declaration test with visible output.
Option Explicit
Dim TestValue
TestValue = InputBox("Enter a value for this declaration example", "Easy Test 1 — InputBox + MsgBox", "825.5")
MsgBox "Variable successfully declared and assigned." & vbCrLf & _
"Value = " & TestValue, vbInformation, "Declaration Quick Test"| Variable | Mapped SQL column | Typical value |
|---|---|---|
SQFNo | SQF_No | 2 |
Charge | ChargeNo | CH-260829-01 |
TempSet | Temp_Set | 850.000 |
TempAct | Temp_Act | 842.650 |
FanStatus | Fan_Status | True / False → 1 / 0 |
2. Dim, Const, Public and Private
Use Dim for normal local variables. Use Const for values that must not change. In reusable Global Script modules, Public and Private control module-level visibility.
Dim TempSet
Const MaxTemp = 1000
TempSet = 850
If TempSet > MaxTemp Then
MsgBox "Setpoint exceeds training limit"
End If
Easy TestEasy Test 2 — 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 2 — InputBox + MsgBox", "850"))
TempAct = CDbl(InputBox("Enter Temp_Act", "Easy Test 2 — InputBox + MsgBox", "825"))
FanInput = InputBox("Fan_Status: enter 1 for RUNNING, 0 for STOPPED", "Easy Test 2 — 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"3. Variant, Empty, Null and Nothing
VBScript does not declare Integer/Real/String types in Dim. Variables are Variants. Empty means uninitialized data, Null means unknown/no database value, and Nothing is for object references.
Dim Charge
If IsEmpty(Charge) Then
Charge = "NOT ENTERED"
End If
'Null is commonly checked after reading nullable SQL fields.
'Nothing is used later with ADODB/WinCC object references.
Easy TestEasy Test 3 — InputBox + MsgBox
Change the InputBox values to force different IF/ELSE outcomes.
Dim TestValue, ResultText
TestValue = CDbl(InputBox("Enter a test value", "Easy Test 3 — 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"4. Arithmetic Operators
Arithmetic operators calculate process differences and derived values.
Dim TempError
TempError = TempSet - TempAct
HMIRuntime.Trace "Temp Error = " & CStr(TempError) & vbCrLf
Easy TestEasy Test 4 — InputBox + MsgBox
Try spaces, decimal commas, numbers and text to see conversion/string functions clearly.
Dim RawValue, CleanValue
RawValue = InputBox("Enter a test value", "Easy Test 4 — InputBox + MsgBox", " 825,500 ")
CleanValue = Trim(RawValue)
CleanValue = Replace(CleanValue, ",", ".")
MsgBox "Original = [" & RawValue & "]" & vbCrLf & _
"Cleaned = [" & CleanValue & "]" & vbCrLf & _
"Length = " & Len(CleanValue), vbInformation, "String / Conversion Quick Test"| Operator | Meaning | SQF example |
|---|---|---|
+ | Add | Total / offset |
- | Subtract | TempSet - TempAct |
* | Multiply | Engineering conversion |
/ | Divide | Ratio/average |
Mod | Remainder | Index/cyclic grouping |
5. Comparison and Logical Operators
Comparison operators return True/False. Logical operators combine process conditions.
If TempAct >= TempSet And FanStatus = True Then
HMIRuntime.Trace "Temperature ready and fan running" & vbCrLf
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"| Type | Operators | Use |
|---|---|---|
| Comparison | = <> > < >= <= | Compare setpoint/actual/status |
| Logical | And Or Not Xor | Combine interlocks/permissives |
6. CStr, CInt and CDbl
Conversion functions make the intended representation explicit.
Dim RawSQF, RawTemp
RawSQF = "2"
RawTemp = "850.250"
SQFNo = CInt(RawSQF)
TempSet = CDbl(RawTemp)
Charge = CStr("CH-001")
Easy TestEasy Test 6 — InputBox + MsgBox
Try spaces, decimal commas, numbers and text to see conversion/string functions clearly.
Dim RawValue, CleanValue
RawValue = InputBox("Enter a test value", "Easy Test 6 — InputBox + MsgBox", " 825,500 ")
CleanValue = Trim(RawValue)
CleanValue = Replace(CleanValue, ",", ".")
MsgBox "Original = [" & RawValue & "]" & vbCrLf & _
"Cleaned = [" & CleanValue & "]" & vbCrLf & _
"Length = " & Len(CleanValue), vbInformation, "String / Conversion Quick Test"IsNumeric() before numeric conversion.7. IsNumeric and Input Validation
Use IsNumeric before converting operator-entered numeric text.
Dim UserTemp
UserTemp = InputBox("Enter Temperature Setpoint")
If IsNumeric(UserTemp) Then
TempSet = CDbl(UserTemp)
Else
MsgBox "Enter a numeric setpoint."
End If
Easy TestEasy Test 7 — 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 7 — InputBox + MsgBox", "850"))
TempAct = CDbl(InputBox("Enter Temp_Act", "Easy Test 7 — InputBox + MsgBox", "825"))
FanInput = InputBox("Fan_Status: enter 1 for RUNNING, 0 for STOPPED", "Easy Test 7 — 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"8. Trim, CStr and Replace
Your master SQF SQL logger converts text and doubles apostrophes in SQL text values. It also normalizes decimal separators before concatenating a numeric SQL statement.
Charge = Replace(CStr(HMIRuntime.Tags("ChargeNo").Read), "'", "''")
EventFrom = Replace(CStr(HMIRuntime.Tags("Event_From").Read), "'", "''")
TempAct = Replace(CStr(HMIRuntime.Tags("Temp_Act").Read), ",", ".")
Easy TestEasy Test 8 — InputBox + MsgBox
Offline tag simulation: useful before connecting the participant PC to WinCC Runtime.
Dim TestValue, ResultText
ResultText = ""
TestValue = InputBox("Enter simulated value for ChargeNo", "Easy Test 8 — InputBox + MsgBox", "TEST")
ResultText = ResultText & "ChargeNo = " & TestValue & vbCrLf
TestValue = InputBox("Enter simulated value for Event_From", "Easy Test 8 — InputBox + MsgBox", "TEST")
ResultText = ResultText & "Event_From = " & TestValue & vbCrLf
TestValue = InputBox("Enter simulated value for Temp_Act", "Easy Test 8 — InputBox + MsgBox", "825.5")
ResultText = ResultText & "Temp_Act = " & TestValue & vbCrLf
MsgBox ResultText & vbCrLf & "Now compare these values with the original HMIRuntime .Read example.", vbInformation, "Tag Read Quick Test"9. String Concatenation and Line Continuation
VBScript uses & to join text. A space followed by underscore continues a long statement on the next line.
Dim Message
Message = "SQF " & CStr(SQFNo) & _
" | Charge " & Charge & _
" | Temp " & CStr(TempAct)
HMIRuntime.Trace Message & vbCrLf
Easy TestEasy Test 9 — InputBox + MsgBox
Try spaces, decimal commas, numbers and text to see conversion/string functions clearly.
Dim RawValue, CleanValue
RawValue = InputBox("Enter a test value", "Easy Test 9 — InputBox + MsgBox", " 825,500 ")
CleanValue = Trim(RawValue)
CleanValue = Replace(CleanValue, ",", ".")
MsgBox "Original = [" & RawValue & "]" & vbCrLf & _
"Cleaned = [" & CleanValue & "]" & vbCrLf & _
"Length = " & Len(CleanValue), vbInformation, "String / Conversion Quick Test"10. Participant Exercises
Complete these before Blog 04.
TempSet - TempAct.And to combine temperature and fan status.SQFNo to Integer.ChargeNo with Replace.SQF Running Project Lab · Blog 03
Map VBScript variables to SQF table columns
Dim SQFNo, Charge, EventFrom, EventTo
Dim TempSet, TempAct, CpSet, CpAct
Dim OilSet, OilAct, JacketSet, JacketAct, FanStatus
TempAct = 842.650
TempSet = 850.000
HMIRuntime.Trace "Temp Error = " & CStr(TempSet - TempAct) & vbCrLf
Easy TestEasy Test 10 — InputBox + MsgBox
Try spaces, decimal commas, numbers and text to see conversion/string functions clearly.
Dim RawValue, CleanValue
RawValue = InputBox("Enter a test value", "Easy Test 10 — InputBox + MsgBox", " 825,500 ")
CleanValue = Trim(RawValue)
CleanValue = Replace(CleanValue, ",", ".")
MsgBox "Original = [" & RawValue & "]" & vbCrLf & _
"Cleaned = [" & CleanValue & "]" & vbCrLf & _
"Length = " & Len(CleanValue), vbInformation, "String / Conversion Quick Test"HMIRuntime.Tags(...).Read and then map into dbo.tblEvent.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.
