Results 1 to 2 of 2
  1. #1
    Join Date
    May 2009
    Posts
    1

    Unanswered: loop control issue

    Ive been trying to loop checkboxes with textboxes in vba excel. If chk1 is checked then txt 1 is enabled that will allow the user to input a value. The final step is to create a string with chk1 caption with txt value. the checkboxes code works as well as the enable code however ive been unsuccessful with looping the textboxes
    Private Sub chk2_Click()
    txt2.Enabled = chk2
    End Sub
    Private Sub chk4_Click()
    txt4.Enabled = chk4
    End Sub
    Private Sub chk1_Click()
    txt1.Enabled = chk1
    End Sub

    Private Sub cmdAdd_Click()

    Dim chk As Control
    Dim Aux As String
    Dim TB As String
    Dim b As Integer

    For b = 1 To 19
    TB = frmLogBook.Controls("txt" & i).Value
    Next b


    For Each chk In frmLogBook.Controls
    If TypeName(chk) = "CheckBox" Then
    If chk.Object.GroupName = "auxiliary" Then
    If chk.Value = True Then
    Aux = chk.Caption & " " & TB & vbNewLine
    End If
    End If
    End If
    Next chk

    Can anybody help??
    Last edited by lulu1018; 05-27-09 at 12:43.

  2. #2
    Join Date
    May 2009
    Posts
    258
    I see a couple problems with the following code:
    Code:
    For b = 1 To 19
    TB = frmLogBook.Controls("txt" & i).Value
    Next b
    First, you are looping on a variable "b", but you are using a variable "i" to get the textbox name. Second, all this logic will do, if it were to work, would be to continuously set the "TB" variable over and over again (19x) without doing anything with it.

    You probably want to do something similar to the following:
    Code:
    For b = 1 To 19
    	TB = frmLogBook.Controls("txt" & b).Value
    
    	For Each chk In frmLogBook.Controls
    		If TypeName(chk) = "CheckBox"
    			If chk.Object.GroupName = "auxiliary" Then
    				If chk.Value = True Then
    					Aux = chk.Caption & " " & TB & vbNewLine
    				End If
    			End If
    		End If
    	Next chk
    Next b
    You may still have a problem with the following statement, unless you do something with it later, since it just resets the string over and over again:
    Code:
    Aux = chk.Caption & " " & TB & vbNewLine
    Regards,

    Ax

Posting Permissions

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