Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access VBA
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 16th, 2003, 11:23 AM
Authorized User
 
Join Date: Aug 2003
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default changing the backcolor property of the textbox

Hi everyone,

does anyone know how to change the backcolor property of the textbox...so everytime users want to input information on the textbox...the backcolor change to other color instead of white...indicating the active textbox they have to fill in..

any help is greatly appreciated :)

hth

fehrer

 
Old December 16th, 2003, 12:17 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,151
Thanks: 2
Thanked 14 Times in 14 Posts
Send a message via ICQ to SerranoG Send a message via AIM to SerranoG
Default

On the textbox's On Got Focus Event, change its backcolor to the desired color (e.g. vbYellow) and then change the rest to the other (e.g. vbWhite) by cycling through the textbox controls on the form. This will give you the gist of it... I've not tried it.
Code:
   Dim ctlMyControl as Control

   'This loop will turn all backcolors to white.
   For Each ctlMyControl in Me
      If ctlMyControl.ControlType = acTextBox Then
         ctlMyControl.BackColor = vbWhite
      End If
   Next ctlMyControl

   'This will turn that one textbox backcolor to yellow.
   Me.txtMyTextBox.BackColor = vbYellow
If you put this code in each textbox's On Got Focus event, remember to change the last line so that you turn only the interested textbox yellow each time.



Greg Serrano
Michigan Dept. of Environmental Quality, Air Quality Division
 
Old December 16th, 2003, 08:32 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 308
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Greg,
Nice little bit of code you've got there - and yes, it does work correctly.
One thing I'd consider changing is instead of explicitly referring to the textbox, use the ActiveControl property, then just referring to the generic code - so you get something like:
Code:
Function ChangeClr()
Dim ctlMyControl as Control

   'This loop will turn all backcolors to white.
   For Each ctlMyControl in Me
      If ctlMyControl.ControlType = acTextBox Then
         ctlMyControl.BackColor = vbWhite
      End If
   Next ctlMyControl

   'This will turn that one textbox backcolor to yellow.
   Me.ActiveControl.BackColor = vbYellow
End Function
This way, rather than have Event Procedures for each textbox, you can just select them all, then put
Code:
=ChangeClr()
In the On Got Focus property, as an expression.
Works a treat

Steven

I am a loud man with a very large hat. This means I am in charge
 
Old December 16th, 2003, 08:59 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 308
Thanks: 0
Thanked 0 Times in 0 Posts
Default

And then there's another option of making it a public function that can be accessed by all forms, by using Screen rather than Me
Steven

I am a loud man with a very large hat. This means I am in charge
 
Old December 16th, 2003, 11:24 PM
Authorized User
 
Join Date: Jun 2003
Posts: 54
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Have you tried using [u]Conditional Formatting </u>from the user interface?

From the Design view, highlight the text box and select "Format\Conditional Formatting". When the dialog box comes up, go to the combo box marked "Condition 1" and select "Field in Focus". Go to the right and select your background color, bold/unbold, font color, etc. When you are done, click OK. Go to the form view and click from another control to your text box. If you did it correctly, the attributes will change when your text box has the focus.

Sure beats programming!

Hope this helps! :)


--- Tom
 
Old December 17th, 2003, 05:36 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 308
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Beats programming!?!?!?!
Blasphemy!!!

Although - you do have a point - and now that I've noticed it - generally I abhor most UI tools - but this one certainly does the trick
Steven

I am a loud man with a very large hat. This means I am in charge
 
Old December 17th, 2003, 07:53 AM
Authorized User
 
Join Date: Aug 2003
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi greg,

thanks for the help...very nice and neat coding...it works very well for the textbox and greatly appreciated....:)...

however it did not work for the combo box when users try to fill the form by selecting from the list..and it did not work either for textbox within the subform

steven & tom thanks to both of u as well...i'll try using the UI trick later for the combo box and subform

cheers,

fehrer



Quote:
quote:Originally posted by SerranoG
 On the textbox's On Got Focus Event, change its backcolor to the desired color (e.g. vbYellow) and then change the rest to the other (e.g. vbWhite) by cycling through the textbox controls on the form. This will give you the gist of it... I've not tried it.
Code:
   Dim ctlMyControl as Control

   'This loop will turn all backcolors to white.
   For Each ctlMyControl in Me
      If ctlMyControl.ControlType = acTextBox Then
         ctlMyControl.BackColor = vbWhite
      End If
   Next ctlMyControl

   'This will turn that one textbox backcolor to yellow.
   Me.txtMyTextBox.BackColor = vbYellow
If you put this code in each textbox's On Got Focus event, remember to change the last line so that you turn only the interested textbox yellow each time.



Greg Serrano
Michigan Dept. of Environmental Quality, Air Quality Division
 
Old December 18th, 2003, 10:13 AM
Authorized User
 
Join Date: Aug 2003
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks tom,

i've been able to change the backcolor property for textbox, combo box, and controls within the subform using the UI trick :)...it works properly

cheers,

fehrer

 
Old December 29th, 2003, 02:23 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,151
Thanks: 2
Thanked 14 Times in 14 Posts
Send a message via ICQ to SerranoG Send a message via AIM to SerranoG
Default

Quote:
quote:Originally posted by Steven
 Greg, Nice little bit of code you've got there - and yes, it does work correctly. One thing I'd consider changing is instead of explicitly referring to the textbox, use the ActiveControl property
 Hey, great addition. I keep forgetting about those properties that let you know where you are NOW, e.g. ActiveControl. There's a bunch of 'em in VBA.


Greg Serrano
Michigan Dept. of Environmental Quality, Air Quality Division
 
Old April 19th, 2016, 10:59 AM
Registered User
 
Join Date: Apr 2016
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default UI trick

I came across this problem but i do not know what and how to apply UI trick
Can anyone help me about this please i m using a combo box instead of a textbox





Similar Threads
Thread Thread Starter Forum Replies Last Post
change TextBox.BackColor property onFocus event drasko ASP.NET 1.0 and 1.1 Basics 8 January 26th, 2009 12:23 AM
Change Textbox Backcolor for certain records only camehere Access VBA 5 April 28th, 2008 06:28 AM
Changing the BackColor property of a Form Qman69 Visual Basic 2005 Basics 0 March 14th, 2007 04:43 PM
changing property doesn't work Vanni VB.NET 4 July 1st, 2005 07:50 AM
changing the onclick property dynamically? nerssi Javascript 8 September 21st, 2004 12:59 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.