Tanquen
Tanquen

Reputation: 43

VBA How to programmatically set objects name?

In FactoryTalk View SE I’m trying to set an objects name in VBA based on another value.

This works:

Dim PumpSP As Object

Set PumpSP = ThisDisplay.PumpSetpoint1

PumpSP.Vaule = 10

This doesn’t work:

Dim PumpSP As Object

Set PumpSP = "ThisDisplay.PumpSetpoint" & "1"

PumpSP.Vaule = 10

How do I get it to take a concatenated string?

Thanks.

Upvotes: 0

Views: 1668

Answers (2)

Tanquen
Tanquen

Reputation: 43

That is the hard part as we have paid support with Rockwell but they will not help with any VBA questions. Ok fine but there VBA commands and class are not like 90% of the Excel or MS Office VBA you find online. Anyway I was able to find the correct class member.

So this work for me: Set PumpSP = ThisDisplay.FindElement("PumpSetpoint" & “1”) Thanks for all the help.

Upvotes: 0

Mathieu Guindon
Mathieu Guindon

Reputation: 71177

How do I get it to take a concatenated string?

You don't, and you can't. PumpSP is an Object, not a String. The only thing you can ever Set it to, is an object reference.

This is very similar to, say, how you would access the controls on a UserForm:

Set box = Me.TextBox1

The reason you can get that object with a string, is because a form has a Controls collection that lets you provide a string, works out which control has that name, and returns the Control object for it:

Set box = Me.Controls("TextBox" & i)

So in your case to go from this:

Set PumpSP = ThisDisplay.PumpSetpoint1

I don't know what ThisDisplay is, but maybe it has a similar collection:

Set PumpSP = ThisDisplay.Controls("PimpSetpoint" & i)

If there's no collection that lets you retrieve items by name with a string literal, you're out of luck.

Upvotes: 2

Related Questions