Need to create object name inside a for loop

I am trying to create an Object Name out of a For Loop integer and a string.
Below is the code, the commented section at the bottom is functional, I am just trying to do the same thing in a loop so its commented out, you can see what I am trying to do.
I cant get the lines 12 and 13  to concatenate to the Object name so I can then set the property using any variation of syntax I can think of.
Line 12 throws an error message immediately upon entering step through.
Line 13 doesn't set off an error message until I step through that line and then it too doesn't work


Private Sub UpdateCheckbox() 'Set checkboxes to match the state of the chart Line
Dim I As Integer
Dim TotalPens As Integer
Dim Val As Object
Dim Show As Object
Dim strVal As String
Dim strShow As String

TotalPens = Cht1.Pens.Count
    For I = 1 To TotalPens
        strVal = I
        Set Show = Cht1.Pens.Item(I)
        Set Val = ("CheckBox" & strVal)
        Val.Value = Show.Showline
    Next

'CheckBox1.Value = Cht1.Pens.Item(1).Showline
'CheckBox2.Value = Cht1.Pens.Item(2).Showline
'CheckBox3.Value = Cht1.Pens.Item(3).Showline
'CheckBox4.Value = Cht1.Pens.Item(4).Showline
'CheckBox5.Value = Cht1.Pens.Item(5).Showline
'CheckBox6.Value = Cht1.Pens.Item(6).Showline
'CheckBox7.Value = Cht1.Pens.Item(7).Showline
'CheckBox8.Value = Cht1.Pens.Item(8).Showline
'CheckBox9.Value = Cht1.Pens.Item(9).Showline
'CheckBox10.Value = Cht1.Pens.Item(10).Showline
'CheckBox11.Value = Cht1.Pens.Item(11).Showline
'CheckBox12.Value = Cht1.Pens.Item(12).Showline
Set Val = Nothing
Set Show = Nothing
End Sub

Open in new window

LVL 1
Salad-DodgerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NorieAnalyst Assistant Commented:
You can create the name but that's only a string, to refer to the actual object you would need to use that name in conjunction with the collection the object belongs to.

How did you create the checkboxes and where are they located?
0
Salad-DodgerAuthor Commented:
When I try to SET the object to the string created by concatenating the parts, that's when it fails. Concatenation works fine, it becomes that proper string, but I can't SET it to be the object after that. I can type the exact same string into the SET line and that works fine, just cant assemble the string on the fly.
This is not an office application - but it uses VBA for its automation and syntax should be identical. How would you assemble an object name and then SET it as I am trying to do?
0
NorieAnalyst Assistant Commented:
How were the checkboxes created?

Do they belong to a collection, e.g. Shapes, ActiveX, OLEObjects?

What application is it you are using?
0
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Salad-DodgerAuthor Commented:
Checkboxes are "Microsoft Forms 2.0 Checkbox" object  
They are inside an activeX container in a SCADA application. they aren't part of a collection, as far as I can tell. If they were, what would the syntax be?
0
NorieAnalyst Assistant Commented:
If they are in a container then you might be able to access them via a collection of the container.

For example, in Excel (or Word) VBA you have userforms, which could be regarded as containers, and you can access the controls on a userform via it's Controls collection.

So how do you refer to the ActiveX container in your application?
0
Salad-DodgerAuthor Commented:
What you see is what I use, the commented portion at the bottom works just fine. I'm just trying to create a loop that does the same thing as teh 11 lines do,
0
NorieAnalyst Assistant Commented:
Unless there's is some sort of collection you can use to refer to the control in the container then I don't think it's going to be possible.

I don't know what environment you are coding in, or what debugging features (if any) it has, but if has any type of Watch window what properties do you see for chk when you add this to your code?
Set chk = Checkbox1

Open in new window

0
Salad-DodgerAuthor Commented:
Is this what you are looking for?
CheckboxObject.jpg
0
NorieAnalyst Assistant Commented:
That does show the properties of the checkbox, what I was hoping to see was some sort of 'parent' property.

