Solved

Collection of Textbox, created from textboxes in various frames.

Posted on 2002-07-30
13
224 Views
Last Modified: 2010-05-01
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
0
Comment
Question by:calebS
  • 6
  • 4
  • 3
13 Comments
 
LVL 4

Expert Comment

by:RichW
ID: 7187683
Well, you haven't declared tbox anywhere.

How are you defining tbox?

0
 
LVL 4

Expert Comment

by:RichW
ID: 7187688
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
 
LVL 1

Author Comment

by:calebS
ID: 7187758
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
 
LVL 4

Expert Comment

by:RichW
ID: 7187780
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
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 7187800
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
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 80 total points
ID: 7187804
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 4

Expert Comment

by:RichW
ID: 7188195
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
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 7188237
> 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
 
LVL 4

Expert Comment

by:RichW
ID: 7188350
>>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
 
LVL 1

Author Comment

by:calebS
ID: 7196606
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
 
LVL 1

Author Comment

by:calebS
ID: 7196712
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
 
LVL 4

Expert Comment

by:RichW
ID: 7197896
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
 
LVL 1

Author Comment

by:calebS
ID: 7232333
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

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now