Referring to controls - What am I doing wrong??? 
Author Message
 Referring to controls - What am I doing wrong???

I want to sequentially refer to some textbox controls as a variable and
then change the .text value of each one to something different.  For
instance:

dt = Date
n = 1
Do Until n = 8
    x = "Text" & n & ".Text"
    x = dt
    dt = dt + 1
    n = n + 1
Loop

Why doesn't each textbox (named sequentially text1, text2, text2, etc)
acquire a different date?

Thanks  I. A.,
Carroll Lastinger



Wed, 10 Jan 2001 03:00:00 GMT  
 Referring to controls - What am I doing wrong???

Quote:

>I want to sequentially refer to some textbox controls as a variable and
>then change the .text value of each one to something different.  For
>instance:

>dt = Date
>n = 1
>Do Until n = 8
>    x = "Text" & n & ".Text"
>    x = dt
>    dt = dt + 1
>    n = n + 1
>Loop

>Why doesn't each textbox (named sequentially text1, text2, text2, etc)
>acquire a different date?

In the loop, your first assign to x the name of the Text property of
one of the text boxes. Then you assign to x a date. At no time are you
assigning anything to the Text property of any of your text boxes.
After all, the following pair of assignments, which are like what
you're doing:

                x = "y"
                x = z

does not have the same result as

                y = z

which is like what you're trying to do.

Replace the first two lines in your loop with

        x = "Text" & n
        Controls(x).Text = dt



Wed, 10 Jan 2001 03:00:00 GMT  
 Referring to controls - What am I doing wrong???
A control name can never be constructed with strings, ie you can never refer
to "Text1" as x=1, y = "Text" & x

Your solution, since you seem to need to relate to the controls in a
numerical fashion, would be to create a control array.

Create one "Text1" control, copy and paste it, and answer Yes to the dialog.
The first control changes to Text1(0) (the control name is Text1, and its
index becomes 0).  The newly pasted one is Text1(1).  Repeat as needed.
Then, in your loop, pass the loop variable as the index:

dt = <some value>

n = 0  'control arrays are 0-based

Do Until n = 8

    Text1(n) = dt

    dt = dt + 1
    n = n + 1
Loop

(I changed the  "= Date" part as others have indicated your implementation
was incorrect, and I did not test that part.)

--

Randy Birch, MVP Visual Basic

VBnet, The Visual Basic Developers Resource Centre
http://www.mvps.org/vbnet

Common Controls Replacement Project Member
http://www.mvps.org/ccrp


:I want to sequentially refer to some textbox controls as a variable and
:then change the .text value of each one to something different.  For
:instance:
:
:dt = Date
:n = 1
:Do Until n = 8
:    x = "Text" & n & ".Text"
:    x = dt
:    dt = dt + 1
:    n = n + 1
:Loop
:
:Why doesn't each textbox (named sequentially text1, text2, text2, etc)
:acquire a different date?
:
:
:Thanks  I. A.,
:Carroll Lastinger



Wed, 10 Jan 2001 03:00:00 GMT  
 Referring to controls - What am I doing wrong???

Quote:

>A control name can never be constructed with strings, ie you can never refer
>to "Text1" as x=1, y = "Text" & x

Well, you CAN if you use the composed string as a key to the Controls
collection of the form: Controls("Text" & x). Of course, the control
array is the way to go for this person's situation.


Wed, 10 Jan 2001 03:00:00 GMT  
 Referring to controls - What am I doing wrong???
<THICK>

OK .... assume a form with 3 text boxes, not in a array, named Text1, Text2
and Text3.  add the following code:

Private Sub Command1_Click()

   Dim x As Integer

   x = 1
   Print x, Controls("Text" & x)

   x = 2
   Print x, Controls("Text" & x)

   x = 3
   Print x, Controls("Text" & x)

End Sub

Result:
Text1
Text2
Text3

Change the Text2 text property to "hello"  Run the code:

Result:
Text1
hello
Text3

This tells me that the print statement returned the control's default
property, not its name.

Lock the second textbox, and change the code to

Private Sub Command1_Click()

   Dim x As Integer

   x = 1
   Print x, Controls("Text" & x).Locked

   x = 2
   Print x, Controls("Text" & x).Locked

   x = 3
   Print x, Controls("Text" & x).Locked

End Sub

Result:
False
True
False

Finally, change the code from Locked to Key. Result, error - object doesn't
support property or method.

I realize you agreed with my analysis that the user should use a control
array, so am just asking for some code to prove the methodology you suggest
is possible, namely "use the composed string as a key to the Controls
collection of the form" . I inferred from your remark that you were not
talking about creating and maintaining a separate collection just to index
the controls.  And I'll admit right now that I avoid using collections like
the plague ... never need them outside of what a control may naturally
provide transparently.

</THICK>
--

Randy Birch, MVP Visual Basic

VBnet, The Visual Basic Developers Resource Centre
http://www.mvps.org/vbnet

Common Controls Replacement Project Member
http://www.mvps.org/ccrp

