PDA

View Full Version : Solved: Looping Through Checkboxes



corngopher
03-06-2005, 02:31 PM
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.

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

Jacob Hilderbrand
03-06-2005, 03:23 PM
This is a basic loop for Check Boxes.

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

TonyJollans
03-06-2005, 05:15 PM
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

:
:
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

corngopher
03-06-2005, 06:45 PM
Thank you both for the excellant code. It all seems so easy after you guys post it :)