add/remove text to a text box

moishy

Registered User.
Local time
Today, 16:05
Joined
Dec 14, 2009
Messages
264
I'm looking for a way to add/remove text to a text box in the after update event of a check box based on its value.
 
Sure.

These are the check boxes:
chkRed, chkBlue, chkGreen, chkYellow, chkBlack, chkPurple

This is the text box:
txtColor

What I want:
Checking each check box will add the corresponding color to the text box.

How I think to accomplish the above:
In the after update event of each check box have something like this:
Code:
Private Sub chkRed_AfterUpdate()
    If Me.chkRed = True Then
        If InStr(txtColor, "Red") = False Then
            txtColor= txtColor & "Red, "
        End If
    ElseIf Me.chkRed = False Then
        If InStr(txtColor, "Red") = True Then
           ' somehow remove the word "red" from txtColor
        End If
    End If
End Sub
 
As you see I'm using InStr. I can't use Left because txtColor can hold more than one color, and there is no telling what order they will be checked and/or unchecked in.
 
This is what I tried but nothing happens, the text is not added to the text box, and neither is it removed (if manually typed in):

Code:
Private Sub chkRed_AfterUpdate()
    If Me.chkRed = True Then
        If InStr(txtColor, "Red") = False Then
            txtColor = txtColor & "Red, "
        End If
    ElseIf Me.chkRed = False Then
        If InStr(txtColor, "Red, ") = True Then
            txtColor = txtColor & Replace(txtColor, "Red, ", "")
        End If
    End If
End Sub
Note: The check boxes and the text box are located on a tab control.
 
You should be replacing the entire txtColor not concatenating.
 
I don't understand.

The text box may read: Green, Blue, Red, Yellow, Black, Purple
What should I replace the above with, I only want to remove "Red, "?
 
Think about it and have a look at your line of code. txtColor may contain that entire string, so why are you concatenating the old value to the replaced value?
 
Oops...

Won't Replace() replace the entire string?
 
Nope! Did you read the help files about what Replace() does?

E.g.:
Code:
[COLOR=red]Replace([/COLOR]"Green, Blue, Red, Yellow, Black, Purple"[COLOR=red], [/COLOR]"Red, "[COLOR=red],[/COLOR] ""[COLOR=red])[/COLOR]
For brevity the red bits is the Replace function. So what that will return is:

"Green, Blue, Yellow, Black, Purple"

You follow now?
 
Double oops...

Crystal clear. Thanks for your patience and help.

So using the above code with your amendment, why doesn't anything happen when the check boxes are updated?
 
Put a breakpoint, step through your code and hover over to see the values.
 
I've discovered that it will only add the text if the text box is not null, any idea why, and how to get around it?

And the removing text part skips the Replace line, to get around that I changed it to this:
Code:
        If InStr(txtColor, "Red") = True Then
            ' do nothing
        Else
            txtColor = Replace(txtColor, "Red, ", "")
        End If
Now it works, but the logic eludes me.

I guess today is just not my day...
 
It's always a good idea to read the help files about functions moishy. Instr() function doesn't return True or False, it returns the starting position where the text was found. And there's more about the Instr() function you need to read about pertaining Nulls.
 
So my use of InStr is wrong. What is the proper way?
 
Ok it must be simple but I still (after spending more time than I would have liked to) can't figure it out.

Can I please have another hint?
 
I've given you a couple of hints already.

* I mentioned that it returns the starting position of the string you're finding if it finds the string.
* If the string you're searching for (i.e. the String1 parameter) is Null it will return Null.
* If the string you're searhing against (i.e. the String1 parameter) is Null it will return Null.

Instr(1, "Red, Green", "Green") will return 6 because that's the starting position of the word Green.
 

Users who are viewing this thread

Back
Top Bottom