0% found this document useful (0 votes)
57 views2 pages

Scrips Wincc Database

This document provides examples of using Visual Basic Script (VBS) to configure database connections in WinCC. The examples demonstrate writing a tag value from WinCC to an Access database using an ODBC connection and reading a value from the database to write to a WinCC tag. The procedures create an Access database with a table and columns, set up an ODBC data source, and use ADO to open a connection, execute SQL commands to insert and retrieve data, and close the connection.

Uploaded by

Thanh Chung Tran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views2 pages

Scrips Wincc Database

This document provides examples of using Visual Basic Script (VBS) to configure database connections in WinCC. The examples demonstrate writing a tag value from WinCC to an Access database using an ODBC connection and reading a value from the database to write to a WinCC tag. The procedures create an Access database with a table and columns, set up an ODBC data source, and use ADO to open a connection, execute SQL commands to insert and retrieve data, and close the connection.

Uploaded by

Thanh Chung Tran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Document: WinCC: Scripting (VBS, ANSI-C, VBA) (02/2016, English)

Type of Example
topic:

Example: Configuring a Database Connection with VBS

Introduction
The following examples describe the configuration of an Access database link via an ODBC driver.

 Example 1 writes a tag value from WinCC in an Access database.


 Example 2 reads a value from the database and writes it in a WinCC tag.
The examples do not contain any handling faults.

Procedure, Example 1
1. Create the Access database with the WINCC_DATA table and columns (ID, TagValue) with the ID as
the Auto Value.
2. Set up the ODBC data source with the name "SampleDSN", reference to the above Access database.
3. Programming.

Example 1

'VBS108
Dim objConnection
Dim strConnectionString
Dim lngValue
Dim strSQL
Dim objCommand
strConnectionString = "Provider=MSDASQL;DSN=SampleDSN;UID=;PWD=;"
lngValue = HMIRuntime.Tags("Tag1").Read
strSQL = "INSERT INTO WINCC_DATA (TagValue) VALUES (" & lngValue & ");"
Set objConnection = CreateObject("ADODB.Connection")
objConnection.ConnectionString = strConnectionString
objConnection.Open
Set objCommand = CreateObject("ADODB.Command")
With objCommand
    .ActiveConnection = objConnection
    .CommandText = strSQL
End With
objCommand.Execute
Set objCommand = Nothing
objConnection.Close
Set objConnection = Nothing

Procedure, Example 2

1. Create the WinCC tag with the name dbValue.


2. Create Access database with WINCC_DATA table and ID, TagValue columns: ID, create TagValue (ID
as auto value).
3. Set up the ODBC data source with the name "SampleDSN", reference to the above Access database.
4. Programming.
VBS108a
Dim objConnection
Dim objCommand
Dim objRecordset
Dim strConnectionString
Dim strSQL
Dim lngValue
Dim lngCount
strConnectionString = "Provider=MSDASQL;DSN=SampleDSN;UID=;PWD=;"
strSQL = "select TagValue from WINCC_DATA where ID = 1"
Set objConnection = CreateObject("ADODB.Connection")
objConnection.ConnectionString = strConnectionString
objConnection.Open
Set objRecordset = CreateObject("ADODB.Recordset")
Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection
objCommand.CommandText = strSQL
Set objRecordset = objCommand.Execute
lngCount = objRecordset.Fields.Count
If (lngCount>0) Then
objRecordset.movefirst
lngValue = objRecordset.Fields(0).Value
HMIRuntime.Tags("dbValue").Write lngValue
Else
HMIRuntime.Trace "Selection returned no fields" & vbNewLine
End If
Set objCommand = Nothing
objConnection.Close
Set objRecordset = Nothing
Set objConnection = Nothing

There are several ways in which to define the ConnectionString for the connection depending on the provider
used:

Microsoft OLE DB provider for ODBC

Enables connections to any ODBC data source. The corresponding syntax is:

"[Provider=MSDASQL;]{DSN=name|FileDSN=filename};
[DATABASE=database;]UID=user; PWD=password"

Other Microsoft OLE DB Providers (e.g. MS Jet, MS SQL Server)

It is possible to work without DSN. The corresponding syntax is:

"[Provider=provider;]DRIVER=driver; SERVER=server;
DATABASE=database; UID=user; PWD=password"

You might also like