[AccessD] need ideas

Heenan, Lambert Lambert.Heenan at AIG.com
Tue Feb 18 14:26:04 CST 2003


Sub: You don't need to pass a form name or object, just the OptionGroup is
passed, as an OptionGroup. The rest of the code is dependant on the Option
values that you define for the radio buttons in the OptionGroup. My quickly
thrown together example hard coded the "Yes" for the radio button label, but
that could be passed as a parameter too.

Basically there are three things the sub / event handler needs to work.

1/ a reference to the OptionGroup object
3/ the possible values of the OptionGroup - defined at design time by
yourself.
=OR= the possible values of the radio button labels
2/ The desired colors for the labels of OptionGroup values X and Y

If you go the Sub route then each OptionGroup's On_Click event would call
the Sub. 

Using the class route and WithEvents I would instatiate the class when my
form loads and release the class object when the form closes. You also need
a mechanism for telling the class which objects are bound to it. This is
very easily handled using a Collection object like this...

Sub BindDateBoxesToForm(f As Form, pcolDateBoxes As Collection)
Dim Ctl As Control

    For Each Ctl In f.Form.Controls
     Select Case Ctl.ControlType
      Case acTextBox:
        If Instr(Ctl.Tag,"DateBox") > 0 Then
          pcolDateBoxes.Add New clsDateBox
          Set pcolDateBoxes(pcolDateBoxes.Count).Control = Ctl ' this line
binds the specific control to the class
        End If
     End Select
    Next Ctl
End Sub

In the above example (which binds textboxes to a class that displays a
pop-up calendar) a Form object and a Collection object is passed to the
routine BindDateBoxesToForm. This loops though the form, finds the textboxes
that have "DateBox" in their tag property. For each of these it creates an
instance of the clsDateBox class which is then added to the Collection
object. This ensured that the class object stays in existence until you
destroy the Collection object.

Your class will need to contain these essential elements

1/ a declaration of the control type you are binding the class to,
WithEvents

e.g. 

	Private WithEvents mDateBox As TextBox

2/ a class terminate event like this...

	Private Sub Class_Terminate()
	   If Not IsDestroyed(mDateBox) Then
	     Set mDateBox = Nothing
	   Else
	    ' you cannot set control to nothing here - you get 2467 Error -
	    ' control is destroyed by MS Access but its reference isn't
nothing -
	    ' Set mDateBox = Nothing
	   End If
	End Sub

3/ The IsDestroyed method

	Private Function IsDestroyed(ByRef rctl As Control) As Boolean
	  IsDestroyed = True
	  On Error GoTo IsDestroyed_exit
	  IsDestroyed = Not (rctl.Name = rctl.Name)
	IsDestroyed_exit:
	  Err.Clear
	End Function

4/ the 'Control' property used when adding instances to the collection in
the Bind routine

	Public Property Set Control(ByVal ctlNewControl As TextBox)
	    Set mDateBox = ctlNewControl
	    With mDateBox
	        .OnGotFocus = "[Event Procedure]"
	        .OnKeyPress = "[Event Procedure]"
	        .OnDblClick = "[Event Procedure]"
	    End With
	End Property

This property is called in the Binding routine to actually bind the class to
your control. It also declares which events the class is going to implement.

5/ The actual event handler code, such as...

	Private Sub mDateBox_GotFocus()
	    mDateBox = dGetDate(mDateBox)
	    ' dGetDate is a function that displays a calendar control and
returns a date.
	End Sub

When it's time to release the class objects, because each instance created
was added to a Collection, the release is simply achieved by setting the
Collection object to Nothing. All the contained objects will be destroyed.

In your case, dealing with OptionGroups, you do have to be careful as the
values of the radio buttons are user definable, so keep this in mind when
writing your event handler(s).

HTH

Lambert

> -----Original Message-----
> From:	John Bartow [SMTP:jbartow at earthlink.net]
> Sent:	Tuesday, February 18, 2003 12:55 PM
> To:	accessd at databaseadvisors.com
> Subject:	RE: [AccessD] need ideas
> 
> OK in both cases I need to:
> 
> find the result based on the frame's value
> find the name of the radio button contained in the frame with that value
> find the name of the associated label contained by that radio button
> change the fore color of it as desired
> 
> So the difference between the two would be:
> Sub: I would have to call a sub for every control on every form and pass
> it
> the form and control name
> 
> WithEvents Class: I would instantiate the class once when the app starts
> and
> it would "keep tabs" on which form and control is being used so and I
> would
> never have to include any additional code
> 
> Is this correct?
> 
> JB
> 
> 
> -----Original Message-----
> From: accessd-admin at databaseadvisors.com
> [mailto:accessd-admin at databaseadvisors.com]On Behalf Of Heenan, Lambert
> Sent: Tuesday, February 18, 2003 11:23 AM
> To: 'accessd at databaseadvisors.com'
> Subject: RE: [AccessD] need ideas
> 
> 
> Whether you use a class with WithEvents, or just a simple Sub that you
> call
> in the option group's On_Click event, here's an example of the basic logic
> that you could use to do the color changing...
> 
> Sub formatOpGroup(opGrp As OptionGroup, sCaption As String)
> Dim c As Control
> 
>     For Each c In opGrp.Controls
>         If TypeOf c Is Label Then
>             If c.Caption = sCaption Then
>                 c.ForeColor = IIf(sCaption = "Yes", RGB(0, 255, 0),
> RGB(255,
> 0, 0))
>             Else
>                 c.ForeColor = RGB(0, 0, 0)
>             End If
>         End If
>     Next c
> End Sub
> 
> Lambert
> 
> > -----Original Message-----
> > From:	John Bartow [SMTP:jbartow at earthlink.net]
> > Sent:	Tuesday, February 18, 2003 11:40 AM
> > To:	AccessD
> > Subject:	[AccessD] need ideas
> >
> > I'm involved in an app where the color of the associated label for radio
> > buttons in a frame should change once its selected.
> >
> > The frames are all the same and have radio buttons with "Yes" and "No"
> as
> > labels. They would like the NO to be red when selected and YES to be
> green
> > when selected.
> >
> > I was thinking of trying a class but this seems like it would be a bit
> > more
> > complicated than the text box background color changing class that we've
> > discussed before.
> >
> > Any ideas or suggestions?
> >
> > JB
> >
> >
> > _______________________________________________
> > AccessD mailing list
> > AccessD at databaseadvisors.com
> > http://databaseadvisors.com/mailman/listinfo/accessd
> > Website: http://www.databaseadvisors.com
> _______________________________________________
> AccessD mailing list
> AccessD at databaseadvisors.com
> http://databaseadvisors.com/mailman/listinfo/accessd
> Website: http://www.databaseadvisors.com
> 
> 
> _______________________________________________
> AccessD mailing list
> AccessD at databaseadvisors.com
> http://databaseadvisors.com/mailman/listinfo/accessd
> Website: http://www.databaseadvisors.com



More information about the AccessD mailing list