Changing Textbox Colors

Rupert Bennett

Active Member
Joined
Nov 20, 2002
Messages
276
Hello Everyone,
I have what I hope is a simple question. It is always tough when you don't know how.

I would like to change the textbox Backcolor when they have focus and when they have lost focus. I have something that works now, but it is a tedious exercise when you need to make changes and you have many textboxes.
I have in each textbox GotFocus Event e.g. Text1.BackColor = RGB(117,225,257)
and iin the LostFocus Event Text1.BackColor = RGB(225,225,0)

Can you give me some code , Global or at the Form level, that will do this for each textbox on the form? This would be a lot more effecient.
Thanks for helping.
Rupert
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
I believe there are only 2 approaches - what you're doing, and writing a class (special code) that will recognize the events on a global scale. Classes are something that I never got good at. If you need to search for code, use keywords such as "vba class for gotfocus" (or got focus); "vba withevents for textbox" or see if you can make this work VBA: Using WithEvents on UserForms
I realize that's about a userform (Excel) so you'd have to watch out for Excel references and edit to suit Access.

You can slightly simplify your current approach by passing the control name to one sub that does the colouring but you still need the got/lost focus events for each textbox. Assuming that by "form" you mean a userform then sort of like this:
VBA Code:
Private Sub txtBox1._GotFocus()
ColourTextbox txtBox1.Name, 255
End Sub
Private Sub txtBox1._LostFocus()
ColourTextbox txtBox1.Name, 16777215
End Sub

Sub ColourTextbox(strName As String, lngNum As Long)
Me.Controls(strName).BackColor = lngNum
End Sub
If you know how to use the immediate window, then you can get the Long number value for any control by doing that in design and then inquiring in the immediate window:
?Forms!myFormNameHere.txtBoxNameHere.backcolor
and hit Enter key.
Uses your own control names of course. I suppose you could eliminate all of the lost focus events just by making the called sub loop through the textboxes and set them all to 'normal' then the specific one to red. You might get some screen flicker though.
 
Upvote 0
I am glad I was thinking along the same lines. :)

I think the class option would be the easiest to implement, though I have never set one up myself.
 
Upvote 0
This should give you a head start, though he is using an unbound form, but the logic is the same I believe.
 
Upvote 0
I am truly grateful for all the replies and help given. However I am unable to make this work satisfactorily.

I am sure this is all due to my own ignorance. I will continue with my old method as I am pressed to meet a deadline.

Thanks
 
Upvote 0
You are/were unable to use what I suggested? Given that the weekend is upon us, I imagine your deadline isn't Monday, so if you post a link to your project on a file share somewhere, I could try to do what I can over the weekend. If you do that, you would have to include info in a post that would enable anyone to open the correct form, etc. In fact, your posted db need not contain any more than this one form and any query/table that depends on it. Up to you, but no promises. I might leave this mortal coil at any moment. ;)
 
Upvote 0
I am truly grateful for all the replies and help given. However I am unable to make this work satisfactorily.

I am sure this is all due to my own ignorance. I will continue with my old method as I am pressed to meet a deadline.

Thanks
If you watch the video, Adolph adds a new textbox control and it behaves the same as the others. So I believe all you need to do is create a clsTextBox and define the GotFocus and LostFocus events.
The define your controlds as a clsTextbox. Then if you suddennly need to do soemthing else with them, you amend the class.

If not now, get your deadline met and then perhaps review it afterwards.
When I watched the video I was sad I did not know about this when I was creating my forms, as it could have made the work a lot easier.
He even creates the code in that video.
 
Upvote 0
Perhaps look here later. MajP is a good person to study classes from.
Bit of a steep learning curve I admit, but then easy to maintain.
 
Upvote 0
This one is better starting at 29 minutes.

Pretty much what you want to do.
 
Upvote 0
I have a found a DB by A.P.R. Pillai which addresses your issue, and just that.
Seems I am unable to upload files here here?, but if you want to PM me with your email address, I will email it to you.
That will at least give you a head start.

Basically you have to set all your textbox controls (or just those you want to the new textbox class, so that the LostFocus and GotFocus events can be common.

All very neat really, but it does require a fair bit of thinking. I get lost with all the other stuff that accompanies the class code, but this DB focuses just on textboxes.
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,263
Members
452,627
Latest member
KitkatToby

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top