Solved

Type mismatch using "for each" with controls on a form.

Posted on 2003-11-07
6
369 Views
Last Modified: 2010-05-01
This must seem very basic. I haven't used for each much before, but I seem to remember seeing similar examples to this somewhere...

In the example below, I have 4 textboxes on a form and a command button. I want to change the background of all the textboxes.  When I run the program, the textboxes do change colour, but I get "run time error 13 - type mismatch.". What am I doing wrong?

Private Sub Command1_Click()
 Dim t As TextBox
 For Each t In Me
    t.BackColor = RGB(100, 0, 0)
 Next
End Sub
0
Comment
Question by:molar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 29

Expert Comment

by:leonstryker
ID: 9700390
I do not think the error is raised here.  Since you only have 4 TextBoxes on the form, I suggest you step through the code and see exactly which line causes the error.  Also, launch your application with full compile (Ctl+F5), there maybe compilation errors which are causing your message.

Leon
0
 
LVL 11

Expert Comment

by:dbrckovi
ID: 9700391
Try this:

Private Sub Command1_Click()
   
      For Each TextBox In Me
           TextBox.BackColor = RGB(100, 0, 0)
      Next

End Sub
0
 
LVL 11

Expert Comment

by:dbrckovi
ID: 9700399
Doesn't have to be TextBox

It can be anything:


Private Sub Command1_Click()
   
   For Each aaa In Me
       aaa.BackColor = RGB(100, 0, 0)
   Next

End Sub
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Expert Comment

by:dbrckovi
ID: 9700447
Molar!

According to MSDN help on For---Each Statement, your example won't work becouse you use an object for itteration, and it should be a variable.

Here is the whole text from MSDN:

---------------------------------------------------------------------------------
For Each...Next Statement

Repeats a group of statements for each element in an array or collection.

Syntax

For Each element In group
[statements]
[Exit For]
[statements]

Next [element]

The For...Each...Next statement syntax has these parts:

Part Description
- element              Required.            Variable used to iterate through the elements of the collection or array. For collections, element can only be a Variant variable, a generic object variable,                                                       or any specific object variable. For arrays, element can only be a Variant variable.
- group                 Required.             Name of an object collection or array (except an array of user-defined types).
- statements          Optional.             One or more statements that are executed on each item in group.


Remarks

The For...Each block is entered if there is at least one element in group. Once the loop has been entered, all the statements in the loop are executed for the first element in group. If there are more elements in group, the statements in the loop continue to execute for each element. When there are no more elements in group, the loop is exited and execution continues with the statement following the Next statement.

Any number of Exit For statements may be placed anywhere in the loop as an alternative way to exit. Exit For is often used after evaluating some condition, for example If?Then, and transfers control to the statement immediately following Next.

You can nest For...Each...Next loops by placing one For...Each...Next loop within another. However, each loop element must be unique.

Note   If you omit element in a Next statement, execution continues as if element is included. If a Next statement is encountered before its corresponding For statement, an error occurs.

You can't use the For...Each...Next statement with an array of user-defined types because a Variant can't contain a user-defined type.
-----------------------------------------------------------------
0
 
LVL 1

Accepted Solution

by:
Crazy_king_in earned 250 total points
ID: 9700466
Try this Simple

Dim Ctl As Control

For Each Ctl In Me
    If TypeOf Ctl Is TextBox Then
        Ctl.BackColor = vbRed
    End If
Next
0
 
LVL 2

Author Comment

by:molar
ID: 9700612
Bingo.

I guess my code must have been trying to set the backcolor of the command button as well.

Doh!

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

751 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