Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Type mismatch error using For Each...Next with cmd buttons

Status
Not open for further replies.

Birddog

Programmer
May 30, 2001
15
US
Please tell me why the following procedure would give me a type mismatch error after disabling all the command buttons on a form except the last one.

I have tried deleting the last button but then the error just backs up one button giving me the error on the "new" last button on the form.

Thanks!


Code:
Private Sub cmdDisableButtons_Click()
On Error GoTo err_cmdDisableButtons_Click
Dim cmdButton As CommandButton
    
For Each cmdButton In Form
    
 Text4.SetFocus 'avoids error when setting enable = false
                 'for command button which has focus.
        
 'Debug line follows, just displays name of each button
 MsgBox cmdButton.Name
        
 cmdButton.Enabled = False
       
Next cmdButton

exit_cmdDisableButtons_Click:

Exit Sub

err_cmdDisableButtons_Click:

'GoTo exit_cmdDisableButtons_Click
MsgBox Err.Number & " " & Err.Description

End Sub
 
I'm not entirely sure I understand what you're doing but try this code (it's yours with MINOR adjustments). I didn't really get a type mismatch but plenty object required or not supported errors so I may not have properly recreated your problem!

If 'Text4' is a text box (which I've assumed it is) this is probably the cause of your type mismatch ad creates a new eror in my code if left inside the loop.

Private Sub cmdDisableButtons_Click()
On Error GoTo err_cmdDisableButtons_Click
Dim cmdButton As Control 'CHANGED FROM COMMANDBUTTON

' MOVED OUT OF LOOP AS IT CRASHES HERE
Text4.SetFocus 'avoids error when setting enable = false
'for command button which has focus.

For Each cmdButton In Me.Controls 'CHANGED FROM FORM

'Debug line follows, just displays name of each button
MsgBox cmdButton.Name

cmdButton.Enabled = False

Next cmdButton

exit_cmdDisableButtons_Click:

Exit Sub

err_cmdDisableButtons_Click:

'GoTo exit_cmdDisableButtons_Click
MsgBox Err.Number & " " & Err.Description
End Sub
 
Thanks for the help Loomah.

I still get the type mismatch error.

What I'm trying to do is write generic code to disable all command buttons on a form after the user presses any command button. I will eventually put this code in a procedure which I will call as the click event of my buttons.

The reason for this is some of our user have slower computers and will repeatedly press a command button even after the click event has started processing (patience is a virtue and in short supply here).

I could reference each button individually but this would take many, many hours.

Thanks for your time though!

 
I don't understand at all now!!

The above code works (xl2000) on a form containing 5 buttons and 1 text box. It loops through each control giving the name then disables them all - including the text box.

Out of interest, did you copy the above code into your project?
 
Loomah,

I'm sorry.

No, I didn't copy your code (I should have) & I missed one of your modifications. To make matters worse, you had commented the change so I wouldn't miss it!

Everything is working now. I really appreciate your help!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top