Form name for a Control

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

When passing a control to a function (ctl As Access.Control) is it possible
to find which form it came from?

Cheers,
Steve.
 
If you're talking about the currently active form, then use
Screen.ActiveForm

If not, then use the control's Parent property.

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia
 
Graham,

That works - sort of!
I have a 'main' form that has a Sub Form control on it. The function i am
using highlights a text box label when the mouse moves over the text box, on
the form within the sub form.

The function looks like this and is called from the Sub Form:

Public Function Change_Control_Colour(ctl As Access.Control, frm As
Access.Form, Optional strInitial_Colour As String, Optional
strHighlight_Colour As String)

Dim ctl2 As Access.Control

' If strInitial_Colour or strHightlight_Colour = "" then change them to
the 'standard' colours.
If strInitial_Colour = "" Then strInitial_Colour = 128
If strHighlight_Colour = "" Then strHighlight_Colour = 16711680

' This function changes the fore colour of the passed ctl
With ctl
' Change the fore colour of the control
If ctl.ForeColor <> Val(strHighlight_Colour) Then .ForeColor =
Val(strHighlight_Colour)
End With

' Reset the colour for the rest of the COL controls.
For Each ctl2 In frm.Controls

If Right(ctl2.Name, 3) = "COL" And ctl2.Name <> ctl.Name _
And ctl2.ForeColor <> Val(strInitial_Colour) Then

ctl2.ForeColor = Val(strInitial_Colour)

End If

Next ctl2

Set ctl = Nothing
Set ctl2 = Nothing
Set frm = Nothing

End Function

The part where the colour of the other controls is set back to the initial
colour (For Each ctl2 in frm.Controls) doesnt work. I am guessing this is
because I need to reference the 'main' form as well (i.e.
frmMain!subForm!Textbox) Can this be done?

Thanks for the help,
Steve.
 
Also pls note that if control on the tab control - then parent is tab, then
to get form you have to use parent.parent
etc
 
ctl.Parent.Parent.Controls works and covers my 2nd query.

The only point now is, I get an error '438 Object does not support this
property or method' when the Control is a button.

Is there anyway round this? (I am guessing I need to use just
ctl.Parent.Controls but cant work out how to change it)

Cheers,
Steve.
 
Steve,

I haven't exhaustively tested this, but it looks like it works OK.

1. Add the following to a standard module:
Private objCtrl As clsControl
Private objLastCtrl As Control

Public Sub ToggleCtrlColor(Optional ctrl As Control)
If ctrl Is Nothing Then
If objLastCtrl Is Nothing Then Exit Sub

If Not objCtrl Is Nothing Then objCtrl.ToggleCtrlColor
objLastCtrl
Else
If objCtrl Is Nothing Then
Set objCtrl = New clsControl

If ctrl Is Nothing Then Set ctrl = objLastCtrl
objCtrl.ToggleCtrlColor ctrl
Else
Set objCtrl = Nothing
Set objCtrl = New clsControl

If Not ctrl Is Nothing Then objCtrl.ToggleCtrlColor ctrl
End If
End If

Set objLastCtrl = ctrl
End Sub

2. Create the following class called "clsControl":
Private objCtrl As Control
Private Const HLCOLOR = 16711680
Private Const NORMCOLOR = 128

Public Sub ToggleCtrlColor(ctrl As Control)
If objCtrl Is Nothing Then
Set objCtrl = ctrl
objCtrl.ForeColor = HLCOLOR
Else
objCtrl.ForeColor = IIf(objCtrl.ForeColor = HLCOLOR, NORMCOLOR,
HLCOLOR)
End If
End Sub

Private Sub Class_Terminate()
On Error Resume Next

If Not objCtrl Is Nothing Then
objCtrl.ForeColor = NORMCOLOR
Set objCtrl = Nothing
End If
End Sub

3. Add the following code to each control's MouseMove event
ToggleCtrlColor Me.Controls("Text0")
'Make sure to change "Text0" to the name of the individual control.

4. Add the following code to the form's MouseMove event:
ToggleCtrlColor


Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia
---------------------------
 
Guys,

Thanks for the help.

Graham, your solution seems to work fine, I am not sure how it works, but it
does the job and will save a lot of repetitive code on my forms! (I was
checking/changing the forecolor of the control on each mousemove event and
the detail mousemove would contain forecolor checks for all the controls!)

Cheers,
Steve.
 
One thing I have noticed is when the mouse moves away from the control it
stays highlighted until another 'toggled' control has the mouse moved over it.

Its not really that important, more of an observation :)

Thanks again,
Steve.
 
Did you carry out Step 4?

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia
---------------------------
 
Yes. I have since moved the command to the forms Detail mousemove event and
that seems to work now.

Many thanks once again,
Steve.
 
Back
Top