FTView VBA Issue. Object-variable not set

Revnus

Member
Join Date
Aug 2019
Location
Brazil
Posts
73
Hi all.

I'm running a VBA code on my FTView which is actually working, but everytime I open the display that calls the VBA code, it throws the error:

"Object-variable or With block variable not set"

Since debugging the code was not highlighting the problem line, I had to isolate the code by parts until I find the problem line.

Below is the code and I highlighted the problem line:


Dim WithEvents INDICE_LISTA_AB As Tag
Dim WithEvents Ogroup As TagGroup

Private Sub Display_AnimationStart()
On Error GoTo Err
If Ogroup Is Nothing Then
Set Ogroup = Application.CreateTagGroup(Me.AreaName, 1000)
Ogroup.Add "{/CB/CC/DATA::[PLC]INDICE_LISTA_AB}"
End If
Ogroup.Active = True
Set INDICE_LISTA_AB = Ogroup.Item("{/CB/CC/DATA_CCO::[PLC]INDICE_LISTA_AB}")
INDICE_LISTA_AB.Value = 0
Exit Sub
Err:
LogDiagnosticsMessage Err.Description, ftDiagSeverityError
End Sub

Private Sub Input_Change()
On Error GoTo Err

If (Not INDICE_LISTA_AB.Value = 0) Then
Button.Enabled = True
Button.ForeColor = RGB(0, 0, 0)
Else
Button.Enabled = False
Button.ForeColor = RGB(192, 192, 192)
End If
Exit Sub
Err:
LogDiagnosticsMessage Err.Description, ftDiagSeverityError
End Sub


PLC tag is a DINT
How do I fix this error?
 
You are either attempting to reference an object that has been set to Nothing.

or attempting to access an element of an array variable that wasn't properly declared.

the array has no elements and is treated as an object.

or you are attempting to access code within a With...End With block before the block has been initialized. A With...End With block must be initialized by executing the With statement entry point.
put in option strict on then the compiler will show you where the error is.
 
You are either attempting to reference an object that has been set to Nothing.

or attempting to access an element of an array variable that wasn't properly declared.

the array has no elements and is treated as an object.

or you are attempting to access code within a With...End With block before the block has been initialized. A With...End With block must be initialized by executing the With statement entry point.
put in option strict on then the compiler will show you where the error is.

Unfortanetly I'm not well trained in VBA. I tried but I can't find where I did not reference or declared properly.

I'm not using "With" instruction.

Option Strict is not avaiable:
"Compile Error:
Expected: Base or Compare or Explicit or Private"
 
From where is Input_Change() called? If INDICE_LISTA_AB hasn't been set to a value, that error is not unreasonable (see parky's first item). I haven't used VBA in a PV before, but can you assign an initial value with the DIM statement? something like


Code:
[COLOR=Blue]Dim WithEvents INDICE_LISTA_AB [B][U]= 0 [/U][/B]As Tag[/COLOR]


I sincerely doubt that will work as-is, but something like it may. Otherwise, you need to make absolutely sure it's initialized before using it.
 
From where is Input_Change() called? If INDICE_LISTA_AB hasn't been set to a value, that error is not unreasonable (see parky's first item). I haven't used VBA in a PV before, but can you assign an initial value with the DIM statement? something like


Code:
[COLOR=Blue]Dim WithEvents INDICE_LISTA_AB [B][U]= 0 [/U][/B]As Tag[/COLOR]


I sincerely doubt that will work as-is, but something like it may. Otherwise, you need to make absolutely sure it's initialized before using it.

"Input_Change()" is a event (change) to the value of a numeric input object in FactoryTalk View.
If there is a change on this value, then the code must check if the value of INDICE_LISTA_AB is not 0 before enabling the FTView object named "Button".

I tried to set the initial value on this line:
INDICE_LISTA_AB.Value = 0

Your code can't work because Tag is not a variable type. It is a object that has properties and one of them is ".Value" which can be of Integer type, String type..
But as I said above, I set the value of 0 when the display starts, which is before the event "Input_Change()"
 
