Looping through User Form controls

mikeymay

Well-known Member
Joined
Jan 17, 2006
Messages
1,600
Office Version
  1. 365
Platform
  1. Windows
I am trying to use the following to check each Textbox in a user form
Code:
Private Sub CheckForm()

Dim ctlText As Control

Application.ScreenUpdating = False

btnAmend.Enabled = False

For Each ctlText In frmAmend.Controls
   If TypeName(ctlText) = "TextBox" Then
      If ctlText <> "" Then
         btnAmend.Enabled = True
         Else
      End If
   End If
Next ctlText

Application.ScreenUpdating = True

End Sub
but I am getting a Type Mismatch error on
Code:
For Each ctlText In frmAmend.Controls
I am struggling to see what is wrong here as from looking at several forums, this is suggested a number of times of how to loop through user form controls.

TIA
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Is the CheckForm routine in the userform module ?

Try declaring the ctlText var as object :
Code:
Dim ctlText As Object
 
Upvote 0
Personally I'd suggest you declare it as MSForms.Control rather than late bound Object. I'd guess you have a reference set to another library with a Control class in it (e.g. Access).
 
Upvote 0
Yes I'm using the form to send data to an access table.

Assuming I can't use the Access ref and .Forms as the same time?
 
Upvote 0
Yes you can, you just need to be more explicit when declaring variables.
 
Upvote 0
Ok so I have the access and forms refs checked so what do I need to declare the control as if not an object?
 
Upvote 0
What I said earlier. :)

Rich (BB code):
Dim ctlText As MSForms.Control
 
Upvote 0
Sorted!!

All working as expected now.

Thanks all for your input.
 
Upvote 0

Forum statistics

Threads
1,214,585
Messages
6,120,394
Members
448,957
Latest member
Hat4Life

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top