Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access VBA
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old December 16th, 2003, 10:23 AM
Authorized User
 
Join Date: Aug 2003
Location: , , Australia.
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

Reply With Quote
  #2 (permalink)  
Old December 16th, 2003, 11:17 AM
Friend of Wrox
Points: 3,947, Level: 26
Points: 3,947, Level: 26 Points: 3,947, Level: 26 Points: 3,947, Level: 26
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Lansing, Michigan, USA.
Posts: 1,150
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
Reply With Quote
  #3 (permalink)  
Old December 16th, 2003, 07:32 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Melbourne, Vic, Australia.
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
Reply With Quote
  #4 (permalink)  
Old December 16th, 2003, 07:59 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Melbourne, Vic, Australia.
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
Reply With Quote
  #5 (permalink)  
Old December 16th, 2003, 10:24 PM
Authorized User
Points: 306, Level: 6
Points: 306, Level: 6 Points: 306, Level: 6 Points: 306, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: , , .
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
Reply With Quote
  #6 (permalink)  
Old December 17th, 2003, 04:36 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Melbourne, Vic, Australia.
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
Reply With Quote
  #7 (permalink)  
Old December 17th, 2003, 06:53 AM
Authorized User
 
Join Date: Aug 2003
Location: , , Australia.
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
Reply With Quote
  #8 (permalink)  
Old December 18th, 2003, 09:13 AM
Authorized User
 
Join Date: Aug 2003
Location: , , Australia.
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

Reply With Quote
  #9 (permalink)  
Old December 29th, 2003, 01:23 PM
Friend of Wrox
Points: 3,947, Level: 26
Points: 3,947, Level: 26 Points: 3,947, Level: 26 Points: 3,947, Level: 26
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Lansing, Michigan, USA.
Posts: 1,150
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
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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 25th, 2009 11:23 PM
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



All times are GMT -4. The time now is 12:15 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.