Sunday, 2 October 2016

WINCC VBScript - Read data from database

This script is common for Wincc Advanced, Wincc Professional. For Wincc 7.x the tag declaration will change

Dim objConnection
Dim objCommand
Dim objRecordset
Dim strConnectionString
Dim strSQL
Dim lngValue
Dim lngCount
Dim strfile

strfile = "C:\Reports\Database.mdb"
strConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strfile & ";"

strSQL = "select * from DQ_LOG where DATE_TIME >= CDATE('"& Start_Time &"') And  DATE_TIME <= CDATE('"& End_Time &"')"

Set objConnection = CreateObject("ADODB.Connection")
objConnection.ConnectionString = strConnectionString
objConnection.Open
Set objRecordset = CreateObject("ADODB.Recordset")
Set objCommand = CreateObject("ADODB.Command")
Set objCommand.ActiveConnection = objConnection
objCommand.CommandText = strSQL
Set objRecordset = objCommand.Execute

Smarttags("Tagvalue1")  = objRecordset.Fields(0).Value
Smarttags("Tagvalue2")  = objRecordset.Fields(1).Value

Set objCommand = Nothing
objConnection.Close
Set objRecordset = Nothing
Set objConnection = Nothing

Steps:
1. Simply copy paste this script
2. Do minor changes as per your application.
Enough!! 

No comments:

Post a Comment