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
LVL 1
calebSAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Guy Hengel [angelIII / a3]Connect With a Mentor Billing EngineerCommented:
BTW, another coding recommendation for performance and debugging enhancements:
replace
Dim textboxCollection As New Collection
by
Dim textboxCollection As Collection
set textboxCollection = new Collection

to shorten the code, you could also do this:

replace
       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)

by

dim lngLoop as long
for lngLoop = 1 to 16
   textboxCollection.Add div7aForm.controls("TextBox" & lngLoop)
next

Cheers
0
 
RichWCommented:
Well, you haven't declared tbox anywhere.

How are you defining tbox?

0
 
RichWCommented:
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
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
calebSAuthor Commented:
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.
0
 
RichWCommented:
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
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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
0
 
RichWCommented:
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
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
> 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

0
 
RichWCommented:
>>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
0
 
calebSAuthor Commented:
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

??
0
 
calebSAuthor Commented:
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

??
0
 
RichWCommented:
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

0
 
calebSAuthor Commented:
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.

http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20339029.html
0
All Courses

From novice to tech pro — start learning today.