Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Bolding label when control has focus!

Status
Not open for further replies.

ind

Programmer
Mar 9, 2000
121
US
I have the following funtion:

Function Color()
On Error Resume Next
Dim ctl As Control

Set ctl = Me.ActiveControl
If ctl.BackColor = 16777215 Then
ctl.BackColor = 14211021
Else
ctl.BackColor = 16777215
End If
End Function

The event is fired on the GotFocus and LostFocus

How can I use this same function to bold the label of the control that has the focus.

 
Hi!

If you give the labels matching names, i.e. if the text box is called txtName call the label lblName, then you can use the following code:

Function Color()
On Error Resume Next
Dim ctl As Control
Dim strLabelName As String

Set ctl = Me.ActiveControl
If ctl.BackColor = 16777215 Then
ctl.BackColor = 14211021
strLabelName = "lbl" & Mid(ctl.Name, 4)
Me!Controls(strLabelName).FontWeight = acBold
Else
ctl.BackColor = 16777215
strLabelName = "lbl" & Mid(ctl.Name, 4)
Me!Controls(strLabelName).FontWeight = acNormal
End If
End Function

You will need to make sure that I am turning it bold in the correct part of the If statement. You will also want to check out the constants because I may have missed something there.

hth
Jeff Bridgham
bridgham@purdue.edu
 
ind,
I posted a chunk of code here about 2 weeks ago, and forgot which forum it was in and search function here doesn't work so well.

But here is the pseudo-code:
Run this code once with your form in design view--(this is generating events for your form)

For Each C in Forms!FormName.Controls
If TypeOf C is TextBox Then 'Or whatever other types you have getting focus
c.OnGotFocus = "=Color()"
End if
Next
Now all your controls call the color function on got focus. You can change the code to just force to the color you want, without that If...Backcolor...etc.
You should also do similar for LostFocus, making a function UnColor() or something.
--Jim
 
ind,
Sorry, I didn't read closely...in the code above I posted, I thought you were looking for how to make *all* controls use that color function...
--Jim
 
Tried it and it didn't work...
Confused, because it makes sense.
Any more suggestions????
 
Hi!

I looked through Access help and apparently there is no constants involved here. Just say = Bold or = Normal. Alternatively to use = 700 for bold(800 and 900 are also acceptable) and = 400 for normal. I guessing that you can use integers between these values too.

hth Jeff Bridgham
bridgham@purdue.edu
 
when i was messing around with this--i had to change the label to a text box for it to change to bold.

i used the 400/700 thing for normal/bold
problem is that when i moved to 'lable' to change it to bold, of course then your text box looses focus, and turns back to white. you dont even see it turn grey it's so fast. so i tried calling function in OnEnter and OnExit, but same thing. and for sure dont make the last line setfocus back to the text box control, it's an endless loop.

so, here's my code, tho you'll still have to work on the focus issue. i found like i said that you can't make a LABEL bold, you have to make it a text box, then controlsource is '="Date:" or whatever. i'm assuming you are naming the orig text boxes for example

txtTextBox

and it's associated 'label' is named i.e.

lblTextBox

this code will parse out the name of the control and figure out the name of the label, pass control to the 'label' and make it bold. again, you'll have to mess around with focus issue.

Code:
Dim ctl As Control
                         Dim ctlName
                         Dim LabelName
                         
                         Set ctl = Me.ActiveControl
                             If ctl.BackColor = 16777215 Then
                                 ctl.BackColor = 14211021
                             Else
                                 ctl.BackColor = 16777215
                         End If
                         
                         'Determine name of associated label and go to it
                         ctlName = Me.ActiveControl.Name
                         LabelName = "lbl" & Right(Me.ActiveControl.Name, Len(Me.ActiveControl.Name) - 3)
                         
                         DoCmd.GoToControl LabelName
                         
                         Dim ctlLabel As Control
                         Set ctlLabel = Me.ActiveControl
                         
                         If ctlLabel.FontWeight = 400 Then   'Normal
                            ctlLabel.FontWeight = 700        'Bold
                         Else
                            ctlLabel.FontWeight = 400        'Normal
                         End If

good luck--g
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top