WinCC Explorer · VBScript · Practical Tutorial

SQL Server CRUD Operations with WinCC VBScript and ADODB

Insert, read, update and delete SQL Server records from WinCC using VBScript, ADODB, parameters, transactions and error handling.

Lab Overview

WinCC VBS Lab 7Estimated time: 90 minutesDifficulty: Advanced

Prerequisites / What You’ll Need

  • Backed-up WinCC Explorer test project
  • Graphics Designer and Global Script VBS access
  • Internal test tags and Runtime diagnostics

SQL Server CRUD Operations with WinCC VBScript and ADODB

This lab targets wincc vbscript sql server crud with copy-ready examples, expected results and diagnostic guidance.

Safety: Test on internal tags or an approved simulation system. Do not bypass PLC permissives, interlocks, user authorization or plant change control.

1. Use a Least-Privilege ADODB Connection

Dim connection
Set connection = CreateObject("ADODB.Connection")
connection.Open "Provider=MSOLEDBSQL;Server=SOFTWELL\WINCC;" & _
    "Database=SQF_DB;Trusted_Connection=Yes;"

If connection.State <> 1 Then
    HMIRuntime.Trace "SQL connection did not open"
End If

Use the installed, approved provider and the Runtime service identity. Grant only the required operations on the intended tables.

2. Insert with ADODB Parameters

Dim command
Set command = CreateObject("ADODB.Command")
Set command.ActiveConnection = connection
command.CommandText = "INSERT INTO dbo.ProcessLog " & _
    "(EventTime, Temperature) VALUES (?, ?)"
command.CommandType = 1
command.Parameters.Append command.CreateParameter("pTime", 135, 1, , Now)
command.Parameters.Append command.CreateParameter("pTemp", 5, 1, , 650.5)
command.Execute

Parameter markers represent values, not table or column names. Keep identifiers as reviewed constants.

3. Read Records Safely

Dim recordset
Set recordset = connection.Execute( _
    "SELECT TOP (10) EventTime, Temperature " & _
    "FROM dbo.ProcessLog ORDER BY EventTime DESC")

Do Until recordset.EOF
    HMIRuntime.Trace CStr(recordset.Fields("EventTime").Value) & _
        " | " & CStr(recordset.Fields("Temperature").Value)
    recordset.MoveNext
Loop

4. Update and Delete with Guard Conditions

Every UPDATE and DELETE needs a reviewed WHERE clause based on a key. Test against disposable rows, check affected-record counts and never expose unrestricted SQL text to an operator field.

5. Transactions and Cleanup

Use BeginTrans, CommitTrans and RollbackTrans when several writes must succeed together. Close the recordset, release the command, close the connection and handle rollback on every failure path.

6. SQL CRUD Troubleshooting

ProblemAction
Provider not foundInstall/approve the matching OLE DB provider and check process bitness
Login failedVerify the WinCC Runtime identity and SQL permissions
Parameter type errorMatch ADODB parameter type, size and direction to the SQL column
Database remains lockedClose recordsets/connections and avoid long transactions in Runtime scripts

Hands-On Verification Lab

Hands-on
1

Prepare a controlled test

Create only the internal tags, folders or disposable database rows required by this tutorial.

The test scope is isolated and documented.
2

Run the smallest example

Execute one operation and inspect WinCC diagnostics before expanding the script.

The expected value, file, object or database result appears once.
3

Test a failure path

Use a safe backup copy to test an invalid tag, path or disposable input.

The script records a useful error and cleans up without an uncontrolled action.

Frequently Asked Questions

Should this WinCC VBScript be tested in production?

No. Use a backed-up training or staging project and follow the plant change-control process.

Which WinCC versions does the example target?

The examples target classic WinCC Explorer V7.x/V8.x. Verify object properties, action signatures and providers in the installed WinCC help.

How should Runtime failures be diagnosed?

Use scoped Err checks, WinCC object diagnostics and HMIRuntime.Trace while recording the exact station, trigger, tag and Runtime identity.

Get the WinCC VB Scripting syllabus

Share your details and a Softwell advisor will contact you with training options.

Build reliable WinCC VBScript projects

Join practical online, classroom or corporate training.

Request Course Details
Verified learning pathway

Discuss WinCC VB Scripting Training

Explore practical WinCC VBS, SCADA, SQL reporting and industrial automation training options.

Content reviewed: 4 August 2026

☎ Call WhatsApp ✉ Email Enquire Now