WinCC VBScripting Practice Sequence Summary
This 28-practical sequence is designed for engineers learning VBScript inside SIMATIC WinCC Explorer/Classic. Each practical has one clear objective, a small configuration task, copy-paste code where applicable, and an expected Runtime result.
| Practical | Training Topic | Main Result |
|---|---|---|
| 01 | Create Internal Tags | WinCC tag database |
| 02 | Read WinCC Tag | Read process data |
| 03 | Write WinCC Tag | Write commands and setpoints |
| 04 | Motor Start/Stop | Equipment control |
| 05 | Analog Monitoring | Temperature, CP, oil and jacket monitoring |
| 06 | Setpoint Entry | Operator data input |
| 07 | IF/ELSE Process Logic | Process decisions |
| 08 | Multiple Interlocks | Equipment protection |
| 09 | InputBox Operator Entry | Operator entry |
| 10 | MsgBox Confirmation | Operator confirmation |
| 11 | Screen Navigation | SCADA navigation |
| 12 | Popup Handling | Equipment popup |
| 13 | Object Properties | Graphic dynamization |
| 14 | FOR Loop | Repetitive scripting |
| 15 | Date/Time Processing | Timestamp handling |
| 16 | Error Handling | Runtime diagnostics |
| 17 | Create SQL Database | SQF_DB |
| 18 | Create SQL Table | dbo.tblEvent |
| 19 | SQL Connection | WinCC to SQL Server |
| 20 | SQL INSERT | Save process values |
| 21 | SQL SELECT | Read historical values |
| 22 | SQL UPDATE | Modify a record |
| 23 | SQL DELETE | Delete a selected record |
| 24 | Historical Search | Charge-wise search |
| 25 | Alarm Logging | Process-event storage |
| 26 | Production Logging | Charge and process logging |
| 27 | Excel Report | Complete history export |
| 28 | Date-wise Report | Daily production report |
WinCC Tag Mapping Used in the Practical Series
Create these tags before the SQL and reporting practicals. Matching tag/database names make the examples easier to understand and troubleshoot.
| WinCC Tag | Suggested Type | SQL / Use |
|---|---|---|
SQF_No | 32-bit integer | SQF_No |
ChargeNo | Text | ChargeNo |
Event_From | Text | Event_From |
Event_To | Text | Event_To |
Temp_Set | Float | Temp_Set |
Temp_Act | Float | Temp_Act |
Cp_Set | Float | Cp_Set |
Cp_Act | Float | Cp_Act |
Oil_Set | Float | Oil_Set |
Oil_Act | Float | Oil_Act |
Jacket_Set | Float | Jacket_Set |
Jacket_Act | Float | Jacket_Act |
Fan_Status | Binary | Fan_Status |
SQL_ID | 32-bit integer | Selected SQL row ID |
SQL_Status | Text | Connection / logging status |
Practice 01 — Create Internal WinCC Tags
Build the WinCC tag database used by every later VBScript, SQL and reporting practical.
- Open WinCC Explorer → Tag Management → Internal Tags.
- Create the process tags below with names that match the SQL project.
- Use Binary for status/commands, a floating-point type for process values, a 32-bit integer for IDs/SQF number, and Text for charge/event strings.
Practice 02 — Read a WinCC Tag Using VBScript
Read an actual process value from WinCC Runtime.
- Create a button in Graphics Designer.
- Open the button Mouse Click event and select VBScript.
- Paste the script and start Runtime.
Dim Temp
Temp = HMIRuntime.Tags("Temp_Act").Read
MsgBox "Actual Temperature = " & Temp & " °C"Practice 03 — Write a WinCC Tag
Write a command or setpoint from VBScript into a WinCC tag.
- Create a test button.
- Use the Write method on the required WinCC tag.
- Verify the changed value in an I/O field or Tag Management diagnostics.
HMIRuntime.Tags("Temp_Set").Write 850
HMIRuntime.Tags("ChargeNo").Write "CHG-001"Practice 04 — Start / Stop Motor Through Script
Control motor command tags from Graphics Designer buttons.
- Create Start and Stop buttons.
- Assign one script to each button.
- Keep PLC-side permissives and safety logic authoritative in the controller.
' START button
HMIRuntime.Tags("Motor_Start").Write 1
HMIRuntime.Tags("Motor_Stop").Write 0
' STOP button
' HMIRuntime.Tags("Motor_Start").Write 0
' HMIRuntime.Tags("Motor_Stop").Write 1Practice 05 — Analog Value Monitoring
Evaluate temperature, carbon potential, oil and jacket values in Runtime.
- Read the actual process tag.
- Compare it against defined operating limits.
- Show an operator status or set a WinCC alarm/interlock tag.
Dim Temp
Temp = HMIRuntime.Tags("Temp_Act").Read
If Temp >= 900 Then
MsgBox "Critical Temperature = " & Temp & " °C"
ElseIf Temp >= 850 Then
MsgBox "High Temperature = " & Temp & " °C"
Else
MsgBox "Temperature Normal = " & Temp & " °C"
End IfPractice 06 — Setpoint Entry
Accept an operator-entered setpoint and write it only when the input is numeric.
- Read the current setpoint as the InputBox default.
- Validate the returned string with IsNumeric.
- Convert and write the value to Temp_Set.
Dim Value
Value = InputBox("Enter Temperature Setpoint", _
"SQF Temperature Setpoint", _
HMIRuntime.Tags("Temp_Set").Read)
If Value <> "" Then
If IsNumeric(Value) Then
HMIRuntime.Tags("Temp_Set").Write CDbl(Value)
Else
MsgBox "Please enter a numeric value."
End If
End IfPractice 07 — IF / ELSE Process Logic
Use process conditions to make a WinCC decision.
- Read setpoint and actual temperature.
- Compare PV against SP.
- Write the Temp_High status according to the result.
Dim TempSet, TempAct
TempSet = HMIRuntime.Tags("Temp_Set").Read
TempAct = HMIRuntime.Tags("Temp_Act").Read
If TempAct > TempSet Then
HMIRuntime.Tags("Temp_High").Write 1
Else
HMIRuntime.Tags("Temp_High").Write 0
End IfPractice 08 — Multiple-Condition Interlock
Combine temperature, CP and oil conditions into one process interlock.
- Read all set and actual values.
- Use OR conditions for abnormal states.
- Set Process_Interlock and remove the start request when any limit is exceeded.
Dim TempSet, TempAct, CpSet, CpAct, OilSet, OilAct
TempSet = HMIRuntime.Tags("Temp_Set").Read
TempAct = HMIRuntime.Tags("Temp_Act").Read
CpSet = HMIRuntime.Tags("Cp_Set").Read
CpAct = HMIRuntime.Tags("Cp_Act").Read
OilSet = HMIRuntime.Tags("Oil_Set").Read
OilAct = HMIRuntime.Tags("Oil_Act").Read
If TempAct > TempSet + 30 Or _
CpAct > CpSet + 0.2 Or _
OilAct > OilSet + 10 Then
HMIRuntime.Tags("Process_Interlock").Write 1
HMIRuntime.Tags("Motor_Start").Write 0
Else
HMIRuntime.Tags("Process_Interlock").Write 0
End IfPractice 09 — InputBox Operator Entry
Collect a charge number from the operator.
- Open an InputBox from a button event.
- Reject an empty response.
- Write the accepted charge number to ChargeNo.
Dim Charge
Charge = InputBox("Enter Charge Number", "SQF Charge Entry")
If Charge <> "" Then
HMIRuntime.Tags("ChargeNo").Write Charge
MsgBox "Charge Number Updated: " & Charge
End IfPractice 10 — MsgBox Confirmation
Require operator confirmation before an action.
- Create the confirmation message.
- Check whether the operator selected Yes.
- Execute the command only after confirmation.
Dim Answer
Answer = MsgBox("Do you want to start the SQF process?", _
vbYesNo + vbQuestion, _
"Process Confirmation")
If Answer = vbYes Then
HMIRuntime.Tags("Motor_Start").Write 1
Else
HMIRuntime.Tags("Motor_Start").Write 0
End IfPractice 11 — Screen Navigation
Navigate between WinCC process, trend and report pictures.
- Confirm the target PDL picture names in Graphics Designer.
- Assign the navigation script to menu buttons.
- Test navigation in Runtime.
' Open process screen
HMIRuntime.BaseScreenName = "SQF_Process.pdl"
' Other examples:
' HMIRuntime.BaseScreenName = "SQF_Trend.pdl"
' HMIRuntime.BaseScreenName = "SQF_Report.pdl"Practice 12 — Popup Handling
Open and close an equipment popup using a Picture Window object.
- Create a Picture Window named PW_ProcessPopup.
- Create the popup picture SQF_Popup.pdl.
- Set PictureName and Visible from the VBScript event.
' Open popup
ScreenItems("PW_ProcessPopup").PictureName = "SQF_Popup.pdl"
ScreenItems("PW_ProcessPopup").Visible = True
' Close popup example:
' ScreenItems("PW_ProcessPopup").Visible = FalsePractice 13 — Dynamic Object Properties
Change a graphic object at Runtime according to a WinCC tag.
- Create a rectangle named RECT_TempStatus.
- Read Temp_High.
- Change visibility or another supported object property.
Dim AlarmState
AlarmState = HMIRuntime.Tags("Temp_High").Read
If AlarmState = 1 Then
ScreenItems("RECT_TempStatus").Visible = True
Else
ScreenItems("RECT_TempStatus").Visible = False
End IfPractice 14 — FOR Loop
Execute repetitive VBScript logic with a controlled loop.
- Declare the loop counter.
- Run the loop from 1 to 10.
- Use HMIRuntime.Trace to observe execution in diagnostics.
Dim i
For i = 1 To 10
HMIRuntime.Trace "WinCC VBScript Loop = " & i & vbCrLf
NextPractice 15 — Date / Time Processing
Create SQL/report-friendly date and time values.
- Use Now for the authoritative current date/time.
- Build the legacy TM display string as HH:MM:SS.
- Store DT as datetime2(3) in SQL Server and treat TM only as a display field.
Dim DT, TM
DT = Now
TM = Right("0" & Hour(Now), 2) & ":" & _
Right("0" & Minute(Now), 2) & ":" & _
Right("0" & Second(Now), 2)
MsgBox "DT = " & DT & vbCrLf & "TM = " & TMPractice 16 — Error Handling
Prevent external communication errors from terminating the Runtime action without a useful diagnostic.
- Enable On Error Resume Next only around the operation that may fail.
- Check Err.Number immediately after the operation.
- Report/trace the error, clear it and restore normal error handling.
On Error Resume Next
Dim Temp
Temp = HMIRuntime.Tags("Temp_Act").Read
If Err.Number <> 0 Then
HMIRuntime.Trace "Error " & Err.Number & ": " & Err.Description & vbCrLf
Err.Clear
Else
MsgBox "Temperature = " & Temp
End If
On Error GoTo 0Practice 17 — Create SQL Database
Create the SQL Server database used by the WinCC reporting project.
- Open SQL Server Management Studio.
- Run the idempotent database creation script.
- Verify SQF_DB in sys.databases.
IF DB_ID(N'SQF_DB') IS NULL
BEGIN
CREATE DATABASE [SQF_DB];
END;
GO
SELECT name
FROM sys.databases
WHERE name = 'SQF_DB';
GOPractice 18 — Create dbo.tblEvent
Create the exact event/process table used by the WinCC VBScript examples.
- Select SQF_DB.
- Create dbo.tblEvent only if it does not already exist.
- Keep DT as the authoritative datetime2(3) timestamp and TM as the legacy display value.
USE [SQF_DB];
GO
IF OBJECT_ID(N'dbo.tblEvent', N'U') IS NULL
BEGIN
CREATE TABLE [dbo].[tblEvent]
(
[ID] int IDENTITY(1,1) NOT NULL,
[DT] datetime2(3) NOT NULL,
[TM] varchar(10) NULL,
[SQF_No] int NULL,
[ChargeNo] varchar(50) NULL,
[Event_From] varchar(100) NULL,
[Event_To] varchar(100) NULL,
[Temp_Set] decimal(9,3) NULL,
[Temp_Act] decimal(9,3) NULL,
[Cp_Set] decimal(9,3) NULL,
[Cp_Act] decimal(9,3) NULL,
[Oil_Set] decimal(9,3) NULL,
[Oil_Act] decimal(9,3) NULL,
[Jacket_Set] decimal(9,3) NULL,
[Jacket_Act] decimal(9,3) NULL,
[Fan_Status] bit NULL,
CONSTRAINT [PK_tblEvent]
PRIMARY KEY CLUSTERED ([ID])
);
END;
GOPractice 19 — Connect WinCC with SQL Server
Test an ADO connection from WinCC Runtime to SQF_DB.
- Replace YOUR_SERVER\INSTANCE with the real SQL Server instance.
- Use Windows authentication when the Runtime account has permission.
- Show the connection result and always close the connection.
On Error Resume Next
Dim Conn
Set Conn = CreateObject("ADODB.Connection")
Conn.Open "Provider=MSOLEDBSQL;" & _
"Server=YOUR_SERVER\INSTANCE;" & _
"Database=SQF_DB;" & _
"Trusted_Connection=Yes;"
If Err.Number <> 0 Then
HMIRuntime.Tags("SQL_Status").Write "SQL CONNECTION FAILED"
MsgBox "SQL Connection Failed" & vbCrLf & Err.Description
Err.Clear
Else
HMIRuntime.Tags("SQL_Status").Write "SQL CONNECTED"
MsgBox "SQL Server Connection Successful"
End If
If Conn.State = 1 Then Conn.Close
Set Conn = Nothing
On Error GoTo 0Practice 20 — Insert WinCC Tags into SQL
Save the complete SQF process snapshot into dbo.tblEvent.
- Read the WinCC tags.
- Escape text values and normalize decimal text for SQL.
- Insert DT with SYSDATETIME() and store TM as HH:MM:SS.
On Error Resume Next
Dim Conn, SQL, TM
Dim SQFNo, Charge, EventFrom, EventTo
Dim TempSet, TempAct, CpSet, CpAct, OilSet, OilAct, JacketSet, JacketAct, FanStatus
SQFNo = HMIRuntime.Tags("SQF_No").Read
Charge = Replace(CStr(HMIRuntime.Tags("ChargeNo").Read), "'", "''")
EventFrom = Replace(CStr(HMIRuntime.Tags("Event_From").Read), "'", "''")
EventTo = Replace(CStr(HMIRuntime.Tags("Event_To").Read), "'", "''")
TempSet = Replace(CStr(HMIRuntime.Tags("Temp_Set").Read), ",", ".")
TempAct = Replace(CStr(HMIRuntime.Tags("Temp_Act").Read), ",", ".")
CpSet = Replace(CStr(HMIRuntime.Tags("Cp_Set").Read), ",", ".")
CpAct = Replace(CStr(HMIRuntime.Tags("Cp_Act").Read), ",", ".")
OilSet = Replace(CStr(HMIRuntime.Tags("Oil_Set").Read), ",", ".")
OilAct = Replace(CStr(HMIRuntime.Tags("Oil_Act").Read), ",", ".")
JacketSet = Replace(CStr(HMIRuntime.Tags("Jacket_Set").Read), ",", ".")
JacketAct = Replace(CStr(HMIRuntime.Tags("Jacket_Act").Read), ",", ".")
FanStatus = HMIRuntime.Tags("Fan_Status").Read
TM = Right("0" & Hour(Now),2) & ":" & Right("0" & Minute(Now),2) & ":" & Right("0" & Second(Now),2)
Set Conn = CreateObject("ADODB.Connection")
Conn.Open "Provider=MSOLEDBSQL;Server=YOUR_SERVER\INSTANCE;Database=SQF_DB;Trusted_Connection=Yes;"
SQL = "INSERT INTO dbo.tblEvent (DT,TM,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) VALUES (" & _
"SYSDATETIME(),'" & TM & "'," & SQFNo & ",'" & Charge & "','" & EventFrom & "','" & EventTo & "'," & _
TempSet & "," & TempAct & "," & CpSet & "," & CpAct & "," & OilSet & "," & OilAct & "," & _
JacketSet & "," & JacketAct & "," & FanStatus & ")"
Conn.Execute SQL
If Err.Number <> 0 Then
HMIRuntime.Tags("SQL_Status").Write "INSERT FAILED"
MsgBox Err.Description
Err.Clear
Else
HMIRuntime.Tags("SQL_Status").Write "DATA SAVED"
End If
If Conn.State = 1 Then Conn.Close
Set Conn = Nothing
On Error GoTo 0Practice 21 — Read SQL Records
Read the latest historical row and load selected fields into WinCC tags.
- Open an ADODB.Connection and ADODB.Recordset.
- Select TOP 1 ordered by ID descending.
- Check EOF before accessing fields.
Dim Conn, RS, SQL
Set Conn = CreateObject("ADODB.Connection")
Set RS = CreateObject("ADODB.Recordset")
Conn.Open "Provider=MSOLEDBSQL;Server=YOUR_SERVER\INSTANCE;Database=SQF_DB;Trusted_Connection=Yes;"
SQL = "SELECT TOP 1 * FROM dbo.tblEvent ORDER BY ID DESC"
RS.Open SQL, Conn
If Not RS.EOF Then
HMIRuntime.Tags("SQL_ID").Write RS.Fields("ID").Value
HMIRuntime.Tags("ChargeNo").Write RS.Fields("ChargeNo").Value
HMIRuntime.Tags("Temp_Set").Write RS.Fields("Temp_Set").Value
HMIRuntime.Tags("Temp_Act").Write RS.Fields("Temp_Act").Value
HMIRuntime.Tags("Fan_Status").Write RS.Fields("Fan_Status").Value
Else
MsgBox "No SQL records found."
End If
RS.Close
Conn.Close
Set RS = Nothing
Set Conn = NothingPractice 22 — Update SQL Records
Modify a selected tblEvent row by ID.
- Load or enter the record ID into SQL_ID.
- Read the latest operator values.
- Execute UPDATE with a WHERE ID filter.
Dim Conn, SQL, ID, Charge, TempSet, TempAct
ID = HMIRuntime.Tags("SQL_ID").Read
Charge = Replace(CStr(HMIRuntime.Tags("ChargeNo").Read), "'", "''")
TempSet = Replace(CStr(HMIRuntime.Tags("Temp_Set").Read), ",", ".")
TempAct = Replace(CStr(HMIRuntime.Tags("Temp_Act").Read), ",", ".")
If ID > 0 Then
Set Conn = CreateObject("ADODB.Connection")
Conn.Open "Provider=MSOLEDBSQL;Server=YOUR_SERVER\INSTANCE;Database=SQF_DB;Trusted_Connection=Yes;"
SQL = "UPDATE dbo.tblEvent SET ChargeNo='" & Charge & "',Temp_Set=" & TempSet & ",Temp_Act=" & TempAct & " WHERE ID=" & ID
Conn.Execute SQL
Conn.Close
Set Conn = Nothing
Else
MsgBox "Enter a valid SQL ID."
End IfPractice 23 — Delete SQL Records
Delete one selected historical record safely after operator confirmation.
- Read SQL_ID.
- Ask for explicit confirmation.
- Execute DELETE with a WHERE ID condition only after Yes.
Dim Conn, SQL, ID, Answer
ID = HMIRuntime.Tags("SQL_ID").Read
If ID > 0 Then
Answer = MsgBox("Delete SQL Record ID " & ID & "?", vbYesNo + vbExclamation, "Confirm Delete")
If Answer = vbYes Then
Set Conn = CreateObject("ADODB.Connection")
Conn.Open "Provider=MSOLEDBSQL;Server=YOUR_SERVER\INSTANCE;Database=SQF_DB;Trusted_Connection=Yes;"
SQL = "DELETE FROM dbo.tblEvent WHERE ID=" & ID
Conn.Execute SQL
Conn.Close
Set Conn = Nothing
End If
Else
MsgBox "Enter a valid SQL ID."
End IfPractice 24 — Search Historical Records
Find the latest database record for a charge number.
- Ask the operator for ChargeNo.
- Escape apostrophes in the search text.
- Select the latest matching row and display its ID/time.
Dim Conn, RS, SQL, Charge
Charge = InputBox("Enter Charge Number", "Historical Search")
If Charge <> "" Then
Charge = Replace(Charge, "'", "''")
Set Conn = CreateObject("ADODB.Connection")
Set RS = CreateObject("ADODB.Recordset")
Conn.Open "Provider=MSOLEDBSQL;Server=YOUR_SERVER\INSTANCE;Database=SQF_DB;Trusted_Connection=Yes;"
SQL = "SELECT TOP 1 * FROM dbo.tblEvent WHERE ChargeNo='" & Charge & "' ORDER BY DT DESC"
RS.Open SQL, Conn
If Not RS.EOF Then
HMIRuntime.Tags("SQL_ID").Write RS.Fields("ID").Value
MsgBox "Record found" & vbCrLf & "ID = " & RS.Fields("ID").Value & vbCrLf & "DT = " & RS.Fields("DT").Value
Else
MsgBox "Charge number not found."
End If
RS.Close
Conn.Close
Set RS = Nothing
Set Conn = Nothing
End IfPractice 25 — Log Alarms into SQL
Store a high-temperature transition as an event record in tblEvent.
- Use a one-shot/edge condition so one alarm transition produces one row.
- Read the relevant process values.
- Insert an event description such as HIGH TEMPERATURE ALARM.
Dim TempSet, TempAct
TempSet = HMIRuntime.Tags("Temp_Set").Read
TempAct = HMIRuntime.Tags("Temp_Act").Read
If TempAct > TempSet + 20 Then
HMIRuntime.Tags("Event_From").Write "PROCESS"
HMIRuntime.Tags("Event_To").Write "HIGH TEMPERATURE ALARM"
' Call the approved SQL event-insert action here once per alarm transition.
End IfPractice 26 — Log Production Data
Record charge/process transitions such as Charging → Heating.
- Set ChargeNo, Event_From and Event_To from operator/process state.
- Capture the current set/actual values.
- Execute the same validated insert routine used in Practice 20 at the event transition.
HMIRuntime.Tags("ChargeNo").Write "CHG-2026-001"
HMIRuntime.Tags("Event_From").Write "Charging"
HMIRuntime.Tags("Event_To").Write "Heating"
' Then execute the validated tblEvent INSERT routine
' at the actual production state transition.Practice 27 — Generate Excel Report
Export the complete tblEvent history from SQL Server into Microsoft Excel.
- Query the required SQL columns.
- Create Excel.Application and a new workbook.
- Write headings and loop through every record.
- AutoFit columns and save the workbook.
On Error Resume Next
Dim Conn, RS, SQL, XL, WB, WS, Row
Set Conn = CreateObject("ADODB.Connection")
Set RS = CreateObject("ADODB.Recordset")
Conn.Open "Provider=MSOLEDBSQL;Server=YOUR_SERVER\INSTANCE;Database=SQF_DB;Trusted_Connection=Yes;"
SQL = "SELECT ID,DT,TM,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 FROM dbo.tblEvent ORDER BY DT"
RS.Open SQL, Conn
Set XL = CreateObject("Excel.Application")
XL.Visible = True
Set WB = XL.Workbooks.Add
Set WS = WB.Worksheets(1)
WS.Name = "SQF Event Report"
WS.Cells(1,1).Value = "ID"
WS.Cells(1,2).Value = "Date Time"
WS.Cells(1,3).Value = "Time"
WS.Cells(1,4).Value = "SQF No"
WS.Cells(1,5).Value = "Charge No"
WS.Cells(1,6).Value = "Event From"
WS.Cells(1,7).Value = "Event To"
WS.Cells(1,8).Value = "Temp Set"
WS.Cells(1,9).Value = "Temp Actual"
WS.Cells(1,10).Value = "CP Set"
WS.Cells(1,11).Value = "CP Actual"
WS.Cells(1,12).Value = "Oil Set"
WS.Cells(1,13).Value = "Oil Actual"
WS.Cells(1,14).Value = "Jacket Set"
WS.Cells(1,15).Value = "Jacket Actual"
WS.Cells(1,16).Value = "Fan Status"
Row = 2
Do While Not RS.EOF
WS.Cells(Row,1).Value = RS.Fields("ID").Value
WS.Cells(Row,2).Value = RS.Fields("DT").Value
WS.Cells(Row,3).Value = RS.Fields("TM").Value
WS.Cells(Row,4).Value = RS.Fields("SQF_No").Value
WS.Cells(Row,5).Value = RS.Fields("ChargeNo").Value
WS.Cells(Row,6).Value = RS.Fields("Event_From").Value
WS.Cells(Row,7).Value = RS.Fields("Event_To").Value
WS.Cells(Row,8).Value = RS.Fields("Temp_Set").Value
WS.Cells(Row,9).Value = RS.Fields("Temp_Act").Value
WS.Cells(Row,10).Value = RS.Fields("Cp_Set").Value
WS.Cells(Row,11).Value = RS.Fields("Cp_Act").Value
WS.Cells(Row,12).Value = RS.Fields("Oil_Set").Value
WS.Cells(Row,13).Value = RS.Fields("Oil_Act").Value
WS.Cells(Row,14).Value = RS.Fields("Jacket_Set").Value
WS.Cells(Row,15).Value = RS.Fields("Jacket_Act").Value
WS.Cells(Row,16).Value = RS.Fields("Fan_Status").Value
Row = Row + 1
RS.MoveNext
Loop
WS.Rows(1).Font.Bold = True
WS.Columns.AutoFit
WB.SaveAs "D:\SQF_Event_Report.xlsx"
RS.Close
Conn.Close
Set RS = Nothing
Set Conn = Nothing
Set WS = Nothing
Set WB = Nothing
Set XL = Nothing
On Error GoTo 0Practice 28 — Date-Wise Excel Report
Generate a daily report using DT as the authoritative SQL timestamp.
- Ask for a date in YYYY-MM-DD format.
- Filter with a half-open range: DT >= date and DT < DATEADD(DAY,1,date).
- Export only the returned rows to Excel and create a date-specific filename.
Dim ReportDate, Conn, RS, SQL, XL, WB, WS, Row, FileName
ReportDate = InputBox("Enter Report Date (YYYY-MM-DD)", "SQF Date-wise Report")
If ReportDate <> "" Then
Set Conn = CreateObject("ADODB.Connection")
Set RS = CreateObject("ADODB.Recordset")
Conn.Open "Provider=MSOLEDBSQL;Server=YOUR_SERVER\INSTANCE;Database=SQF_DB;Trusted_Connection=Yes;"
SQL = "SELECT ID,DT,TM,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 " & _
"FROM dbo.tblEvent WHERE DT >= '" & ReportDate & "' AND DT < DATEADD(DAY,1,'" & ReportDate & "') ORDER BY DT"
RS.Open SQL, Conn
If RS.EOF Then
MsgBox "No records found for " & ReportDate
Else
Set XL = CreateObject("Excel.Application")
XL.Visible = True
Set WB = XL.Workbooks.Add
Set WS = WB.Worksheets(1)
WS.Name = "SQF Daily Report"
WS.Cells(1,1).Value = "SQF DAILY REPORT"
WS.Cells(2,1).Value = "Report Date"
WS.Cells(2,2).Value = ReportDate
WS.Cells(4,1).Value = "ID"
WS.Cells(4,2).Value = "Date Time"
WS.Cells(4,3).Value = "SQF No"
WS.Cells(4,4).Value = "Charge No"
WS.Cells(4,5).Value = "Event From"
WS.Cells(4,6).Value = "Event To"
WS.Cells(4,7).Value = "Temp Set"
WS.Cells(4,8).Value = "Temp Actual"
Row = 5
Do While Not RS.EOF
WS.Cells(Row,1).Value = RS.Fields("ID").Value
WS.Cells(Row,2).Value = RS.Fields("DT").Value
WS.Cells(Row,3).Value = RS.Fields("SQF_No").Value
WS.Cells(Row,4).Value = RS.Fields("ChargeNo").Value
WS.Cells(Row,5).Value = RS.Fields("Event_From").Value
WS.Cells(Row,6).Value = RS.Fields("Event_To").Value
WS.Cells(Row,7).Value = RS.Fields("Temp_Set").Value
WS.Cells(Row,8).Value = RS.Fields("Temp_Act").Value
Row = Row + 1
RS.MoveNext
Loop
WS.Rows(4).Font.Bold = True
WS.Columns.AutoFit
FileName = "D:\SQF_Report_" & Replace(ReportDate,"-","_") & ".xlsx"
WB.SaveAs FileName
MsgBox "Date-wise report created:" & vbCrLf & FileName
End If
RS.Close
Conn.Close
Set RS = Nothing
Set Conn = Nothing
End IfContinue the WinCC / Industry 4.0 Learning Path
After completing the 28 practicals, continue with deeper SQL reporting, Python reporting and Industry 4.0 integration.
WinCC VBScripting Practice FAQ
What is WinCC VBScripting Practice?
It is a hands-on learning sequence for SIMATIC WinCC Classic/Explorer that uses VBScript to read and write tags, control graphics, execute process logic, connect to SQL Server and create reports.
Can WinCC VBScript write process data to SQL Server?
Yes. A WinCC VBScript can create an ADODB.Connection, execute INSERT, SELECT, UPDATE and DELETE statements, and use WinCC tag values as process data.
Which SQL table is used in these practicals?
The examples use SQF_DB.dbo.tblEvent with timestamp, SQF number, charge number, event, temperature, CP, oil, jacket and fan-status columns.
Can WinCC VBScript generate an Excel report?
Yes. The reporting practicals create Excel.Application through COM automation, read SQL Server records and write them into a formatted Excel workbook.
