Easier way to change colour of text when text box is active?

Thanks so much for everyones input on this. Glad we got to a solution that will make my database easier going forwards and hopefully help others in the future.
 
Hi Ridders - Am using your module and all working fine except for one thing.

WHen i open the form the textbox1 with has setfocus automatically on it by default as it is tab stop 0 is not populating in colour with a red label. If i tab out of it and back to it it works as it should.

Is there anything that can be tweaked in the code or alternatively in the form setup on activation to make the module trigger?
 
If you look at mine, I set the focus to the form close event at start up using the form activate event code. I did so for precisely that reason.

If Gasman's solution doesn't have that issue, i'd use his method instead
 
Sorry to be a never ending pain but just wondering if you could explain the .tag line in gasmans code to me in plain terms so i can understand what it is achieving

Code:
      If (.OnGotFocus = vbNullString) And (.OnLostFocus = vbNullString) Then
                    .OnGotFocus = "=Hilight([" & .Name & "], True)"
                    .OnLostFocus = "=Hilight([" & .Name & "], False)"
                    .Tag = .Tag & IIf(.Tag <> vbNullString, mstrcTagSeparator, Null) & _
                        mstrcTagBackColor & mstrcTagAssignmnent & .BackColor
                End If

Many thanks
 
Also gasman file is .mdb. Will that mean his code wont work if i copy it itno a .accdb module?
 
Its part of Allen Browne's original code and I gave up trying to understand it.
Its in my version as well.

My original plan was to upload my own method of doing it which is easier to understand BUT each control has to be listed by name... so not generic.

EDIT Allen's original was mdb and I assume you used it successfully on your pc.
It should be ok
 
Fair enough, it can get confusing fast. On your code am i right in thinking you are not actually changing the background colour of the textbox but in fact highlighting the text box.

I only ask because on the textbox which starts with tabstop 0 i set backcolor to yellow thinking when i tabbed off it the code you wrote would switch it back to its original.

Or as i have set yellow to original is that why it isnt switching back to white?
 
From looking at your code i think it is the latter option above whereby it returns the textbox to the colour set in properties.
 
If you set the original to yellow that's what it will revert to.

The easiest way to test what's going on is to temporarily set the colour to something unusual and then see what triggers it
 
He is putting the backcolor selected into the tag property of the control along with anything else that is already there.

Something like 'UsualBackColor=13684991'
Then he reads that tag to see what he should set the highlight to.

He writes the code for the GetFocus and LostFocus events there as well. That is why you have to move existing code in those events (if you have any) to the Enter/Exit events, that is my understanding of it.

Very clever code. :D

Sorry to be a never ending pain but just wondering if you could explain the .tag line in gasmans code to me in plain terms so i can understand what it is achieving

Code:
      If (.OnGotFocus = vbNullString) And (.OnLostFocus = vbNullString) Then
                    .OnGotFocus = "=Hilight([" & .Name & "], True)"
                    .OnLostFocus = "=Hilight([" & .Name & "], False)"
                    .Tag = .Tag & IIf(.Tag <> vbNullString, mstrcTagSeparator, Null) & _
                        mstrcTagBackColor & mstrcTagAssignmnent & .BackColor
                End If
Many thanks
 
Colin

If the above interpretation is correct would it be possible to write and if statement that would say
if original backcolor is the same as backcolor change on gotfocus then
at off focus change the backcolor to white
else
change it back to oringal color?

REgards
 
Gasman

Your code throws a bug as below. Any ideas

Code:
Public Function Hilight(ctl As Access.Control, bOn As Boolean)
    'Purpose:   This code that gets called when focus moves into/out of a control.
    'Arguments: ctl = the control whose BackColor should be changed.
    '           bOn = flag: True if receiving focus, False if losing focus.
    ' Make label red when control has focus - E.P.Steel (03/10/17)
    Dim strBackColor As String
    
    If bOn Then
        'Assign the 'got focus' color.
        ctl.BackColor = mlngcFocusBackColor
   ''BUG''     ctl.Controls(0).ForeColor = vbRed
    Else
        'Restore the color from the control's Tag property (white if not found.)
        strBackColor = ReadFromTag(ctl, mstrcTagBackColor)
        If IsNumeric(strBackColor) Then
            ctl.BackColor = Val(strBackColor)
        Else
            ctl.BackColor = vbWhite
        End If
        ctl.Controls(0).ForeColor = vbBlack

    End If
End Function
 
Well no, you haven't eaxctly given a lot to go on. :(

I'm not an expert here, I just modified AB's code as I *thought* it was working and then tested on his form, and it worked.
As I mentioned it was 'my attempt'.

If I can help, I will, but I need a little more to go on with?
Name of control, type of control

Have you even tried to debug it?
In the example
the first ctl.name is Title and the ctl.controls(0).caption is Title: so they are linked in that way somehow.
 
OK so a generic form i set up works fine with your code so im guessing it may be something in my already established forms. I will have a play and report the error back once found in case someone else has similar issues
 
Found issue - My forms labels were not bound to the textbox control.

Thanks again.
 
Well done!:cool:
If it helps at all, AB's version does not need the labels named as controlname prefixed with 'lbl'
Found issue - My forms labels were not bound to the textbox control.

Thanks again.
 
Last edited:
Hi Guys. THought this appropriate to add to this thread but may add a new one if you think benifical. IN this thread we have managed to achieve changing textbox backcolors chaging as enter and leave a textbox and the respective label also changing color.

Is there a way of tweaking gasmans code so that when you also tab to a command button its chages colour like it does when you hover over it. Obv appreciate could code this for each cmd button but would love to do something like we achieved for the txtbox
 
Not by me.
I can see that a command button does not have those properties. I do not know what property would be used for the change.
Then you would have to check for the control type everywhere.

There is probably a good reason Allen Browne did not implement that feature for anything other than those he has taken into account.
 

Users who are viewing this thread

Back
Top Bottom