Consulting

Results 1 to 3 of 3

Thread: Removing certain text from a textbox ???

  1. #1

    Exclamation Removing certain text from a textbox ???

    I have some tick boxes and a textbox. if a tick box is ticked then a message is put into the textbox. EG-

    if FlaggedSection3 is ticked then the textbox will add "Section 3" to it. An example - if I tick all three tick boxes (FlaggedSection1, FlaggedSection 2 and FlaggedSection3) then the textbox will say:

    Section 1 Section 2 Section 3.

    However, how do I do it so that if a tickbox is unchecked then the corrisponding message is taken out of the textbox. So if i untick FlaggedSection2 then the textbox will then read:

    Section 1 Section 3

    my current code for one of the tickboxes is:

    Private Sub FlagSection3_Click()
    'If Section 3 is flagged then display in the Flagged textbox at the bottom
    If FlagSection3.Value = True Then
    txtFlagged.Text = txtFlagged.Text + "Section 3 "
    End If
    End Sub



    Similar for the other two tickboxes,

    Thanks Carl.

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location

    Answer to the question posted (why do I need a title?)

    Carl,

    Building strings form various combinations of variables can be fun and challenging. You basically need to evaluate "everything" with "every" change. In hindsight, I probably wouldn't have taken the time to work this out in this clunky fashion, but it should give you the gist of the process:

    [VBA]Option Explicit
    Private strText As String
    Private Sub CheckBox1_Change()
    If Me.CheckBox1 Then
    strText = "CheckBox 1 is checked."
    Select Case True
    Case Me.CheckBox2, Me.CheckBox3
    strText = "Check 1, Check 2 and Check 3 are checked"
    Case Me.CheckBox2
    strText = "Check 1 and Check 2 are checked"
    Case Me.CheckBox3
    strText = "Check 1 and Check 3 are checked"
    End Select
    Else
    Select Case True
    Case Me.CheckBox2, Me.CheckBox3
    strText = "Check 2 and Check 3 are checked"
    Case Me.CheckBox2
    strText = "Check 2 is checked"
    Case Me.CheckBox3
    strText = "Check 3 is checked"
    Case Else
    strText = ""
    End Select
    End If
    Me.TextBox1 = strText
    End Sub
    Private Sub CheckBox2_Change()
    If Me.CheckBox2 Then
    strText = "CheckBox 2 is checked."
    Select Case True
    Case Me.CheckBox1 And Me.CheckBox3
    strText = "Check 1, Check 2 and Check 3 are checked"
    Case Me.CheckBox1
    strText = "Check 1 and Check 2 are checked"
    Case Me.CheckBox3
    strText = "Check 2 and Check 3 are checked"
    End Select
    Else
    Select Case True
    Case Me.CheckBox1 And Me.CheckBox3
    strText = "Check 1 and Check 3 are checked"
    Case Me.CheckBox1
    strText = "Check 1 is checked"
    Case Me.CheckBox3
    strText = "Check 3 is checked"
    Case Else
    strText = ""
    End Select
    End If
    Me.TextBox1 = strText
    End Sub
    Private Sub CheckBox3_Change()
    If Me.CheckBox3 Then
    strText = "CheckBox 3 is checked."
    Select Case True
    Case Me.CheckBox1 And Me.CheckBox2
    strText = "Check 1, Check 2 and Check 3 are checked"
    Case Me.CheckBox1
    strText = "Check 1 and Check 3 are checked"
    Case Me.CheckBox2
    strText = "Check 2 and Check 3 are checked"
    End Select
    Else
    Select Case True
    Case Me.CheckBox1 And Me.CheckBox2
    strText = "Check 1 and Check 2 are checked"
    Case Me.CheckBox1
    strText = "Check 1 is checked"
    Case Me.CheckBox2
    strText = "Check 2 is checked"
    Case Else
    strText = ""
    End Select
    End If
    If strText = "" Then
    strText = "No boxes are checked"
    End If
    Me.TextBox1 = strText

    End Sub
    [/VBA]

    You can see that even with 3 checkboxes it becomes pretty gnarly

    As the number of checks options becomes larger the process can quickly become unmanageble. In this case you would give your checkboxes a common groupname e.g., CheckBoxA1, CheckBoxA2 etc. where "A" is the group identifier. Then you can call a function with the checkbox change event that evaluates all like "group" controls and builds the string:


    [VBA]Option Explicit
    Private Sub CheckBoxA1_Change()
    Me.TextBox1.Text = BuildString("CheckBoxA")
    End Sub
    Private Sub CheckBoxA2_Change()
    Me.TextBox1.Text = BuildString("CheckBoxA")
    End Sub
    Private Sub CheckBoxA3_Change()
    Me.TextBox1.Text = BuildString("CheckBoxA")
    End Sub
    Private Sub CheckBoxA4_Change()
    Me.TextBox1.Text = BuildString("CheckBoxA")
    End Sub
    Function BuildString(ByRef strGroupName As String) As String
    Dim strText As String
    Dim arrText() As String
    Dim i As Long
    Dim oCtr As Control
    For Each oCtr In Me.Controls
    If TypeName(oCtr) = "CheckBox" Then
    If InStr(oCtr.Name, strGroupName) > 0 Then
    If Me.Controls(oCtr.Name).Value = True Then
    strText = strText & oCtr.Caption & "|"
    End If
    End If
    End If
    Next
    'Clip final delimiter
    If Len(strText) > 0 Then
    strText = Left(strText, Len(strText) - 1)
    arrText = Split(strText, "|")
    strText = ""
    Select Case UBound(arrText)
    Case 0
    strText = arrText(0)
    Case 1
    strText = arrText(0) & " and " & arrText(1)
    Case Else
    strText = arrText(0)
    For i = 1 To UBound(arrText)
    If i < UBound(arrText) Then
    strText = strText & ", " & arrText(i)
    Else
    strText = strText & " and " & arrText(i)
    End If
    Next i
    End Select
    End If
    BuildString = strText
    End Function[/VBA]

    Good luck

    Quote Originally Posted by carlbowles10
    I have some tick boxes and a textbox. if a tick box is ticked then a message is put into the textbox. EG-

    if FlaggedSection3 is ticked then the textbox will add "Section 3" to it. An example - if I tick all three tick boxes (FlaggedSection1, FlaggedSection 2 and FlaggedSection3) then the textbox will say:

    Section 1 Section 2 Section 3.

    However, how do I do it so that if a tickbox is unchecked then the corrisponding message is taken out of the textbox. So if i untick FlaggedSection2 then the textbox will then read:

    Section 1 Section 3

    my current code for one of the tickboxes is:

    Private Sub FlagSection3_Click()
    'If Section 3 is flagged then display in the Flagged textbox at the bottom
    If FlagSection3.Value = True Then
    txtFlagged.Text = txtFlagged.Text + "Section 3 "
    End If
    End Sub



    Similar for the other two tickboxes,

    Thanks Carl.
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    I'd start with something like this. Not very clever, but simple

    [VBA]
    Option Explicit
    Private Sub CheckBox1_Click()
    Call UpdateTextBox
    End Sub
    Private Sub CheckBox2_Click()
    Call UpdateTextBox
    End Sub
    Private Sub CheckBox3_Click()
    Call UpdateTextBox
    End Sub

    Sub UpdateTextBox()
    TextBox1.Text = IIf(CheckBox1, "Section1", vbNullString) & " " & _
    IIf(CheckBox2, "Section2", vbNullString) & " " & _
    IIf(CheckBox3, "Section3", vbNullString)
    End Sub
    [/VBA]

    Paul

Posting Permissions

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