Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loop through CkBoxs and if cecked copy caption to Text box

Status
Not open for further replies.

act2

Technical User
Dec 17, 2005
34
US
I would like to be able to look at the captions of 5 check boxes and based on if there’re checked copy the name (caption) to a sub form text box. I’m not sure if I can do it and what the syntax should be. The code that I’m playing with is this:


Code:
Dim ctl As Control, flg As Boolean
   Dim Msg As String, Style As Integer, Title As String
   
   For Each ctl In Me.Controls
      If ctl.ControlType = acCheckBox Then
         If ctl Then
           If  flg = True Then
   	[COLOR=red]ctl.caption = Forms.frmMain.frmsubMain.Form.TestBox [/color] 
          Exit For
         End If
      End If
End If
   Next
 
how about this
where you have ctl.caption insert
ctl.Properties("Caption")

-Pete
 
It seems you're copying from the subform to this, and not what you say, but to retrieve a checkbox "caption" - well, I'm assuming now you mean the label which is attached? If so, you cab reach it through

[tt]ctl.controls(0).caption[/tt]

Roy-Vidar
 
Roy and Pete Thanks a lot for your help on this
I guess I’m confusing the issue here. What I have done just to test the code is on a form. I have a text box unbound to see if I get any results. I will worry about the sub form part later. Tried your code and it compiled but I saw no results in the text box. I must have a flaw in my logic, suggestions needed. Thanks




Code:
Private Sub cmdEnter_Click()


Dim ctl As Control, flg As Boolean
   
   
   For Each ctl In Me.Controls
      If ctl.ControlType = acCheckBox Then
         If ctl Then
           If flg = True Then
               [COLOR=red] ctl.Controls(0).Caption = Me.test [/color]
      
      
         End If
      End If
End If
   Next

End Sub
 
Well, there can be several reasons,

one - that you are not dealing with altering a textbox, but presumably a label attached to a checkbox

two - since you haven't given the variable "flg" any value, it's instantiated as false, thus you won't get into that part of the loop (it needs three conditions, it must be a checkbox, the checkbox must be checked, and the flg variables must be true)

What you are doing, had it worked, is to put the contents of something called "test" into the label attached to a checkbox.

Roy-Vidar
 
Thanks Roy, Let me start again. I want the Caption next to the checkbox to be inserted in to a text box if checked. If I did it with out looping I would do this.

Dim Ck as String
Ck = Me.Label25.Caption
Me.test.Value = Ck

I'm new to all this and just learning and may have my terminology may be askew, but I really enjoy the challenge. Thanks for taking your time to help me.
 
Yes, you would probably do the same now (just direct assigning)

[tt]Me.test = ctl.Controls(0).Caption

' in stead of

ctl.Controls(0).Caption = Me.test [/tt]

- after you've ensured the flg thinge also has correct value

Roy-Vidar
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top