Reading MsgFilterSQL String from WinCC Alarm Control in TIA

David Krause11 min read
SiemensTutorial / How-toWinCC
Licensed PE Working through this on a live machine? A Maine-licensed engineer can take it from here — included with IMD hardware, by the hour for everything else. Book an engineer

Overview of the MsgFilterSQL Property

The MsgFilterSQL property on the WinCC Alarm Control defines a SQL-style WHERE clause that limits the alarm records displayed in the alarm view at runtime. In WinCC Runtime Professional (RT Professional), the property is exposed through the Alarm Control's runtime object model and accepts a subset of SQL that maps to the underlying alarm logging tables. The TIA Portal help system documents the syntax as "SQL Statements for Filtering the Alarm View" and lists each available filter column, its type, and an example statement (SQL Statements for Filtering the Alarm View, RT Professional).

When an engineer configures an Alarm Control in the TIA Portal editor, the engineering tool generates an internal default filter based on the visible columns, alarm classes, areas, sort order, and selection rules. The active filter string at runtime may differ from the engineering default for three reasons:

  • Application code reassigns MsgFilterSQL dynamically through a VB Script or C# action.
  • The operator interacts with the column headers or filter row in the alarm view and the control appends ad-hoc predicates.
  • A selection-based filter is layered on top of the base filter through the Selection property or the MsgFilterSQL follow-up filter.

Identifying the exact string that is currently applied is a recurring task during commissioning, regression testing, and alarm-log auditing. The remainder of this article documents the procedure that survives every TIA Portal version from V14 through V21.

Problem: DefaultMsgFilterSQL No Longer Exposed in WinCC Professional V14

Siemens Knowledge Base entry 109482844 (5668269) - How to use the "MsgFilterSQL" property of the WinCC Alarm Control historically described a static-property path: Object Properties > Control Properties > DefaultMsgFilterSQL > Static tab. Engineers porting HMI projects from WinCC flexible 2008, WinCC V13, or earlier WinCC V13 SP1 builds to WinCC Professional V14 cannot locate the Static tab in the TIA Portal Inspector.

The root cause is an Inspector reorganization that landed with WinCC Professional V14. The control-property node was renamed, promoted to a runtime-only accessor, and is no longer surfaced as a configurable static value. TIA Portal V15, V15.1, V16, V17, V18, V19, V20, and V21 keep the same accessor-only model. The active filter must therefore be read from the runtime object through a script.

Compatibility note: The VB Script workaround described below works in TIA Portal V14 SP1 through V21 for WinCC Runtime Professional. The WinCC Comfort Panels and WinCC Runtime Advanced use a different filter syntax and a different runtime object model; that path is summarized in the "Supported Platforms" section below.

Prerequisites

  • TIA Portal V14 SP1 or later with WinCC Professional option package installed.
  • WinCC Runtime Professional (RT Professional) license or a WinCC ES simulation runtime on the engineering station.
  • A configured WinCC Alarm Control placed on an HMI screen, bound to a valid alarm source (AS symbols, Alarm_8P tags, or logged tags).
  • VB Script runtime enabled: Runtime Settings > Scripts > VB > Enable VB scripts.
  • User rights sufficient to trigger the script on the operator panel; the technique works in both RT simulation and on a physical Comfort Panel / IPC runtime.

Step-by-Step: Reading the Active Filter via VB Script

  1. Open the HMI screen that contains the Alarm Control in the TIA Portal editor.
  2. Identify the exact name of the Alarm Control object. The default is Control1; renamed controls are visible in the Properties > General > Name field.
  3. From the toolbox, drag a Button onto the same screen and rename it to btnReadFilter for clarity.
  4. Right-click the button, select Properties > Events > Click, and create a new VB Script action.
  5. Paste the canonical read-and-display script:
    Sub OnClick(ByVal item)
        Dim sFilter
        sFilter = ScreenItems("Control1").MsgFilterSQL
        MsgBox sFilter
    End Sub
    
  6. Replace Control1 with the actual name of the Alarm Control if it differs from the default.
  7. Compile the HMI project (Project tree > HMI device > Compile > Software (rebuild all)).
  8. Start the WinCC Runtime Simulation from the toolbar (or transfer the project to the target device).
  9. Click the button. A MsgBox appears with the SQL string that the Alarm Control is currently applying.
Performance note: Reading MsgFilterSQL is a cheap accessor call. Avoid polling it from a global scheduler; instead, trigger the read on demand from a button, an alarm-event handler, or a tag change event to keep the HMI thread responsive.

Enhanced Script: Logging the Filter for Diagnostics

For commissioning, the filter string is often needed headless (no operator available to dismiss a MsgBox). The script below writes the active filter to both an internal tag and a text file so it can be reviewed after the fact:

