开发Excel中的接口以访问SCADA应用程序中的数据库(Schneider的ClearScada)
能够在Excel后面的Visual Basic中列出对象的所有属性的能力。
非常容易根据“ Inservice”之类的属性列出所有对象。并创建所有对象的数组。
Set oPnts = myFolder.List("CDBObject")
For Each item In oPnts
Set oObj = oServ.FindObject(oPnts.item(Count + 1).FullName)
Next Item
但我无法列出对象的所有属性。像cndp3analogin。
我想做这样的事情。 oobj.list并获取与对象关联的所有属性的数组。
我如何解决此问题当前是硬编码与对象关联的所有属性,并使用“ case select”语句并将其打印为Excel。此方法不是动态的,也不能解决用.xml文件添加的新属性的问题。我认为问题可能与在对象上使用“列表”之类的方法的能力有关。
要撰写一些C从另一个角度来解决这个问题。只是向正确的方向推动会很棒!
构建详细信息:
Excel2016 Microsoft Visual Basic for Application 7.1 VBA:零售7.1.1056
'***************************************************************************************
'Name: PointProperties
'Author: Matthew Pritchard
'Date: 07 December 2017
'
'Description: This function returns an ADODB recordset which contains all of the properties
' available for the point type passed to the function
'
'Revision: 1.1 15 January 2025
' Added optional "Filter" argument to function'
'
'Arguments: SQLConn - An ADODB connection to the ClearSCADA server to query
' PointType - The ClearSCADA point type of the point to get the properties for
' Filter - A string filter to be applied to the field definition table name field of the query
'
'Return Type: ADODB RECORDSET
'
'***************************************************************************************
Private Function PointProperties(SQLConn As ADODB.Connection, PointType As String, Optional Filter As String = "") As ADODB.Recordset
'Dim oConn As ADODB.connection
Dim oPropertyList As ADODB.Recordset
Dim sSQL As String
Set oPropertyList = CreateObject("ADODB.Recordset")
'Query for properties
sSQL = "SELECT NAME FROM DBFIELDDEF WHERE TABLE = '" & PointType & "'"
'Revision: 1.1 - If a filter has been pass to the function add it to the query.
'Both the returned name of the property and the filter has been set to
'lower case so that the case is not important for the final filtered
'value returned.
If Len(Filter) > 0 Then
sSQL = sSQL + " AND LOWER(NAME) LIKE '%" + LCase(Filter) + "%'"
End If
oPropertyList.Open sSQL, SQLConn
oPropertyList.Close
oPropertyList.CursorLocation = adUseClient
oPropertyList.Open
Set PointProperties = oPropertyList
End Function
显然要使用该功能,您将必须将ADODB连接传递到数据库到该功能,然后您还将传递对象类型(例如示例中的CNDP3Analogin)。然后,这将返回具有该对象类型的所有可用属性的记录集(包括适当的元数据中创建的任何自定义字段。
为此,其中包含过滤器的原因是,我在Excel中使用了从所选对象中产生一个字段的列表框,这些字段可以由用户可以过滤,如下图:
输入图像描述在这里此示例显示了选择为“系统表”的对象类型(已应用了显示的过滤器,但通常显示数据库中可用的所有表),然后在“添加”列表框的“字段中”动态填充。
为了本文的目的,可以简单地将来自该函数的返回的记录写入Excel中的单元格中的摘要中的上述函数,例如以下内容:
Set GetObjectFields = PointProperties(oConn, "CNDP3AnalogIn")
GetObjectFields.MoveFirst
For i = 1 To GetObjectFields.RecordCount
Worksheets(1).Cells(i, 1) = GetObjectFields.Fields("Name")
GetObjectFields.MoveNext
Next