Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Referring to a many CheckBoxes for a FOR Statement, and performing the same test for each CheckBox.

Posted on 2006-06-21
7
Medium Priority
?
213 Views
Last Modified: 2010-04-08
I have a form in Outlook which has 10 check boxes. What i won't to do is perform the same test for each checkbox. If the checkboxes are Named CheckBox1 to CheckBox10, how do i refer to them so i can use a "For Checkbox1 to CheckBox10", do my test?
0
Comment
Question by:james_daley
  • 4
  • 3
7 Comments
 
LVL 76

Expert Comment

by:David Lee
ID: 16958510
Greetings, james_daley.

You need to set the checkboxes up as a control array.  The checkboxes then become CheckBox(0) through CheckBox(9).  You can then use either of these two approaches to itterate through them:

    For Each Item in CheckBox

or

    For x = 0 to 9
        CheckBox(x)
    Next

I've never tried to use a control array in an Outlook form though and am not certain they're supported.  I'll check that as soon as I can.

Cheers!
0
 
LVL 1

Author Comment

by:james_daley
ID: 16965885
looking at other questions posted it seems that i need to name all the checkboxes the same but change the index value, then the code would work. problem is there is no property i can find in outlok that supports this index. any ideas?
0
 
LVL 76

Expert Comment

by:David Lee
ID: 16972693
Sorry about that, James.  I move back and forth between VB, VBA, and VBScript and get them mixed up sometimes.  Here's how to do this in code behind an Outlook form.  This is just and example, but you can see how the process works.

Sub CommandButton1_Click()
    Dim objForm, objControl, strName, intIndex
    'Get the Outlook form object
    Set objForm = Item.GetInspector.ModifiedFormPages("Message")
    For intIndex = 1 to 2
        'Build the control name
        strName = "Checkbox" & intIndex
        'Get the control from the Controls collection of the form object
        Set objControl = objForm.Controls(strName)
        'Set or test the control's value
        objControl.Value = Not objControl.Value
    Next
    Set objControl = Nothing
    Set objForm = Nothing
End Sub
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 1

Author Comment

by:james_daley
ID: 16989652
ok so as i have no programming experience i'm a little lost. in particular the "Set objForm =..." puzzles me as it seems to stop there all the time and i don't understand the "message" part. what do i need to change in this code given my form is called UserForm1, Checkboxes named CheckBoxes1 to CheckBoxes 10, and the test i wanna perform is if the CheckBox is ticked insert name ( caption of checkbox) into text file, if not then don't write it. hopefully after going through 1 to 10 i get a list of names of who's ticked and this list should reset everytime the command button is hit. sorry about the lack of understanding!

thanks again
0
 
LVL 76

Expert Comment

by:David Lee
ID: 17006291
> in particular the "Set objForm =..." puzzles me
Ok, I'll explain.  That line of code is getting a particular form page.  We need to get the form object because it contains a collection of control objects, and that's what we need to access.  In my example I was using a message form and the checkboxes happened to be on the main page which is named "Message".  Substitute the name of the page the checkboxes are on in your custom form.  The code for the scenario you described above would be something like this:

'Get the form the controls are on
Set objForm = Item.GetInspector.ModifiedFormPages("MyPageName")
For intIndex = 1 To 10
    'Build the control name
    strName = "Checkbox" & intIndex
    'Get the control from the Controls collection of the form object
    Set objControl = objForm.Controls(strName)
    'If the box is checked
    If objControl.Value Then
        'Code to insert the checkbox caption in the text file
    End If
Next
0
 
LVL 1

Author Comment

by:james_daley
ID: 17007172
it still get stuck at "MyPageName". what i have is a custom userform i've made called UserForm1, wth no pages, just 2 command buttons, some radio buttons which controls which check boxes start off ticked and unticked tick and a logo. even if i create 2 pages in the form called Page1 and Page2, if i reference the MyPageName as Page1 which all the controls are on i till get th same pop up "Run Time error 424: Object Required".
0
 
LVL 76

Accepted Solution

by:
David Lee earned 2000 total points
ID: 17007961
You never mentioned that this is userform, so I was under the impression that this was a custom Outlook form.  In Outlook "form" represents a contact, task, appointment, message, etc. form.  No, userforms don't have pages.  Doing this in a userform will look like this instead.

Dim objForm As UserForm

Private Sub CommandButton1_Click()
    Dim strMessage As String, _
        strName As String, _
        objControl As Control, _
        intIndex As Integer
    For intIndex = 1 To 10
        strName = "Checkbox" & intIndex
        Set objControl = objForm.Controls(strName)
        If objControl.Value Then
            'Code to insert the checkbox caption in the text file
        End If
    Next
    Set objControl = Nothing
End Sub

Private Sub UserForm_Initialize()
    Set objForm = Me
End Sub
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article lists the top 5 trialware OST to PST Converter Tools. These tools save a lot of time for users when they want to convert OST to PST after their Exchange server is no longer available or other critical issues with Exchange server or impo…
The core idea of this article is to make you acquainted with the best way in which you can export Exchange mailbox to PST format.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question