Sub OnClick(ByVal item)
    Dim sFilter, oFS, oTS

    sFilter = ScreenItems("Control1").MsgFilterSQL

    ' Mirror to an HMI tag for in-panel display
    SmartTags("FilterString") = sFilter

    ' Append to a log file with a timestamp
    Set oFS = CreateObject("Scripting.FileSystemObject")
    Set oTS = oFS.OpenTextFile("C:\Temp\FilterSQL.log", 8, True) ' 8 = ForAppending
    oTS.WriteLine Now & " | " & sFilter
    oTS.Close

    Set oTS = Nothing
    Set oFS = Nothing
End Sub

Declare the FilterString tag in the HMI tag table as a WSTRING with a length of at least 1000 characters; the runtime filter statement routinely reaches 400-600 characters when multiple column filters are active.

On a Comfort Panel that lacks a writable C:\Temp folder, swap the path for the panel's Storage Card location, e.g. \Storage Card\SIMATIC\FilterSQL.log, and ensure the user "Operator" has write permission to the path.

MsgFilterSQL SQL Syntax Reference (RT Professional)

The MsgFilterSQL parser in RT Professional accepts a SQL SELECT fragment over the internal alarm-log virtual table. The TIA Portal V21 reference pages document the following top-level elements (SQL Statements for Filtering the Alarm View):

SQL Element Type Default / Range Description
Filter (row limit) Integer 10000 Maximum number of alarms to output. Use SELECT TOP 5000 ... to lower the row count for slow networks.
Date/Time DATETIME Full runtime window Filter the time window of the alarm source.
State Integer 0 / 1 / 2 / 3 / 4 Came in (1), went out (2), acknowledged (3), locked (4). Combine with IN.
Priority Integer 0 - 16 Alarm priority as configured on the AS side.
Class String Class name Alarm class name; quoted string, locale-invariant.
Area String Area name Filter by AS area hierarchy.
Source / Origin String Tag name Filter by the originating tag or AS symbol.
Comment / Operator String Free text Filter by comment text or operator name (case-sensitive by default).

Supported Columns, Data Types, and Operators

The filter columns map to alarm-log table columns. The TIA Portal help lists the canonical column identifiers; the table below summarizes the operators that the parser accepts for each column type. String comparisons follow the Microsoft Jet/DASL wildcard conventions (Filtering Items Using a String Comparison - Microsoft Learn): % substitutes for any sequence of characters, _ for a single character.

Column Data Type Supported Operators Notes
MsgNumber Integer =, <>, <, >, <=, >=, IN, BETWEEN AS-side message number; IN (1,2,3) supported.
Priority Integer =, <>, <, >, IN 0 = informational, 16 = highest.
State Integer =, IN Bit-OR combinations not supported.
TimeStamp DATETIME =, BETWEEN, <, > Locale-independent ISO-8601 format recommended.
ClassID / ClassName String =, LIKE, IN Case-sensitive; wrap wildcards around partial matches.
Source String =, LIKE AS-side symbol path; wildcards with %.
Comment String =, LIKE Operator-entered comment text.
Important: RT Professional does not support JOIN, sub-selects, or aggregate functions in MsgFilterSQL. Statements that include those clauses are rejected at parse time and the Alarm Control falls back to the engineering default.

Common Filter Examples

-- Only unacknowledged alarms raised in the last hour
SELECT * FROM ALOG
WHERE State IN (1, 2)
  AND TimeStamp >= DATEADD(hour, -1, GETDATE())

-- High-priority alarms from a specific area, capped at 500 rows
SELECT TOP 500 * FROM ALOG
WHERE Priority >= 12
  AND Area = 'Plant/Line3'

-- Wildcard match on source symbol
SELECT * FROM ALOG
WHERE Source LIKE 'DB100.DBX%'

-- Time-window filter with explicit ISO-8601 timestamps
SELECT * FROM ALOG
WHERE TimeStamp BETWEEN '2025-01-01T00:00:00' AND '2025-01-31T23:59:59'

These statements can be assigned directly to ScreenItems("Control1").MsgFilterSQL at runtime through VB Script or C# to swap the active filter without re-compiling the project.

Alternative Methods to Inspect the Active Filter

When VB Script execution is disabled (regulated environments) or the runtime is headless (WinCC Operator Station in service mode), three additional methods can be used to surface the active filter:

  • CSV export from the Alarm Control: the on-screen toolbar exposes an Export to CSV action. The exported file contains a header line that lists the resolved filter at export time. Inspect ExportOptions.Filter in the editor to pre-configure.
  • WinCC OLE DB provider: query the underlying alarm-log table directly through the WinCC OLE DB provider and correlate the row count against the alarm view to verify the filter is effective.
  • Project XML inspection: the compiled HMI project contains the configured default filter under the Alarm Control node. This shows the engineering default, not the runtime override, so it is only a starting point.

