making code more 'compact'

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have code like:

With cmd_City
.FontBold = True
.FontSize = 12
.Forecolor = RGB(255,0,0)
etc....
End With

My problem is that I want to apply these IDENTICAL properties to several
different command buttons on the form (cmd_City, cmd_Division, cmd_Region,
etc...)

Is there a more compact way to set these properties other than just
repeating the same 'With - End With' code for each one?

thanks
 
Perhaps you should consider converting it into a function and simply calling
it as required or create an array of the control and then simply loop through
them.

Daniel
 
Sarah,

Try using a sub with the control name as a variable, then calling the sub
for each button:

Private Sub CmdFormat(myBtn as string)
With Me(myBtn)
.FontBold = True
...
End With
End Sub

....
'Format the City button.
CmdFormat "cmd_City"
....

OR, do them all at the same time by looping:

Private Sub FormatButtons()
Dim ctl as Control

For each ctl in Me.Controls
If ctl.ControlType = acCommandButton then
'Put another IF/THEN here testing on ctl.ControlName if
' only certain ones are to be changed.
ctl.FontBold = True
ctl.FontSize = 12
...
End If
Next ctl

End Sub

HTH,
Bruce
 

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

Back
Top