John Colby
jcolby at colbyconsulting.com
Thu Oct 2 13:38:18 CDT 2003
In my framework I use the background color of a combo to denote that the
combo has dbl-click (open a data entry form) functionality.
In order to do this I use the following function to find any given control's
label:
'.Comments :
'.Parameters:
'.Sets :
'.Returns :
'.Created by: John W. Colby
'.Created : 6/17/02 11:22:19 AM
'
'Finds the label that "belongs to" any given control.
'
Function CtlLbl(ctlFindLbl As Control) As Label
On Error GoTo Err_CtlLbl
Dim ctl As Control
For Each ctl In ctlFindLbl.Controls
If ctl.ControlType = acLabel Then
Set CtlLbl = ctl
End If
Next ctl
Exit_CtlLbl:
Exit Function
Err_CtlLbl:
Select Case err
Case 0 '.insert Errors you wish to ignore here
Resume Next
Case Else '.All other errors will trap
Beep
MsgBox err.Description, , "Error in Function Utils.CtlLbl"
Resume Exit_CtlLbl
End Select
Resume 0 '.FOR TROUBLESHOOTING
End Function
My combo class has a private label variable. I use the function above to
find the label and set the class' label variable.
dim mlbl as label
Set mlbl = CtlLbl(mcbo)
Having done this, the class can now directly manipulate the properties of
the label associated with the combo such as back color, forecolor, font etc.
For example
if mcbo.text < 0 then
mlbl.backcolor = vbRed
else
mlbl.backcolor = vbGreen
endif
The problem is controls on a continuous form where the label is in the page
or form header don't have a label per se (in their controls collection), and
thus the function returns a null. IOW I can't manipulate a label for a
COLUMN of controls the same way I can for an independent label directly
attached to a control. And yes, I know the example above makes no sense for
a column of combos since different records could plus or negative. However
I want to be able to say a column of combos can be dbl-clicked to open a
data entry form, and in this context it does make sense.
So I added a function to the combo class to allow me to pass in a reference
to a label, which is then stored in the label variable in the class.
'
'Connects a label to a combo - used for continuous forms where the label is
in the header etc.
'
Function ConnectLabel(llbl As Label)
Set mlbl = llbl
End Function
I can now "assign" a label to a class instance using the syntax:
colCombos.Item("cboIDPT").ConnectLabel lblIDPT
Therefore any code in the class that attempts to manipulate the label of the
control (combo in this case) "finds" a label to manipulate even though there
isn't one in the control.controls collection.
And IT WORKS!
John W. Colby
www.colbyconsulting.com