It looks like you're using the status of a PLC tag to control the appearance of a button. I've always just used the "states" tab of the button properties. On the "Connection" tab, you can have the button's value (what it writes) and indicator (what it uses for display appearance) be different tags.

I've only tried to use VBA a couple of times on these and I could never get anything to work reliably on panel itself, even if it simulated properly.
 
It looks like you're using the status of a PLC tag to control the appearance of a button. I've always just used the "states" tab of the button properties. On the "Connection" tab, you can have the button's value (what it writes) and indicator (what it uses for display appearance) be different tags.

I've only tried to use VBA a couple of times on these and I could never get anything to work reliably on panel itself, even if it simulated properly.

In fact, my code is longer than what I posted here. But I was careful to extract and post only what was related to the error that is occurring.

The goal is not to control the appearance of the button although this is actually being done by the code.

My display is actually a registration screen that communicates with SQL Express, where the operator inserts the inputs he wants and when all the fields are filled in, a button to save the data is enabled. This check to see if the button can be enabled is done each time a field on the screen is changed.

I know that some logic could be done in the PLC, but as it is CCW and there is not much memory, I preferred to do it in VBA.

Returning to the problem, my code is working, the only thing left is to resolve this problem that appears in the DiagnosticViewer, "Object-variable not set", related to the line I marked yellow.

Even though it's working, I want to make sure it's 100% when I deliver it to the customer.

It would also be good to learn a little, understanding what precisely is generating this error.
 
Gotcha. I figured that there was more going on but wanted to be sure. You're beyond my experience right now, but it's definitely an interesting topic to learn.
 
Gotcha. I figured that there was more going on but wanted to be sure. You're beyond my experience right now, but it's definitely an interesting topic to learn.


Ok. Anyway, thanks for your willingness to help.

Let's wait and see if Parky or another plctalk master can help.:)
 
Are you sure it's the tag variable that's causing your error? That 2nd function refers to a "Button" variable that doesn't seem to be defined anywhere.
 
The code in red might be a workaround, assuming Input_Change is called before Display_AnimationStart:


Dim WithEvents INDICE_LISTA_AB As Tag
Dim WithEvents Ogroup As TagGroup

Private Sub Display_AnimationStart()
On Error GoTo Err
If Ogroup Is Nothing Then
Set Ogroup = Application.CreateTagGroup(Me.AreaName, 1000)
Ogroup.Add "{/CB/CC/DATA::[PLC]INDICE_LISTA_AB}"
End If
Ogroup.Active = True
Set INDICE_LISTA_AB = Ogroup.Item("{/CB/CC/DATA_CCO::[PLC]INDICE_LISTA_AB}")
INDICE_LISTA_AB.Value = 0
Exit Sub
Err:
LogDiagnosticsMessage Err.Description, ftDiagSeverityError
End Sub

