Apply vba code to multiple userform objects

S

sjoopie

Thanks for the help,

on the following line:

ctl.BackStyle = fmBackStyleOpaque

i receive error message 43
 
N

Nikos Yannacopoulos

That's because you are trying to set the backstyle property of a control
that doesn't have one, such as a command button, for instance. You need to
make some provision in your code to set the property of the desired controls
only. Looking at the names of the controls in your first post, it looks like
the following could work:

For Each ctl In ProjectenForm.Controls
If Left(ctl.Name, 2) = "Eg" Or Left(ctl.Name, 2) = "Mg" Then
ctl.BackStyle = fmBackStyleOpaque
End If
Next

provided that the name of the control causing the error doesn't start with
Eg or Mg.

HTH,
Nikos
 
B

Bob Phillips

Or test the control type

For Each ctl In ProjectenForm.Controls
If Typename(ctl) = "TextBox" Then
ctl.BackStyle = fmBackStyleOpaque
End If
Next


--

HTH

RP
(remove nothere from the email address if mailing direct)


Nikos Yannacopoulos said:
That's because you are trying to set the backstyle property of a control
that doesn't have one, such as a command button, for instance. You need to
make some provision in your code to set the property of the desired controls
only. Looking at the names of the controls in your first post, it looks like
the following could work:

For Each ctl In ProjectenForm.Controls
If Left(ctl.Name, 2) = "Eg" Or Left(ctl.Name, 2) = "Mg" Then
ctl.BackStyle = fmBackStyleOpaque
End If
Next

provided that the name of the control causing the error doesn't start with
Eg or Mg.

HTH,
Nikos
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top