我手头发布这个问题无济于事。我在SQL上构建了一个表来获取我想要引入Visual Basic的标记名。从Visual Basic我写了一个公共函数来将我的标记名拉为字符串。然后,我编写了一个公共子,用于从OPC中提取FactoryTalk的标签值。我现在在"这个显示器"我在哪里转储组合代码一起工作。我不能让它在它开始的地方正常工作" 0到UBound。它从SQL数据库中提取标记名。但是在下一个命令中,我得到"运行时错误' 91':对象变量或者没有设置块变量"。我不确定我还想用我的代码定义或设置什么。任何帮助它开始工作的人都将不胜感激。
Private Sub GetCommsOKTValues()
Dim HMITagGroup As TagGroup
Dim HMITag As Tag
Dim i As Integer
Dim intIndex As Integer
Dim astrtagNames() As String
Dim GetCommsOKTag As String
Dim avarValues() As Variant
astrtagNames = GetCommsOKTags
With HMITagGroup
For intIndex = 0 To UBound(astrtagNames)
Set HMITag = .Item(astrtagNames(intIndex))
avarValues(intIndex) = HMITag.Value
Next
End With
Set HMITag = Nothing
Exit Sub
ErrHandler:
Call UltimaLogMessage("VBA Error in modFTVTagFunctions.GetTagGroupValues - Err#" & Err.Number & " - " & Err.Description & ", Tagname = " & astrtagNames(intIndex))
Resume Next
End Sub
Public Function GetCommsOKTags() As String()
' This function returns an array of tagnames used to define the CommsOK.
Dim result As Boolean
Dim strSQL As String
Dim rs As New Recordset
Dim dbConn As New Connection
Dim tagNames() As String
Dim i As Integer
On Error GoTo ErrHandler
' Make a connection to the database.
With dbConn
.CursorLocation = adUseClient
Call .Open(DBConnString)
End With
' Setup the query for the recipe Values.
strSQL = "SELECT TagName FROM CommsOKTags"
Call rs.Open(strSQL, dbConn, adOpenStatic, adLockReadOnly)
' Make sure there were some records.
If rs.RecordCount > 0 Then
ReDim tagNames(rs.RecordCount - 1) As String
i = 0
Do
tagNames(i) = rs("TagName")
rs.MoveNext
i = i + 1
Loop Until rs.EOF
End If
rs.Close
Set rs = Nothing
dbConn.Close
Set dbConn = Nothing
GetCommsOKTags = tagNames
Exit Function
ErrHandler:
Call MsgBox("An error occured while getting recipe tags -" & vbCrLf & Err.Number & " - " &
Err.Description, vbExclamation + vbOKOnly)
Call UltimaLogMessage("modDBFunctions.GetCommsOKTags failed! " & Err.Number & " - " &
Err.Description)
GetCommsOKTags = tagNames
End Function
答案 0 :(得分:1)
在HMITag查找循环中,如果当前标记没有对象,则会出现错误。您需要检测此情况并进行处理(您必须决定如何处理丢失或无效的标记)。
此代码将避免您获得的错误
Dim HMITagGroup As TagGroup 'here it is declared, but not assigned anything'
Dim HMITag As Tag
Dim i As Integer
Dim intIndex As Integer
Dim astrtagNames() As String
Dim GetCommsOKTag As String
Dim avarValues() As Variant
astrtagNames = GetCommsOKTags
'HMITagGroup must be assigned to something or you are guaranteed to get a missing object error'
Set HMITagGroup = ???
With HMITagGroup
For intIndex = 0 To UBound(astrtagNames)
If Not .Item(astrtagNames(intIndex) Is Nothing Then
Set HMITag = .Item(astrtagNames(intIndex))
avarValues(intIndex) = HMITag.Value
Else
'astrtagNames(intIndex) does not have a matching HMITagGroup.Item'
'The original code was not handling this condition.'
'You might want to log the name of this tag, and see why there is no HMITagGroup.Item.'
End If
Next
End With