Is there anything like that there?
0
Salad-DodgerAuthor Commented:
VBA-Object.jpg
0
Salad-DodgerAuthor Commented:
Or more likely, this one. Highest on the tree
parent.jpg
0
NorieAnalyst Assistant Commented:
Do any of the Parent objects have collections, e.g. OLEObjects, Shapes?

If they do are the checkboxes listed in any of those collections?
0
Salad-DodgerAuthor Commented:
Checkboxes are shown under cFixPicture
0
NorieAnalyst Assistant Commented:
Do you mean there's a collection name Checkboxes under cFixPicture and when you expand it you can see the checkboxes you want to refer to in your code?

Where is the code you posted located?

Can you get a reference to the cFixPicture object where, I presume, the checkboxes are located?
0
Salad-DodgerAuthor Commented:
The code I posted is in the first message
Please forgive me but I'm not following where this depth into the object is going.
The problem I see is simpler in that I can't assemble the name properly for the SET command to accept it as an object name, even though the watch window shows the correct string has been created.
Example:
If I use the following, the script runs fine
Set = CheckBox1
or 
Set = Cht1.Pens.Item(1)

Open in new window



When I attempt to assemble the objects name ( Cht1.Pens.Item(1) or CheckBox1) on the fly using the I variable of the loop index in either technique below that this SET command doesn't work and an error is thrown.
   
 For I = 1 To 11
        Set Show = Cht1.Pens.Item(I)
        Set Val = "CheckBox" & l

Open in new window

0
NorieAnalyst Assistant Commented:
All ("CheckBox" & strVal) returns is a string, it can only be used to refer to an object via the collection that contains the object.

For example if I had a collection named Checkboxes I could use something like this.
For I = 1 to 5
    strCheckName = "CheckBox" & I
    Set objChk = Checkboxes(strChkName)
    ' do stuff with the object reference objChk
Next I

Open in new window


PS Set is for objects, not simple data types like String.
0
Salad-DodgerAuthor Commented:
Thats what I am doing. Using the String to set the object.

DIM Show as Object
DIM Val as Object
For I = 1 To 11
        Set Show = Cht1.Pens.Item(I)
        Set Val = "CheckBox" & l

Open in new window

0
NorieAnalyst Assistant Commented:
You cannot use a string to 'create' an object, it's just a string.

Where exactly are the checkboxes located?

Where is the code located?
0
Salad-DodgerAuthor Commented:
I'm not creating one, I'm using a string as the name of the existing object for which I want to to access the properties (in this case Checkbox1 through Checkbox11) .  
This technique works in this application. When I write
DIM VarName as Object 
Set VarName = CheckBox1

Open in new window

, the watch window shows that the object CheckBox1 is "opened up".

Perhaps there is something different about the implementation here but SET will make the object show up in the watch window. When that occurs I can adjust the properties of the object. What I cant do is make my concatenated text replace the hardcoded text.

I'm not sure how to answer where the checkboxes and code are located. They are all in the same screen, an activeX Container, no forms so I don't know how else to answer this.
0
NorieAnalyst Assistant Commented:
In this code you are directly referring to the object CheckBox1, CheckBox1 is not hard-coded text.
Dim VarName as Object 
Set VarName = CheckBox1

Open in new window


How are you adding the code?

Do you insert a module and put the code there?

Are you putting it in an existing module that is attached to something?
0
Salad-DodgerAuthor Commented:
This is handwritten - I typed it in the word "CheckBox1"
Dim VarName as Object 
Set VarName = CheckBox1

Open in new window


This is where the word "CheckBox1" gets "created" by code instead of typing it in.
DIM Show as Object
DIM Val as Object
For I = 1 To 11
        Set Show = Cht1.Pens.Item(I)
        Set Val = "CheckBox" & l

Open in new window


I type the code into the VBA IDE of the application.  I don't insert a module and I don't attach anything
0
NorieAnalyst Assistant Commented:
Does the IDE have a Project Explorer, or something similar,  like the Excel/Word VBA IDE has?

PS You cannot use Set or Object for a simple data type like a string.
0
Fabrice LambertFabrice LambertCommented:
Unless there is a collection somewhere holding all the controls, there arn't any easy way to refer to a control via its name.

