Module for changing ForeColor on GotFocus (1 Viewer)

Maccus

New member
Local time
Today, 15:54
Joined
Aug 24, 2017
Messages
9
I have a form with around 50 text/combo-boxes that (apart from one field) have the Font ForeColor set to 11250603 Grey on load using VBA

I want each cell, on selection (or GotFocus), to change the ForeColor to vbBlack. I have done it the field by field but that is repetitive and clumsy. I would like to use a succinct piece of code perhaps at Form level and/or in a module to do the same task but haven't found anything that suits Access.

The closest I have come to identifying a possible route is by using DirectCast() but I am unfamiliar with it and its parameters.

If someone could point me to a piece of code or YouTube video or even a text that I could adapt, it would be much appreciated.
 

Ranman256

Well-known member
Local time
Today, 10:54
Joined
Apr 9, 2015
Messages
4,337
Direct wont work here, but here's some code to cycle thru the form and write the event code for you.
Open the file it creates and paste the code into your form.

It sets the color when the text box get the focus, then sets it when the user leaves.
YOU set the color constants, kYELO, or kGRAY (name them as you wish)

Code:
Public Sub MakeFocusCode()
Dim ctl, vFile

vFile = "c:\temp\vbCode.txt"
Open vFile For Output As #1
For Each ctl In Controls
  If TypeName(ctl) = "Textbox" Then
    Print #1, "Private Sub " & ctl.Name & "_GotFocus()"
    Print #1, ctl.Name & ".BackColor = kYELO"
    Print #1, "End Sub"
    Print #1, ""
    Print #1, "Private Sub " & ctl.Name & "_LostFocus()"
    Print #1, "txtFirstN.BackColor = kGRAY"
    Print #1, "End Sub"
  End If
Next
Close 1
End Sub
 

missinglinq

AWF VIP
Local time
Today, 10:54
Joined
Jun 20, 2003
Messages
6,423
You don't need a function, or code at all! This is really simple, with Conditional Formatting. In Form Design View

  1. Hold down <Shift> and Left Click on each Control in turn.
  2. In pre-2007, on the Menu go to Format - Conditional Format
  3. In 2007 and later, on the Ribbon, click on the Design Tab, then Click on the Conditional Icon
  4. Under Condition1 use the Down Arrow to select Field Has Focus
  5. Click on the Paint Bucket Icon.
  6. Select the desired Back Color.
  7. Click OK
You're done!

Linq ;0)>
 

Maccus

New member
Local time
Today, 15:54
Joined
Aug 24, 2017
Messages
9
I shall definitely give that a try. Many thanks.

Update: brilliant! Works for me. Thanks again. :D

Update - the Sequel: the conditional formatting works great but resets to the original colour on LostFocus. I may use that for BackColor rather than code it. For the ForeColor, I need something that leaves the field changed permanently.
 
Last edited:

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 22:54
Joined
May 7, 2009
Messages
19,230
this is my own version.
not too much elaborated though.
you only need 4 lines of code.
and its very simple.

copy the two classes to your db.

for each form that you have you need
to add this:

Private clsForm As clsFormTextBox
Private Sub Form_Load()
Set clsForm = New clsFormTextBox
clsForm.init Me, vbYellow 'the color of textbox when got focus
End Sub

Private Sub Form_Unload(Cancel As Integer)
' remove the instance of the class
Set clsForm=Nothing
End Sub
 

Attachments

  • sampleTextbox.zip
    54.5 KB · Views: 73

Maccus

New member
Local time
Today, 15:54
Joined
Aug 24, 2017
Messages
9
Many thanks for your code. I'll look into that too. :)
 

Users who are viewing this thread

Top Bottom