Private Sub Input_Change()
On Error GoTo Err
Dim iValue = 0
If INDICE_LISTA_AB Is Nothing Then
iValue = 0 ''' if object does not yet exist, assume .Value is 0
Else
iValue = INDICE_LISTA_AB.Value
End If
If (Not iValue = 0) Then

Button.Enabled = True
Button.ForeColor = RGB(0, 0, 0)
Else
Button.Enabled = False
Button.ForeColor = RGB(192, 192, 192)
End If
Exit Sub
Err:
LogDiagnosticsMessage Err.Description, ftDiagSeverityError
End Sub


 
Are you sure it's the tag variable that's causing your error? That 2nd function refers to a "Button" variable that doesn't seem to be defined anywhere.

The button is an object from FactoryTalk View.
When I go to the FTViewStudio and select the properties from the button, I can choose "expose to VBA".
Automatically the object is available when I go the code. It is not necessary to declare it in the code.
 
In FTView VB, does the environment save the globals between callbacks?

I'm not sure what you mean by "globals between callbacks", but I did more testing and found that if I delete a call to a public function that exists in other code in a module within the same project, the error doesn't happen .
At first, I didn't post the code of this function here because it is not related to the variable, but somehow I think that the variable I highlighted is losing reference after calling this public function from another module.

Here goes the code with the call:


Dim WithEvents INDICE_LISTA_AB As Tag
Dim Read As String
Dim WithEvents Ogroup As TagGroup

Private Sub Display_AnimationStart()
On Error GoTo Err
Read = "SELECT TOP (1) [Num] FROM [DB].[dbo].[Table] order by [Num] desc"
ExSQL (Read)

If Ogroup Is Nothing Then
Set Ogroup = Application.CreateTagGroup(Me.AreaName, 1000)
Ogroup.Add "{/CB/CC/DATA::[PLC]INDICE_LISTA_AB}"
End If
Ogroup.Active = True
Set INDICE_LISTA_AB = Ogroup.Item("{/CB/CC/DATA_CCO::[PLC]INDICE_LISTA_AB}")
INDICE_LISTA_AB.Value = 0
Exit Sub
Err:
LogDiagnosticsMessage Err.Description, ftDiagSeverityError
End Sub

Private Sub Input_Change()
On Error GoTo Err
If (Not INDICE_LISTA_AB.Value = 0) Then
Button.Enabled = True
Button.ForeColor = RGB(0, 0, 0)
Else
Button.Enabled = False
Button.ForeColor = RGB(192, 192, 192)
End If
Exit Sub
Err:
LogDiagnosticsMessage Err.Description, ftDiagSeverityError
End Sub


If I eliminate either the red highlighted part or the yellow one, the error does not happen.
 
The code in red might be a workaround, assuming Input_Change is called before Display_AnimationStart:


Dim WithEvents INDICE_LISTA_AB As Tag
Dim WithEvents Ogroup As TagGroup

Private Sub Display_AnimationStart()
On Error GoTo Err
If Ogroup Is Nothing Then
Set Ogroup = Application.CreateTagGroup(Me.AreaName, 1000)
Ogroup.Add "{/CB/CC/DATA::[PLC]INDICE_LISTA_AB}"
End If
Ogroup.Active = True
Set INDICE_LISTA_AB = Ogroup.Item("{/CB/CC/DATA_CCO::[PLC]INDICE_LISTA_AB}")
INDICE_LISTA_AB.Value = 0
Exit Sub
Err:
LogDiagnosticsMessage Err.Description, ftDiagSeverityError
End Sub

Private Sub Input_Change()
On Error GoTo Err
Dim iValue = 0
If INDICE_LISTA_AB Is Nothing Then
iValue = 0 ''' if object does not yet exist, assume .Value is 0
Else
iValue = INDICE_LISTA_AB.Value
End If
If (Not iValue = 0) Then

Button.Enabled = True
Button.ForeColor = RGB(0, 0, 0)
Else
Button.Enabled = False
Button.ForeColor = RGB(192, 192, 192)
End If
Exit Sub
Err:
LogDiagnosticsMessage Err.Description, ftDiagSeverityError
End Sub



Input_Change is called after Display_AnimationStart:
Input is an object on the display that is called.

When the display is opened I write "" to inputs by referencing directly the objects which are exposed to VBA.

When the operator changes the Input value on the display at runtime, the code checks if the input is not 0, so it can enable a button which is an object on the same display.
 

Similar Topics

Hi all. Currently I'm working on a VBa script for FTView. I would like to understand a bit better about some commands. 1) What are the...
Replies
3
Views
598
Greetings, I have this question: How can I add a tag, in my case LS:0 {#5} (literal string of tag #5), to a variable using VBA? I couldn't find...
Replies
4
Views
897
Hello Every one, I need a help on vba code for getting data from MSSQL server 2014 to display in FTview SE client version 12 . Thanks. Please...
Replies
4
Views
1,815
I've been messing around with trying to find a way to clear all PlantPAx AOI latched alarms via the Alarm Banner or Summary objects. So far, I can...
Replies
2
Views
1,737
I have a VBA on an alarm banner that calls a large number of pop-ups alarms based on changes of digital PLC tag value. Problem is that when the...
Replies
2
Views
2,324
Back
Top Bottom