<!-- Softwell WinCC VBScript 20-Part Tutorial · Blog 03 · updated 2026-08-29 --> WinCC VBScript Variables, Operators & Data Conversion
Blog 03 · WinCC VBScript Tutorial · SQF Running Project

WinCC VBScript Variables, Operators & Data Conversion

Build the data foundation for the SQF furnace logger. Learn how VBScript stores values, compares them, converts operator/WinCC data, and prepares text and numbers before later HMIRuntime and SQL chapters.

WinCC VBScript · Beginner to Project Level
WinCC VBScript Variables, Operators & Data Conversion

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

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

Outcome

Declare and explain every SQF process variable, choose the correct operator, convert values safely, and prepare strings/numbers for later SQL logging.

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. 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"
VariableMapped SQL columnTypical value
SQFNoSQF_No2
ChargeChargeNoCH-260829-01
TempSetTemp_Set850.000
TempActTemp_Act842.650
FanStatusFan_StatusTrue / 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"
Training rule: Prefer the smallest practical scope. Local variables are easier to understand and troubleshoot.

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"
OperatorMeaningSQF example
+AddTotal / offset
-SubtractTempSet - TempAct
*MultiplyEngineering conversion
/DivideRatio/average
ModRemainderIndex/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"
TypeOperatorsUse
Comparison= <> > < >= <=Compare setpoint/actual/status
LogicalAnd Or Not XorCombine 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"
Remember: validate user-entered text with 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"
Later SQL chapter: parameterized ADODB commands are the stronger production pattern. This lesson explains the string-processing functions used in the supplied SQF script.

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.

Declare all 15 SQL-bound SQF variables.
Calculate TempSet - TempAct.
Use And to combine temperature and fan status.
Validate a numeric temperature entered with InputBox.
Convert SQFNo to Integer.
Escape an apostrophe in 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"
The same variable names will later receive values from 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.

Continue the WinCC VBScript 20-Part Tutorial

Use the same SQF process variables in the next chapter.

Next: Blog 04
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