:
:>A control name can never be constructed with strings, ie you can never
refer
:>to "Text1" as x=1, y = "Text" & x
:
:Well, you CAN if you use the composed string as a key to the Controls
:collection of the form: Controls("Text" & x). Of course, the control
:array is the way to go for this person's situation.
:



Wed, 10 Jan 2001 03:00:00 GMT  
 Referring to controls - What am I doing wrong???

 dt = Date
 n = 1
 Do Until n = 8
     x = "Text" & n & ".Text"       'Here the variant x contains a string of
Text1.Text'
     x = dt                     'and here it contains '26/7/98' etc. That is all that is
happening.
     dt = dt + 1
     n = n + 1
 Loop

To do what you want, set up your all text boxes on the screen as Text1 but
give each one a different Index
value. (You can even do this at run-time,but let's not worry about that
now.)

So you have 8 text boxes on your form. Text1(0),Text1(1) etc...

dt=Date
n=1
Do Until n=8
   Text1(n).Text=dt
   dt=dt+1
   n=n+1
Loop

Simple as that I think.

Andy Stafford.
Piquet.



Quote:
> I want to sequentially refer to some textbox controls as a variable and
> then change the .text value of each one to something different.  For
> instance:

> dt = Date
> n = 1
> Do Until n = 8
>     x = "Text" & n & ".Text"
>     x = dt
>     dt = dt + 1
>     n = n + 1
> Loop

> Why doesn't each textbox (named sequentially text1, text2, text2, etc)
> acquire a different date?

> Thanks  I. A.,
> Carroll Lastinger




Thu, 11 Jan 2001 03:00:00 GMT  
 Referring to controls - What am I doing wrong???


Fri, 19 Jun 1992 00:00:00 GMT  
 Referring to controls - What am I doing wrong???
Randy,

I did not follow the abovementioned argument, but going over your code, you
have
Dim x as Integer

I did the following:
lblLabel1 (a label) and cmdCommand (a commandbutton) on Form1

if I Dim x as Integer I get: Eror 13 Type mismatch
If I Dim x as String it works,

Does your Dim x as Integer really work, and if so, why?

Gert

=-=-=-=-=
Option Explicit
Dim loopf As Integer
Dim x As String

Private Sub cmdCommand_Click()
For loopf = 0 To Me.Controls.Count - 1
Print loopf
x = 1
Print Controls("lblLabel" + x).Caption
Next loopf

End Sub
=-=-=-===-=-



Thu, 11 Jan 2001 03:00:00 GMT  
 Referring to controls - What am I doing wrong???


Fri, 19 Jun 1992 00:00:00 GMT  
 Referring to controls - What am I doing wrong???
I'm not understanding what you're getting at. Controls("Text1") is
effectively the same thing as Text1 when used in procedures in Form
modules. In your demonstrations below, you show that using
Controls("Text" & x) gives, in each case, the same result as just
using TextN, where N is the numeric value in the variable x. This is
what I had said would happen. Explanatory comments below:

Quote:

><THICK>

>OK .... assume a form with 3 text boxes, not in a array, named Text1, Text2
>and Text3.  add the following code:

>Private Sub Command1_Click()

>   Dim x As Integer

>   x = 1
>   Print x, Controls("Text" & x)

>   x = 2
>   Print x, Controls("Text" & x)

>   x = 3
>   Print x, Controls("Text" & x)

>End Sub

>Result:
>Text1
>Text2
>Text3

>Change the Text2 text property to "hello"  Run the code:

>Result:
>Text1
>hello
>Text3

>This tells me that the print statement returned the control's default
>property, not its name.

The same as if the Print statements had read, respectively,

        Print x, Text1
        Print x, Text2
        Print x, Text3

- Show quoted text -

Quote:

>Lock the second textbox, and change the code to

>Private Sub Command1_Click()

>   Dim x As Integer

>   x = 1
>   Print x, Controls("Text" & x).Locked

>   x = 2
>   Print x, Controls("Text" & x).Locked

>   x = 3
>   Print x, Controls("Text" & x).Locked

>End Sub

>Result:
>False
>True
>False

The same as if you had had Text1.Locked, Text2.Locked and
Text3.Locked.

Quote:

>Finally, change the code from Locked to Key. Result, error - object doesn't
>support property or method.

The same as if you had had Text1.Key, Text2.Key and Text3.Key. Text
boxes don't have a Key property as, say, Node or ListItem objects do.


Thu, 11 Jan 2001 03:00:00 GMT  
 Referring to controls - What am I doing wrong???
Hmm.. interesting.  What you suggest I've never tried before, ie:

Controls("Text" & x) = "New text"

I was confusing the issue with a similar, yet different, "popular" request
... namely using the string and numeric value to build a control name
directly, and then set text to that, i.e.

   x = 1
  "Text" & x = "New Title"

My experience, and one that has served me well thus far, has been to the
control array when the need for index-referencing of controls was needed.
You've shown me an alternate method exists. Thanks.

