Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

WinCC Scripting: VBS, ANSI-C, VBA

Example: Configuring a Database Connection with VBS

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.

This document constitutes a free excerpt compiled by the user himself/herself from the documentation provided by Siemens for this product. Siemens disclaims
all liability for the completeness of this document. It shall only be used for the user's own internal purposes. It shall not be passed on to third parties. The complete
documentation can be found at:
/dokumentation/default.aspx?DocVersionId=53797698699&Language=en-US&TopicId=45950277643 1/17/2017
WinCC Scripting: VBS, ANSI-C, VBA
Example: Configuring a Database Connection with VBS

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


This document constitutes a free excerpt compiled by the user himself/herself from the documentation provided by Siemens for this product. Siemens disclaims
all liability for the completeness of this document. It shall only be used for the user's own internal purposes. It shall not be passed on to third parties. The complete
documentation can be found at:
/dokumentation/default.aspx?DocVersionId=53797698699&Language=en-US&TopicId=45950277643 1/17/2017
WinCC Scripting: VBS, ANSI-C, VBA
Example: Configuring a Database Connection with VBS

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.

This document constitutes a free excerpt compiled by the user himself/herself from the documentation provided by Siemens for this product. Siemens disclaims
all liability for the completeness of this document. It shall only be used for the user's own internal purposes. It shall not be passed on to third parties. The complete
documentation can be found at:
/dokumentation/default.aspx?DocVersionId=53797698699&Language=en-US&TopicId=45950277643 1/17/2017
WinCC Scripting: VBS, ANSI-C, VBA
Example: Configuring a Database Connection with VBS

Example 2

'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
This document constitutes a free excerpt compiled by the user himself/herself from the documentation provided by Siemens for this product. Siemens disclaims
all liability for the completeness of this document. It shall only be used for the user's own internal purposes. It shall not be passed on to third parties. The complete
lngValue = can
documentation objRecordset.Fields(0).Value
be found at:
/dokumentation/default.aspx?DocVersionId=53797698699&Language=en-US&TopicId=45950277643 1/17/2017

HMIRuntime.Tags("dbValue").Write lngValue
WinCC Scripting: VBS, ANSI-C, VBA
Example: Configuring a Database Connection with VBS

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"

See also
→ General examples for VBScript

This document constitutes a free excerpt compiled by the user himself/herself from the documentation provided by Siemens for this product. Siemens disclaims
all liability for the completeness of this document. It shall only be used for the user's own internal purposes. It shall not be passed on to third parties. The complete
documentation can be found at:
/dokumentation/default.aspx?DocVersionId=53797698699&Language=en-US&TopicId=45950277643 1/17/2017

You might also like