Solved

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

Posted on 2003-11-07
6
367 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
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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 Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

830 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