Supported Platforms and Version Matrix

TIA Portal Version WinCC Variant MsgFilterSQL Behavior
V13 / V13 SP1 WinCC Professional DefaultMsgFilterSQL visible in Inspector > Static tab.
V14 / V14 SP1 WinCC Professional DefaultMsgFilterSQL removed from Inspector; runtime accessor only.
V15 / V15.1 WinCC Professional / RT Professional Runtime accessor only; new MsgFilterSQL column list documented.
V16 / V17 / V18 RT Professional Same runtime accessor; expanded SQL subset.
V19 / V20 / V21 RT Professional Current reference; SQL subset documented online.
V16+ WinCC Comfort Panels / RT Advanced Different SQL subset (see "SQL Statements for Filtering the Alarm View" reference).

Troubleshooting Matrix

Symptom Likely Cause Resolution
MsgBox shows empty string Alarm Control not initialized, or no filter is currently applied. Wait for the alarm view to populate before clicking; verify the control is on the active screen and not behind another layer.
Runtime error "Object doesn't support this property or method" Screen item name mismatch or non-Alarm-Control object. Verify ScreenItems("Control1") matches the actual control name; use the Properties > Name field.
Script does not execute on click VB Script runtime disabled in Runtime Settings. Enable Runtime Settings > Scripts > VB > Enable VB scripts; recompile and retransfer.
Filter string truncated at 256 characters Target tag declared as STRING instead of WSTRING. Re-declare the tag as WSTRING with length >= 1000.
File write access denied Operator user lacks rights on the target path. Use the Storage Card path on Comfort Panels or grant the runtime user write access to the folder.
MsgFilterSQL returns the engineering default, not the runtime override Override applied through a child control (e.g. embedded AlarmView). Read the parent Alarm Control's property; nested alarm views each have their own MsgFilterSQL.
Filter valid in script but view shows no alarms Predicate references a column not in the visible schema. Reference the SQL Statements reference to confirm the column exists in the target TIA Portal version.

Verification and Runtime Acceptance Test

  1. Configure an Alarm Control with the engineering default filter; do not modify any property.
  2. Place the btnReadFilter button on the same screen and load the read-and-display script.
  3. Compile the project and start RT simulation.
  4. Click the button. Confirm the MsgBox displays a non-empty SELECT ... FROM ALOG ... statement.
  5. Add a second action that assigns a known string, e.g. SELECT * FROM ALOG WHERE Priority >= 12, to ScreenItems("Control1").MsgFilterSQL.
  6. Click the button again. Confirm the MsgBox now displays the new filter.
  7. Reset the alarm view and verify the row count changes as expected when the priority filter is applied.
  8. Document the engineering default and any runtime overrides in the project's Functional Specification.

Frequently Asked Questions

Why is DefaultMsgFilterSQL missing in TIA Portal V14?

The property was reorganized when TIA Portal V14 introduced the unified WinCC Professional editor. The Static tab path described in older Siemens documentation no longer applies; the active filter is now only accessible as a runtime property through the Alarm Control's object model. Use the VB Script procedure in this article to read it.

Can I set MsgFilterSQL from a VB Script or C# action?

Yes. Assign the property directly: ScreenItems("Control1").MsgFilterSQL = "SELECT * FROM ALOG WHERE Priority >= 12". The alarm view refreshes on the next property change cycle; call ScreenItems("Control1").ApplyFilter() if the control exposes that method in your TIA Portal version.

What is the maximum number of alarms returned by MsgFilterSQL?

The default row limit is 10000. Lower it with SELECT TOP n ... at the head of the statement. The TIA Portal help system documents the integer parameter under the Filter SQL element.

Does MsgFilterSQL support JOINs, sub-selects, or aggregates?

No. RT Professional's MsgFilterSQL parser accepts only a single-table SELECT with WHERE, ORDER BY, and TOP. JOINs, sub-selects, GROUP BY, and HAVING are rejected at parse time and the Alarm Control falls back to the engineering default filter.

Is the same property available on WinCC Comfort Panels and RT Advanced?

Yes, but with a different SQL subset. Comfort Panels and RT Advanced use the Basic Panels / Panels Comfort filter syntax documented in the TIA Portal V21 reference. The runtime accessor is named identically (MsgFilterSQL) but the column list and operators differ; always verify against the current TIA Portal help for the target platform.

How do I capture the filter on a headless WinCC Operator Station?

Replace the MsgBox in the script with a file write or an HMI tag assignment as shown in the "Enhanced Script" section. On a Comfort Panel, write to the Storage Card path; on an IPC runtime, write to a network share that is accessible from the engineering station.

Back to blog