Maybe the eval function if it is available, but that's ugly:
eval("CheckBox" & I & ".Value = Cht1.Pens.Item(" & I &  ").Showline")

Open in new window

0
Salad-DodgerAuthor Commented:
The Ide shows the containing screen as "pop_trnd_copper"

the object appears in local watch as "Me.CheckBox1"

Nothing I can find will let me build the name CheckBox1 through CheckBox11 by concatenation because the parser wont "figure out" that the string that is used IS the object name. It can tell that its not typed in (hardcoded)

How else would you loop through a sequence of similar object names?

EVAL statement is not available here
0
Salad-DodgerAuthor Commented:
Norie
it seems you faced the exact same question in 2007

https://www.mrexcel.com/forum/excel-questions/256510-convert-string-object-function.html

I think the problem we are facing that you arent understanding is that this system I am using (and the other poster) is not anything like the Office IDE so its handled differently than excel or word does. That must be the disconnect we are experiencing.

The question I am asking makes perfect sense to me, Its a simple substitution. But the SET command will not allow that substitution.
0
Salad-DodgerAuthor Commented:
This does the job a different way, touches a lot more than just the objects I needed.
Does this show you what you are looking for as far as collections etc are concerned?

Private Sub SetCheckBoxesTrue() 'using a loop
Dim objPic As Object
Dim objShape As Object
Dim I As Integer

Set objPic = Application.ActiveDocument.Page.ContainedObjects 'define picture object

    For I = 1 To objPic.Count 'find objects in picture

      Set objShape = objPic.Item(I)'find animation objects in objects

        If objShape.Name Like "CheckBox*" Then
            objShape.Value = True 'Set Value true
        End If

    Next I

Set objShape = Nothing
Set objPic = Nothing
 
End Sub

Open in new window

0
NorieAnalyst Assistant Commented:
Salad-Dodger

We don't know what 'system' you are using because you haven't told us what 'system' you are using.:)

Without that information it's kind of hard to give specific advice.

PS I've searched for SCADA, CFixPicture etc. but wasn't able to find anything useful.
0
Fabrice LambertFabrice LambertCommented:
Worst case, you can build a collection with controls, then you'll be able to use their name dynamically:
dim controls as collection

private sub form_initialise
    controls.add CheckBox1, "CheckBox1"
    controls.add CheckBox2, "CheckBox2"
    '// ect ....
end sub

Private Sub UpdateCheckbox()
    for i = 1 to 12
        controls("CheckBox" & i).value = Cht1.Pens.Item(i).Showline
    next
end sub

private sub form_terminate()
    set controls = nothing
end sub

Open in new window

0
Salad-DodgerAuthor Commented:
the System is GE Proficy iFix - they are a closed package that require expensive maintenance agreements to access their KB and docs. I find telling people that ahead of time doesn't really help unless its a SCADA group since they usually cant get any info from the GE site anyway. I hope to keep it generic VBA... if there is such a thing. The platform has VBA code and "Automation" code . Automation code is embedded  routines (written in VBA) that I can call to accomplish tasks. More like a collection of globally accessible Functions and Procedures. The code for these is available and is generally too far over my head for easy adaptation using even more deeply embedded routines such as FDS (Fix Datasystem Object) and some C Dlls'


Fabrice,
So a "collection" here is simply a collection of objects of my own choosing? I gotta look into that.  That'll be great if that works here.
0
NorieAnalyst Assistant Commented:
A collection is usually something that's part of the object model.

For example an Excel workbook has a collection of worksheets.

In the pseudo coded Fabrics posted controls would, I assume anyway, be a collection belonging to a form.
0
Salad-DodgerAuthor Commented:
Hmm, I feel a cloud coming overhead...
0
NorieAnalyst Assistant Commented:
0
Salad-DodgerAuthor Commented:
Yes it is.
0
Fabrice LambertFabrice LambertCommented:
in the "Rotating a group" sample, I found that you can use the form's FindObject member function to retrieve an object based on its name.
So your for loop can look something like this:
For I = 1 To TotalPens
    Dim chkBox As Object
    Set chkBox = Me.FindObject("CheckBox" & I)

    Set Show = Cht1.Pens.Item(I)
    chkBox.Value = Show.Showline
    Set chkBox = Nothing
