Solved

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

Posted on 2003-11-07
6
359 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…
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…

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

16 Experts available now in Live!

Get 1:1 Help Now