Consulting

Results 1 to 4 of 4

Thread: Solved: Looping Through Checkboxes

  1. #1

    Solved: Looping Through Checkboxes

    Good afternoon,
    I am trying to get this bit of code to loop through and see which checkboxes are checked and pull up the information associated with it. Each checkbox will be associated with a database, and if multiple checkboxes are checked, it will pull up the requested database information. I just cant figure out how to loop the code to see which boxes are checked. Yes, the Diplay Trainer Name in Document area is messed up to, but I think it will give me less trouble then the loop.

    [VBA] Public Sub PrintClientList()
    'declare variables and assign address to object variables
    Dim balTrainer As Balloon, intReturnValue As Integer
    Dim blnOpen As Boolean, strFileName As String, docClient As Document, tblClient As Table
    Set balTrainer = Assistant.NewBalloon
    Set docClient = Application.Documents("T11-WD-E1D2.doc")
    'display assistant
    Assistant.On = True
    Assistant.Visible = True
    'create and display balloon
    With balTrainer
    .Heading = "Trainers"
    .Text = "Select Trainer(s)"
    .Checkboxes(1).Text = "George"
    .Checkboxes(2).Text = "Nora"
    .Checkboxes(3).Text = "Pam"
    .Button = msoButtonSetOkCancel
    End With
    intReturnValue = balTrainer.Show
    'display trainer name in document
    Select Case intReturnValue
    Case 1
    docClient.Paragraphs(6).Range.Text = _
    "Trainer:" & vbTab & balTrainer.Checkboxes(1).Text & vbNewLine
    Case 2
    docClient.Paragraphs(6).Range.Text = _
    "Trainer:" & vbTab & balTrainer.Checkboxes(2).Text & vbNewLine
    Case 3
    docClient.Paragraphs(6).Range.Text = _
    "Trainer:" & vbTab & balTrainer.Checkboxes(3).Text & vbNewLine
    End Select
    'delete existing tables
    For Each tblClient In docClient.Tables
    tblClient.Delete
    Next tblClient
    'get the name of the Access database
    With Application.Dialogs(wdDialogFileOpen)
    .Name = "*.mdb"
    blnOpen = .Display
    strFileName = .Name
    End With
    If blnOpen = True And Right(strFileName, 4) = ".mdb" Then
    'insert database information and display Print dialog box
    Selection.EndKey unit:=wdStory
    Selection.Range.InsertDatabase Format:=wdTableFormatClassic2, Style:=63, _
    connection:="table client", DataSource:=strFileName
    Application.Dialogs(wdDialogFilePrint).Show
    End If

    End Sub [/VBA]

  2. #2
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    This is a basic loop for Check Boxes.
    [vba]
    Option Explicit

    Sub Macro1()

    Dim ObjCheck As FormField

    For Each ObjCheck In ActiveDocument.FormFields
    If ObjCheck.Type = wdFieldFormCheckBox Then
    If ObjCheck.CheckBox.Value = True Then

    'Your Code Here
    MsgBox ObjCheck.Name


    End If
    End If
    Next

    End Sub
    [/vba]

  3. #3
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Hi corngopher,

    Welcome to VBAX!

    You have your Balloon's collection of Checkboxes; just loop through them. What you are doing wrong is trying to use the return from the Show to identify the checkbox - it identifies the button.

    Try something like this

    [VBA]:
    :
    intReturnValue = balTrainer.Show
    If intReturnValue = msoBalloonButtonOK Then
    For Each cb In balTrainer.Checkboxes
    If cb.Checked Then
    docClient.Paragraphs(6).Range.Text = "Trainer:" & vbTab & cb.Text & vbNewLine
    End If
    Next
    :
    :
    ' rest of your code
    End If[/VBA]
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  4. #4
    Thank you both for the excellant code. It all seems so easy after you guys post it

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •