Writing clean If Then code

R

Roger Marrs

I've written the following code. It gets the job done, but I'm wondering if
there isn't a cleaner way to write it.

If TypeOfCounselAppointed.Value = True Then
..FormFields("CounselTypeAppointed").CheckBox.Value = True
End If
If TypeOfCounselRetained.Value = True Then
..FormFields("CounselTypeRetained").CheckBox.Value = True
End If
If TypeOfCounselSelf.Value = True Then
..FormFields("CounselTypeSelf").CheckBox.Value = True
End If
If TypeOfCounselOther.Value = True Then
..FormFields("CounselTypeOther").CheckBox.Value = True
End If

Thanks, Roger
 
J

Jonathan West

Hi Roger

You don't need

If TypeOfCounselAppointed.Value = True Then

you can use this instead

If TypeOfCounselAppointed.Value Then

I'm assuming that you have a UserForm with checkboxes whose values you are
wanting to reflect in the matching checkboxes in the formfields of a
document form.

If this is so, and if you can organise the userform in the following way,
then the code can be greatly simplified

- The checkboxes in the userform all have their equivalents in the document,
and
- The names of the checkboxes match the formfield names in the document.

if all this is true, then you can write the following code, which will allow
you to automatically process as many checkboxes as you have

Dim oControl as Control
For each oControl in Me.Controls
If TypeOf oControl Is MsForms.CheckBox
.FormFields(oControl.Name).CheckBox.Value = oControl.Value
End If
Next oControl

--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
R

Roger Marrs

Jonathan,
Thanks for the reply. The controls on my UserForm are mostly OptionButtons,
but the controls in the Document are CheckBoxes. However, I was able to
change the code you suggested to accomodate that difference, and it works
great, plus my code looks much better. Thanks.
Roger Marrs
 

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