--

Randy Birch, MVP Visual Basic

VBnet, The Visual Basic Developers Resource Centre
http://www.mvps.org/vbnet

Common Controls Replacement Project Member
http://www.mvps.org/ccrp

:I'm not understanding what you're getting at. Controls("Text1") is
:effectively the same thing as Text1 when used in procedures in Form
:modules. In your demonstrations below, you show that using
:Controls("Text" & x) gives, in each case, the same result as just
:using TextN, where N is the numeric value in the variable x. This is
:what I had said would happen. Explanatory comments below:
:
:
:><THICK>
:>
:>OK .... assume a form with 3 text boxes, not in a array, named Text1,
Text2
:>and Text3.  add the following code:
:>
:>Private Sub Command1_Click()
:>
:>   Dim x As Integer
:>
:>   x = 1
:>   Print x, Controls("Text" & x)
:>
:>   x = 2
:>   Print x, Controls("Text" & x)
:>
:>   x = 3
:>   Print x, Controls("Text" & x)
:>
:>End Sub
:>
:>Result:
:>Text1
:>Text2
:>Text3
:>
:>Change the Text2 text property to "hello"  Run the code:
:>
:>Result:
:>Text1
:>hello
:>Text3
:>
:>This tells me that the print statement returned the control's default
:>property, not its name.
:
:The same as if the Print statements had read, respectively,
:
: Print x, Text1
: Print x, Text2
: Print x, Text3
:
:>
:>Lock the second textbox, and change the code to
:>
:>Private Sub Command1_Click()
:>
:>   Dim x As Integer
:>
:>   x = 1
:>   Print x, Controls("Text" & x).Locked
:>
:>   x = 2
:>   Print x, Controls("Text" & x).Locked
:>
:>   x = 3
:>   Print x, Controls("Text" & x).Locked
:>
:>End Sub
:>
:>Result:
:>False
:>True
:>False
:
:The same as if you had had Text1.Locked, Text2.Locked and
:Text3.Locked.
:
:>
:>Finally, change the code from Locked to Key. Result, error - object
doesn't
:>support property or method.
:
:The same as if you had had Text1.Key, Text2.Key and Text3.Key. Text
:boxes don't have a Key property as, say, Node or ListItem objects do.
:



Fri, 12 Jan 2001 03:00:00 GMT  
 Referring to controls - What am I doing wrong???
: Hmm.. interesting.  What you suggest I've never tried before, ie:

: Controls("Text" & x) = "New text"

: I was confusing the issue with a similar, yet different, "popular" request
: ... namely using the string and numeric value to build a control name
: directly, and then set text to that, i.e.

:    x = 1
:   "Text" & x = "New Title"

Right, which is close to what Carroll was trying to do, except his version
amounted to

        x = "Text1.Text"
        x = "Hello"

and expecting that Text1 would now display "Hello".

: My experience, and one that has served me well thus far, has been to the
: control array when the need for index-referencing of controls was needed.
: You've shown me an alternate method exists. Thanks.

You're welcome. This method is useful, for example, in Office forms, where
control arrays are not available.



Sat, 13 Jan 2001 03:00:00 GMT  
 Referring to controls - What am I doing wrong???
: You're welcome. This method is useful, for example, in Office forms, where
: control arrays are not available.

Also, I just learned of a case where control arrays can be a hindrance in
VB. If you have classes that you use as control listeners, you can't use
them on controls that belong to an array.

I don't know exactly why this is, but it probably has something to do with
the fact that an event handler for a control in a control array has the
extra Index As Integer argument. So, I guess, a ListBox in an array is not
really exactly the same type of object as a scalar ListBox, and so forth,
since it has to raise events with the same name in a different manner. But
in your listener, you can only have a declaration of the form

        Public WithEvents m_ListBox as ListBox

so VB will only let you define event procedures for m_ListBox without the
Index As Integer argument.

What happens if you attach a listener to a control in an array, and your
code in the form module reads

        Dim lstn as MyListener
        Set lstn.m_ListBox = lbl(3)

is that you'll get a runtime error on the Set line, telling you that some
items (like control array items) can't send events to some others (like
classes).

In cases like the above, using the Controls("Text" & x) method allows one
to have an effective array that isn't as convenient to use as a real one
but that allows listeners to be applied to its members.



Sat, 13 Jan 2001 03:00:00 GMT  
 
 [ 13 post ] 

 Relevant Pages 

1. Referring to controls - What am I doing wrong???

2. ADO Data Control - What Am I Doing Wrong?

3. Will somebody tell me what I am doing wrong with this ListView Control

4. Recordset: What am I doing wrong?

5. What am I doing wrong??

6. What am I doing wrong?

7. Help with syntax. What am I doing wrong

8. What am I doing wrong?

9. What am I doing wrong?

10. What am I doing wrong?

11. Please tell me what I am doing wrong - DAO parameter Append Query :(

12. What am I doing wrong? -MultiSelect List box

 

 
Powered by phpBB® Forum Software