Link to home
Start Free TrialLog in
Avatar of calebS
calebS

asked on

Collection of Textbox, created from textboxes in various frames.

Please find my code below.

The Textboxes are all on the form.
They are seperated inside 3 frames, which is why I chose to put them into a new collection. That way I could manipulate the collection.

I am getting an error message when I try to run this, basically it says 'tbox' is not an object. But I say it is.

Any thoughts?
****************************************************
Private Sub updateForm()
    'set the textbox collection
    'This did not required as much effort until I wanted  to format the form
    'in a more asthetically pleasing manner.
    Dim textboxCollection As New Collection
        textboxCollection.Add (div7aForm.TextBox1)
        textboxCollection.Add (div7aForm.TextBox2)
        textboxCollection.Add (div7aForm.TextBox3)
        textboxCollection.Add (div7aForm.TextBox4)
        textboxCollection.Add (div7aForm.TextBox5)
        textboxCollection.Add (div7aForm.TextBox6)
        textboxCollection.Add (div7aForm.TextBox7)
        textboxCollection.Add (div7aForm.TextBox8)
        textboxCollection.Add (div7aForm.TextBox9)
        textboxCollection.Add (div7aForm.TextBox10)
        textboxCollection.Add (div7aForm.TextBox11)
        textboxCollection.Add (div7aForm.TextBox12)
        textboxCollection.Add (div7aForm.TextBox13)
        textboxCollection.Add (div7aForm.TextBox14)
        textboxCollection.Add (div7aForm.TextBox15)
        textboxCollection.Add (div7aForm.TextBox16)

        'Used this to check collection had 16 ojects.    
        MsgBox textboxCollection.Count
       
    For Each tbox In textboxCollection
      If ((div7aForm.CheckBox1) Or (tbox.Text <> "")) Then
            With ActiveDocument.Content.Find
                .Text = tbox.Tag  
                .Replacement.Text = tbox.Text
                .Execute replace:=wdReplaceAll, Forward:=True, Wrap:=wdFindContinue
            End With
        End If
    Next tbox
End Sub
Avatar of RichW
RichW
Flag of United States of America image

Well, you haven't declared tbox anywhere.

How are you defining tbox?

Why not do this:

Dim tbox as TextBox

For Each tbox In div7aForm
  If ((div7aForm.CheckBox1) Or (tbox.Text <> "")) Then
           With ActiveDocument.Content.Find
               .Text = tbox.Tag  
               .Replacement.Text = tbox.Text
               .Execute replace:=wdReplaceAll, Forward:=True, Wrap:=wdFindContinue
           End With
       End If


Next

For what you're trying to do I don't believe there is a need for a collection.

RichW
Avatar of calebS
calebS

ASKER

richW,

Thankyou, I had been trying to do just what you said, it hadn't occured to me to define tbox as a texbox, and in that way it would only select textboxes from the collection.

I will try it and get back to you.

Thanks.
Cool.  You've already got the collection and you step through it in the For Each statement.

Let me know how you make out.

RichW
Avatar of Guy Hengel [angelIII / a3]
if the comment of RichW is correct, you don't use the following line on top of each module:
OPTION EXPLICIT
This makes you force to declare each variable before you use it. In your precise case, the variable not being defined, it is implicitely of type VARIANT, which you cannot use in FOR EACH  construct.
CHeers
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
But angelIII, there's no need for a collection object, since the form's controls collection allows you to manipulate through it and work with each object.

Also, I would never advise to remove the Option Explicit.  This would point out that a variable hasn't been declared, and thus force him to declare the type.

The only time I would want to add objects to a collection object is when I'm dynamically adding objects to a form from a class.  I would then have a plurally named collection class that stores the information about each object I'm creating, but that's not needed here.

He's trying to test each textbox and replace text on the true condition.  There's no need to add these textbox objects to a collection object when they're already in a collection that can be used.

RichW
> no need for a collection object
eventually. If the collection was only used for THAT part of the code, I would agree with you. But if this "collection" will be used for other loops, you have to duplicate the same logic to loop only those text boxes over and over again...

> I would never advise to remove the Option Explicit.  
Maybe you reread my sentence, where I say that calebS has probably this line missing. Thus we agree :-)

CHeers

>>if the comment of RichW is correct, you don't use the following line on top of each module:
OPTION EXPLICIT

Hmmm, I thought you were saying that I didn't mention Option Explicit, which in my mind had nothing to do with the code snippet. :) So, yes, we agree.  I misread you.

From what calebS stated I believe he chose a collection object just for the function he's doing, because he thought he needed to do that since the textboxes were in three different frames.

All I'm trying to point out is that tbox wasn't declared anywhere, and there already is a collection for the form's controls.

Cheers

RichW
Avatar of calebS

ASKER

A couple of questions, my first for RichW.
I just tried your suggestion and I am getting the same error message.
I do not understand why, I am also a little lost by the 'OPTION EXPLICIT' part of your discussion, so I skipped over it. Is it something I should consider??

Angel,
why do you say the following:

Dim textboxCollection As Collection
set textboxCollection = new Collection

is better then:

Dim textboxCollection As New Collection

??
Avatar of calebS

ASKER

A couple of questions, my first for RichW.
I just tried your suggestion and I am getting the same error message.
I do not understand why, I am also a little lost by the 'OPTION EXPLICIT' part of your discussion, so I skipped over it. Is it something I should consider??

Angel,
why do you say the following:

Dim textboxCollection As Collection
set textboxCollection = new Collection

is better then:

Dim textboxCollection As New Collection

??
Okay, the Option Explicit part was a misunderstanding between angelIII and myself.  angelIII  thought that maybe you didn't have this line at the top of your module, and I thought he was recommending removing it.  

In case you don't understand, Option Explicit is a statement you use at the top of every module that will force you to declare every variable you try to use.  This way, when you go to compile or run the project, VB will find any variable that have not been declared, and thus, you may find out that you are using a variable for one datatype but it wasn't declared as such.

So far as using the New keyword in the Dim statement, or using it in the second Set statement, the difference has to do with binding.  You have early binding and late binding.  Early binding instantiates the object as soon as it's declared, whereas late binding instantiates the object the first time you try to use it.  The difference has to do with the amount of overhead you are using, and other things.  Early binding will take up overhead as soon as it's declared, whereas late binding won't use up overhead until you actually use the variable.

What angelIII and I were discussing was whether or not you are using this routine only once or if you want to reuse it multiple times.

If you are only using it once, then stepping through the form's controls collection (my example) would be fine, but if you're going to use the routine more than once, then putting the textboxes into a collection would be better, since you would always have the collection to be resused again and again.  Otherwise, my example says, if you're only doing this once, why open up a collection object when you already have a collection of your form's controls?

So, to wrap up, always have Option Explicit as the first line of any module.  You have to decide if you're going to reuse this routine.

Try this approach:

Dim tbox As Object
For Each tbox In div7aForm
    If TypeOf tbox Is TextBox Then
        If ((div7aForm.Check1) Or (tbox.Text <> "")) Then
            With ActiveDocument.Content.Find
              .Text = tbox.Tag
              .Replacement.Text = tbox.Text
              .Execute Replace:=wdReplaceAll, Forward:=True, Wrap:=wdFindContinue
            End With
      End If
End If
Next

Avatar of calebS

ASKER

Angell,
I implemented your suggestion and it worked, thankyou.

RichW, Thanks for you help Sorry I couldn't split the points or I would.

I have another problem if either of you would like to have a go.

https://www.experts-exchange.com/questions/20339029/ActiveDocument-content-find-text.html