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.
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 & ");"
objConnection.ConnectionString = strConnectionString
objConnection.Open
With objCommand
.ActiveConnection = objConnection
.CommandText = strSQL
End With
objCommand.Execute
objConnection.Close
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=;"
objConnection.ConnectionString = strConnectionString
objConnection.Open
objCommand.ActiveConnection = objConnection
objCommand.CommandText = strSQL
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"
"[Provider=provider;]DRIVER=driver; SERVER=server;
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