Next

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Salad-DodgerAuthor Commented:
I'll dig into that today...
In my previous attempts this line would be where it fails because of the use of the string concatenation.
Set chkBox = Me.FindObject("CheckBox" & I)

Open in new window

Are you saying that the FindObject function in the Set line somehow gets around that limitation? Otherwise isn't this what I have been attempting all along? If so I'll have to locate that function's source and see what it does differently.
0
Fabrice LambertFabrice LambertCommented:
With MS Office products, forms has a Controls object collection, wich hold all controls (buttons, checkboxs, radio buttons ect ...) held by a form.
And an item in a collection can be accessed with it's index or with it's name.

The FindObject member function seems to be iFIX's implementation of the Controls collection.
0
Salad-DodgerAuthor Commented:
If you are right, thats outstanding!
0
Salad-DodgerAuthor Commented:
Final outcome was the use of 2 methods. I was told "collection" is also available to me so I'll have to read more on that as well. Thank you so much for the help.

[code]Private Sub SetCheckBoxesTrue()
Dim objPic As Object
Dim objShape As Object
Dim I As Integer
    Set objPic = Application.ActiveDocument.Page.ContainedObjects
   
    For I = 1 To objPic.Count
        Set objShape = objPic.Item(I)
            If objShape.Name Like "CheckBox*" Then
                    objShape.Value = True
            End If
    Next I
Set objShape = Nothing
Set objPic = Nothing
End Sub


Private Sub SetCheckBoxesToMatch()
Dim I As Integer
Dim iTotalPens As Integer
iTotalPens = Cht1.Pens.Count
    For I = 1 To iTotalPens
        Dim oChkBox As Object
        Dim oShow As Object
        Set oChkBox = Me.FindObject("CheckBox" & I)
        Set oShow = Cht1.Pens.Item(I)
        oChkBox.Value = oShow.Showline
        Set oChkBox = Nothing
        Set oShow = Nothing
    Next
 End Sub[/code]

Apparently text formatting doesn't work in this last response dialog...
0
NorieAnalyst Assistant Commented:
Looks like ContainedObjects, here, is the 'collection'.
Set objPic = Application.ActiveDocument.Page.ContainedObjects 

Open in new window


In your code you use objPic(I) which is referencing the objects in the collection using their numeric index, have you tried using the object name?

For example, does something like this work?
Set objShape = objPic.Item("CheckBox1") 

Open in new window

1
Salad-DodgerAuthor Commented:
That does indeed work. Which was the confusing part that the findobjects works around.

I can assemble a string variable using "Checkbox" & I that concatenates to "CheckBox1" - but that wont work. I have to expressly hardcode the object name in the SET line. Somehow FindObjects works that out.
0
NorieAnalyst Assistant Commented:
So something like this doesn't work?
Dim objPic As Object
Dim objShape As Object
Dim I As Integer

    Set objPic = Application.ActiveDocument.Page.ContainedObjects 

    For I = 1 To 10
        Set objShape = objPic.Item("CheckBox" & I)

        ' do stuff with objShape

    Next I

Open in new window

[/code]
0
Salad-DodgerAuthor Commented:
nope
hangs at this line every time, Doesn't like the "& I"
Set objShape = objPic.Item("CheckBox" & I)
0
NorieAnalyst Assistant Commented:
Strange, how about this?
Dim objPic As Object
Dim objShape As Object
Dim strObjName As String
Dim I As Integer

    Set objPic = Application.ActiveDocument.Page.ContainedObjects 

    For I = 1 To 10

        strObjName = "CheckBox" & I)

        Set objShape = objPic.Item(strObjName)

        ' do stuff with objShape

    Next I

Open in new window

0
Salad-DodgerAuthor Commented:
Edit:
I missed that you had it as a contained object - that might work, I didn't try it exactly that way but given what I now know, I expect that might work. Its just when its in a SET statement, not within a containedobject collection,  that you cant make the substitution
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VBA

From novice to tech pro — start learning today.