PDA

View Full Version : Checkboxes - simplifying code



chrishon
10-05-2006, 11:18 AM
I have 20 checkboxes on a form. If checkbox is checked, it perfoms a task. Is there a simplier way of doing this? For each checkbox I have:
If chkBox1 = True Then
Selection.TypeText "Text1"
End If

If chkBox2 = True Then
Selection.TypeText "Text2"
End If

fumei
10-05-2006, 11:38 AM
We need more information.

Are these checkboxes in a document, or on a userform?

If in the document, are these checkboxes formfields, or are they ActiveX checkboxes?

How are you firing these pieces of code?
If checkbox is checked, it perfoms a task.What procedure is doing the checking if it is checked?

chrishon
10-05-2006, 11:54 AM
Checkboxes are on user form. If I understand your question correctly, it fires when the user hits the OK button. It runs through and checks if any boxes are checked. If they are, then it puts the text (for that checkbox) in a Word table document. The procedure is just if statements. I have the code and user form if that would be more helpful.

fumei
10-05-2006, 01:13 PM
OK, it is on a userform. This was important to know. OK, the OK button is doing a validation of the values of the checkboxes.

First off, I notice that you using Selection.TypeText. Why? Can you be sure that the Selection is at the correct place? I ask because I don't know what the circumstances are.

Is this userform used to do one thing? The user makes a choice from a bunch of checkboxes, and then you type in text at the Selection point? What if the user check three boxes - what then? Or are you using a Frame to only allow one choice.

Yes, perhaps the code and the file may help. While you do that, I am attaching a file. To fire the userform click the "Show Form" icon on the top toolbar. Take a look at the code for the userform.

fumei
10-05-2006, 01:15 PM
Again though, it would help if you fully stated what you are trying to do. Using Selection.TypeText is generally not a very good design for action. It should be avoided if possible.

chrishon
10-06-2006, 06:58 AM
Attached is the file. It is a template in Word and when you open the document the form comes up. You fill in the text boxes and then check any subfiles you may want (being sure to tell what label to start with and how many labels are needed). When you hit ok, it puts the labels in the Word document. If you need more than one label, it copies the number you need in the document. The reason I'm using Selection.TypeText is because that was the way I was taught, that is why I'm trying to find a better and easier way of doing this. Hope you can help. I looked at your file and code this seems much easier, but how does it know what text to put in the document?

fumei
10-10-2006, 12:06 PM
I will take a deeper look when i have the time, but I immediately would suggest using Select Case, rather than a gazillion If...Then statements. For example:If cmbPosition.Value = 1 Then
Selection.GoTo What:=wdGoToBookmark, Name:="Client1"
End If

If cmbPosition.Value = 2 Then
Selection.GoTo What:=wdGoToBookmark, Name:="Client2"
End If

If cmbPosition.Value = 3 Then
Selection.GoTo What:=wdGoToBookmark, Name:="Client3"
End If

If cmbPosition.Value = 4 Then
Selection.GoTo What:=wdGoToBookmark, Name:="Client4"
End If

If cmbPosition.Value = 5 Then
Selection.GoTo What:=wdGoToBookmark, Name:="Client5"
End If

If cmbPosition.Value = 6 Then
Selection.GoTo What:=wdGoToBookmark, Name:="Client6"
End If
' etc etc etc' you have 30 If..Then statements.Note that your code MUST evaluate every single If...Then statement. Every single If...Then is actually parsed and evaluated to see if it is True, or False. Here is a better way:Select Case cmbPosition.Value
Case 1
Selection.GoTo What:=wdGoToBookmark, _
Name:="Client1"
Case 2
Selection.GoTo What:=wdGoToBookmark, _
Name:="Client2"
Case 3
Selection.GoTo What:=wdGoToBookmark, _
Name:="Client3"
' etc etc
End SelectThe difference is that what is checked is the value of cmbPosition.Value....ONCE. This is much more efficient code.

how does it know what text to put in the document?
I don't understand. It puts whatever text you tell it to put. You posted:
It runs through and checks if any boxes are checked. If they are, then it puts the text (for that checkbox) in a Word table document. So that it what I did. I had it put "yes" then the name of the checkbox. I am not sure what "text (for that checkbox)" actually means.

You tell ME what text you want it to put in, and then I will have it do that.

chrishon
10-11-2006, 12:41 PM
Thank you for your suggestions. I will use the Select Case instead of the If statements. I knew there had to be a better way, I just didn't know what it was.

For the checkboxes, the code is set up so that if the Correspondence box is checked, it puts the text "Correspondence" in the document. If the Notes & Memorandum box is checked, it puts in "Notes & Memorandum", etc. See below:

If chkCorrespondence = True Then
Call format
Selection.TypeText "CORRESPONDENCE"
Call CopySubFile
End If

I think this is what you are asking. Thank you again for your help.