re-use of code in a module ...

  • Thread starter Thread starter Joe Best
  • Start date Start date
J

Joe Best

I have 6 forms with a label on each called lblSaveDetails.

When the user hovers over the label, it's colour changes from white to
yellow. I have placed the code for this to happen within each form as a sub
procedure.

What I would like to do have this code appear only once in a module. Do i
need to create a public function to do this. Any ideas please?

Many thanks,

Joe.
 
Yes - put the procedure into a standard module - you can pass the control
object to the procedure so that the procedure can remain ignorant about
where the control actually exists.

public sub HoverLabel(ctlIn as control)
'your code goes here and instead of using syntax
'like me!MyLabel you refer directly to ctlIn
'for example:
ctlin.forecolor=vbyellow
End sub
 
Thanks for the reply.

What I failed to mention was that I don't just have one label that I want to
change back to a different colour, I actually need to change all labels.

I currently have a rectangle box (shpSide) on my form with two labels
(lblSaveDetails & lblGoToNCRBrowser) on top of that. When I mouse move over
a label it changes from white to yellow and then when the mouse passes over
the shpSide control the fore colour of the label returns to white.

As I mentioned, this is the same for all six forms. Hope I have explained
this properly.

Current code is:

Private Sub lblSaveDetails_MouseMove(Button As Integer, Shift As Integer, X
As Single, Y As Single)
Me.lblSaveDetails.ForeColor = vbYellow
End Sub


Private Sub lblGoToNCRBrowser_MouseMove(Button As Integer, Shift As Integer,
X As Single, Y As Single)
Me.lblGoToNCRBrowser.ForeColor = vbYellow
End Sub

Private Sub shpSide_MouseMove(Button As Integer, Shift As Integer, X As
Single, Y As Single)
Me.lblSaveDetails.ForeColor = vbWhite
Me.lblGoToNCRBrowser.ForeColor = vbWhite
End Sub

How would I implement this in a module?

Joe.


Sandra Daigle said:
Yes - put the procedure into a standard module - you can pass the control
object to the procedure so that the procedure can remain ignorant about
where the control actually exists.

public sub HoverLabel(ctlIn as control)
'your code goes here and instead of using syntax
'like me!MyLabel you refer directly to ctlIn
'for example:
ctlin.forecolor=vbyellow
End sub


--
Sandra Daigle
[Microsoft Access MVP]
For the benefit of others please post all replies to this newsgroup.

Joe said:
I have 6 forms with a label on each called lblSaveDetails.

When the user hovers over the label, it's colour changes from white to
yellow. I have placed the code for this to happen within each form as a
sub procedure.

What I would like to do have this code appear only once in a module. Do i
need to create a public function to do this. Any ideas please?

Many thanks,

Joe.
 
Hi Joe,

Here is an article that describes how to do this - to use, you have to put a
function call into the OnMouseMove property (in the property sheet) of each
control. The function call passes the name of the control to the function.

http://www.mvps.org/access/forms/frm0037.htm

There is a way to automate this using a class module and WithEvents. It's a
bit more extensible and easier to implement on a form with many controls. I
have some sample code at my office - I'll put it together for you if you are
interested.

--
Sandra Daigle
[Microsoft Access MVP]
For the benefit of others please post all replies to this newsgroup.

Joe said:
Thanks for the reply.

What I failed to mention was that I don't just have one label that I want
to change back to a different colour, I actually need to change all
labels.

I currently have a rectangle box (shpSide) on my form with two labels
(lblSaveDetails & lblGoToNCRBrowser) on top of that. When I mouse move
over a label it changes from white to yellow and then when the mouse
passes over the shpSide control the fore colour of the label returns to
white.

As I mentioned, this is the same for all six forms. Hope I have explained
this properly.

Current code is:

Private Sub lblSaveDetails_MouseMove(Button As Integer, Shift As Integer,
X As Single, Y As Single)
Me.lblSaveDetails.ForeColor = vbYellow
End Sub


Private Sub lblGoToNCRBrowser_MouseMove(Button As Integer, Shift As
Integer, X As Single, Y As Single)
Me.lblGoToNCRBrowser.ForeColor = vbYellow
End Sub

Private Sub shpSide_MouseMove(Button As Integer, Shift As Integer, X As
Single, Y As Single)
Me.lblSaveDetails.ForeColor = vbWhite
Me.lblGoToNCRBrowser.ForeColor = vbWhite
End Sub

How would I implement this in a module?

Joe.


Sandra Daigle said:
Yes - put the procedure into a standard module - you can pass the control
object to the procedure so that the procedure can remain ignorant about
where the control actually exists.

public sub HoverLabel(ctlIn as control)
'your code goes here and instead of using syntax
'like me!MyLabel you refer directly to ctlIn
'for example:
ctlin.forecolor=vbyellow
End sub


--
Sandra Daigle
[Microsoft Access MVP]
For the benefit of others please post all replies to this newsgroup.

Joe said:
I have 6 forms with a label on each called lblSaveDetails.

When the user hovers over the label, it's colour changes from white to
yellow. I have placed the code for this to happen within each form as a
sub procedure.

What I would like to do have this code appear only once in a module. Do
i need to create a public function to do this. Any ideas please?

Many thanks,

Joe.
 
Many thanks for that Sandra.

Yes I would be very grateful if you could dig out the code for me if
possible.

Once again, many thanx.

Joe.


Sandra Daigle said:
Hi Joe,

Here is an article that describes how to do this - to use, you have to put a
function call into the OnMouseMove property (in the property sheet) of each
control. The function call passes the name of the control to the function.

http://www.mvps.org/access/forms/frm0037.htm

There is a way to automate this using a class module and WithEvents. It's a
bit more extensible and easier to implement on a form with many controls. I
have some sample code at my office - I'll put it together for you if you are
interested.

--
Sandra Daigle
[Microsoft Access MVP]
For the benefit of others please post all replies to this newsgroup.

Joe said:
Thanks for the reply.

What I failed to mention was that I don't just have one label that I want
to change back to a different colour, I actually need to change all
labels.

I currently have a rectangle box (shpSide) on my form with two labels
(lblSaveDetails & lblGoToNCRBrowser) on top of that. When I mouse move
over a label it changes from white to yellow and then when the mouse
passes over the shpSide control the fore colour of the label returns to
white.

As I mentioned, this is the same for all six forms. Hope I have explained
this properly.

Current code is:

Private Sub lblSaveDetails_MouseMove(Button As Integer, Shift As Integer,
X As Single, Y As Single)
Me.lblSaveDetails.ForeColor = vbYellow
End Sub


Private Sub lblGoToNCRBrowser_MouseMove(Button As Integer, Shift As
Integer, X As Single, Y As Single)
Me.lblGoToNCRBrowser.ForeColor = vbYellow
End Sub

Private Sub shpSide_MouseMove(Button As Integer, Shift As Integer, X As
Single, Y As Single)
Me.lblSaveDetails.ForeColor = vbWhite
Me.lblGoToNCRBrowser.ForeColor = vbWhite
End Sub

How would I implement this in a module?

Joe.


Sandra Daigle said:
Yes - put the procedure into a standard module - you can pass the control
object to the procedure so that the procedure can remain ignorant about
where the control actually exists.

public sub HoverLabel(ctlIn as control)
'your code goes here and instead of using syntax
'like me!MyLabel you refer directly to ctlIn
'for example:
ctlin.forecolor=vbyellow
End sub


--
Sandra Daigle
[Microsoft Access MVP]
For the benefit of others please post all replies to this newsgroup.

Joe Best wrote:
I have 6 forms with a label on each called lblSaveDetails.

When the user hovers over the label, it's colour changes from white to
yellow. I have placed the code for this to happen within each form as a
sub procedure.

What I would like to do have this code appear only once in a module. Do
i need to create a public function to do this. Any ideas please?

Many thanks,

Joe.
 
No problem, here is some *extremely* rough code which will give you the gist
of how to do this. This does work but it is not error proof - I've liberally
used "On error resume Next" just to expedite getting this to you. A better
solution would be checking for specific conditions and handling them
accordingly.

First thing you need to know is that a label which is attached to a control
does not have event procedure properties - this simply means that you have
to use the event procedure of the attached control. Keep in mind that if you
have textbox1 which has an attached label, that label can be referenced as
me.textbox1.controls(0) - if the control does not have an attached label,
the reference will fail. (this was one error condition I could have
explicitly tested however I don't care if it doesn't have a label.

The way this works is that each control that needs to be highlighted has the
word "Hover" in its tag property. The form's open event will build a
collection of controls that need to have this hover capability. Each control
will be used to create a HoverCtl object. The HoverCtl object determines
what kind of control it is and then using WithEvents, the appropriate event
properties are created. Notice that this sample only handles textboxes and
labels - you would need to expand the Select Case to handle other types of
controls.

It looks like a lot of code but once you have it done, it's easy to add the
functionality to a new control (or a form full of controls). Give it a look
and if you have questions post back:

*** Code in form
Option Compare Database
Dim mcolControls As Collection
Private Const mclngNormalColor = vbBlack
Public mLastCtl As Control
Private Sub Detail_MouseMove(Button As Integer, Shift As Integer, X As
Single, Y As Single)
'mLastCtl is set by the class to let us know which control to change
back
'reset the forecolor of any highlighted control to the normal color
On Error Resume Next
If Not mLastCtl Is Nothing Then
mLastCtl.ForeColor = mclngNormalColor
mLastCtl.Controls(0).ForeColor = mclngNormalColor
End If
End Sub

Private Sub Form_Open(Cancel As Integer)
Dim ctl As Control
Dim objHover As clsHoverCtl
Set mcolControls = New Collection
For Each ctl In Me.Controls
If ctl.Tag = "Hover" Then
Set objHover = New clsHoverCtl
Set objHover.ctl = ctl
mcolControls.Add objHover, ctl.Name
End If
Next ctl
End Sub

Private Sub Text0_MouseMove(Button As Integer, Shift As Integer, X As
Single, Y As Single)
Me.Text0.ForeColor = vbRed
Me.Text0.Controls(0).ForeColor = vbRed
End Sub


*Code in Class module which is named clsHoverCtl

Private Const mcStrEventProc As String = "[Event Procedure]"
Public Property Set ctl(ctl As Control)
On Error Resume Next
Set mctl = ctl
Select Case mctl.ControlType
Case acTextBox
Set mctlTxt = mctl
mctlTxt.OnMouseMove = mcStrEventProc
Case acLabel
Set mctlLabel = mctl
mctlLabel.OnMouseMove = mcStrEventProc
End Select
End Property
Public Property Get ctl()
Set ctl = mctl
End Property

Private Sub mctlLabel_MouseMove(Button As Integer, Shift As Integer, X As
Single, Y As Single)
mctlLabel.ForeColor = vbRed
Set mctlLabel.Parent.mLastCtl = mctlLabel
End Sub


Private Sub mctlTxt_MouseMove(Button As Integer, Shift As Integer, X As
Single, Y As Single)
mctlTxt.ForeColor = vbRed
Set mctlTxt.Parent.mLastCtl = mctlTxt
On Error Resume Next
mctlTxt.Controls(0).ForeColor = vbRed
End Sub



--
Sandra Daigle [Microsoft Access MVP]
Please post all replies to the newsgroup.


Joe said:
Many thanks for that Sandra.

Yes I would be very grateful if you could dig out the code for me if
possible.

Once again, many thanx.

Joe.


Sandra Daigle said:
Hi Joe,

Here is an article that describes how to do this - to use, you have
to put a function call into the OnMouseMove property (in the
property sheet) of each control. The function call passes the name
of the control to the function.

http://www.mvps.org/access/forms/frm0037.htm

There is a way to automate this using a class module and WithEvents.
It's a bit more extensible and easier to implement on a form with
many controls. I have some sample code at my office - I'll put it
together for you if you are interested.

--
Sandra Daigle
[Microsoft Access MVP]
For the benefit of others please post all replies to this newsgroup.

Joe said:
Thanks for the reply.

What I failed to mention was that I don't just have one label that
I want to change back to a different colour, I actually need to
change all labels.

I currently have a rectangle box (shpSide) on my form with two
labels (lblSaveDetails & lblGoToNCRBrowser) on top of that. When I
mouse move over a label it changes from white to yellow and then
when the mouse passes over the shpSide control the fore colour of
the label returns to white.

As I mentioned, this is the same for all six forms. Hope I have
explained this properly.

Current code is:

Private Sub lblSaveDetails_MouseMove(Button As Integer, Shift As
Integer, X As Single, Y As Single)
Me.lblSaveDetails.ForeColor = vbYellow
End Sub


Private Sub lblGoToNCRBrowser_MouseMove(Button As Integer, Shift As
Integer, X As Single, Y As Single)
Me.lblGoToNCRBrowser.ForeColor = vbYellow
End Sub

Private Sub shpSide_MouseMove(Button As Integer, Shift As Integer,
X As Single, Y As Single)
Me.lblSaveDetails.ForeColor = vbWhite
Me.lblGoToNCRBrowser.ForeColor = vbWhite
End Sub

How would I implement this in a module?

Joe.


Yes - put the procedure into a standard module - you can pass the
control object to the procedure so that the procedure can remain
ignorant about where the control actually exists.

public sub HoverLabel(ctlIn as control)
'your code goes here and instead of using syntax
'like me!MyLabel you refer directly to ctlIn
'for example:
ctlin.forecolor=vbyellow
End sub


--
Sandra Daigle
[Microsoft Access MVP]
For the benefit of others please post all replies to this
newsgroup.

Joe Best wrote:
I have 6 forms with a label on each called lblSaveDetails.

When the user hovers over the label, it's colour changes from
white to yellow. I have placed the code for this to happen within
each form as a sub procedure.

What I would like to do have this code appear only once in a
module. Do i need to create a public function to do this. Any
ideas please?

Many thanks,

Joe.
 
Very much appreciated. Many thanks for that Sandra.


Sandra Daigle said:
No problem, here is some *extremely* rough code which will give you the gist
of how to do this. This does work but it is not error proof - I've liberally
used "On error resume Next" just to expedite getting this to you. A better
solution would be checking for specific conditions and handling them
accordingly.

First thing you need to know is that a label which is attached to a control
does not have event procedure properties - this simply means that you have
to use the event procedure of the attached control. Keep in mind that if you
have textbox1 which has an attached label, that label can be referenced as
me.textbox1.controls(0) - if the control does not have an attached label,
the reference will fail. (this was one error condition I could have
explicitly tested however I don't care if it doesn't have a label.

The way this works is that each control that needs to be highlighted has the
word "Hover" in its tag property. The form's open event will build a
collection of controls that need to have this hover capability. Each control
will be used to create a HoverCtl object. The HoverCtl object determines
what kind of control it is and then using WithEvents, the appropriate event
properties are created. Notice that this sample only handles textboxes and
labels - you would need to expand the Select Case to handle other types of
controls.

It looks like a lot of code but once you have it done, it's easy to add the
functionality to a new control (or a form full of controls). Give it a look
and if you have questions post back:

*** Code in form
Option Compare Database
Dim mcolControls As Collection
Private Const mclngNormalColor = vbBlack
Public mLastCtl As Control
Private Sub Detail_MouseMove(Button As Integer, Shift As Integer, X As
Single, Y As Single)
'mLastCtl is set by the class to let us know which control to change
back
'reset the forecolor of any highlighted control to the normal color
On Error Resume Next
If Not mLastCtl Is Nothing Then
mLastCtl.ForeColor = mclngNormalColor
mLastCtl.Controls(0).ForeColor = mclngNormalColor
End If
End Sub

Private Sub Form_Open(Cancel As Integer)
Dim ctl As Control
Dim objHover As clsHoverCtl
Set mcolControls = New Collection
For Each ctl In Me.Controls
If ctl.Tag = "Hover" Then
Set objHover = New clsHoverCtl
Set objHover.ctl = ctl
mcolControls.Add objHover, ctl.Name
End If
Next ctl
End Sub

Private Sub Text0_MouseMove(Button As Integer, Shift As Integer, X As
Single, Y As Single)
Me.Text0.ForeColor = vbRed
Me.Text0.Controls(0).ForeColor = vbRed
End Sub


*Code in Class module which is named clsHoverCtl

Private Const mcStrEventProc As String = "[Event Procedure]"
Public Property Set ctl(ctl As Control)
On Error Resume Next
Set mctl = ctl
Select Case mctl.ControlType
Case acTextBox
Set mctlTxt = mctl
mctlTxt.OnMouseMove = mcStrEventProc
Case acLabel
Set mctlLabel = mctl
mctlLabel.OnMouseMove = mcStrEventProc
End Select
End Property
Public Property Get ctl()
Set ctl = mctl
End Property

Private Sub mctlLabel_MouseMove(Button As Integer, Shift As Integer, X As
Single, Y As Single)
mctlLabel.ForeColor = vbRed
Set mctlLabel.Parent.mLastCtl = mctlLabel
End Sub


Private Sub mctlTxt_MouseMove(Button As Integer, Shift As Integer, X As
Single, Y As Single)
mctlTxt.ForeColor = vbRed
Set mctlTxt.Parent.mLastCtl = mctlTxt
On Error Resume Next
mctlTxt.Controls(0).ForeColor = vbRed
End Sub



--
Sandra Daigle [Microsoft Access MVP]
Please post all replies to the newsgroup.


Joe said:
Many thanks for that Sandra.

Yes I would be very grateful if you could dig out the code for me if
possible.

Once again, many thanx.

Joe.


Sandra Daigle said:
Hi Joe,

Here is an article that describes how to do this - to use, you have
to put a function call into the OnMouseMove property (in the
property sheet) of each control. The function call passes the name
of the control to the function.

http://www.mvps.org/access/forms/frm0037.htm

There is a way to automate this using a class module and WithEvents.
It's a bit more extensible and easier to implement on a form with
many controls. I have some sample code at my office - I'll put it
together for you if you are interested.

--
Sandra Daigle
[Microsoft Access MVP]
For the benefit of others please post all replies to this newsgroup.

Joe Best wrote:
Thanks for the reply.

What I failed to mention was that I don't just have one label that
I want to change back to a different colour, I actually need to
change all labels.

I currently have a rectangle box (shpSide) on my form with two
labels (lblSaveDetails & lblGoToNCRBrowser) on top of that. When I
mouse move over a label it changes from white to yellow and then
when the mouse passes over the shpSide control the fore colour of
the label returns to white.

As I mentioned, this is the same for all six forms. Hope I have
explained this properly.

Current code is:

Private Sub lblSaveDetails_MouseMove(Button As Integer, Shift As
Integer, X As Single, Y As Single)
Me.lblSaveDetails.ForeColor = vbYellow
End Sub


Private Sub lblGoToNCRBrowser_MouseMove(Button As Integer, Shift As
Integer, X As Single, Y As Single)
Me.lblGoToNCRBrowser.ForeColor = vbYellow
End Sub

Private Sub shpSide_MouseMove(Button As Integer, Shift As Integer,
X As Single, Y As Single)
Me.lblSaveDetails.ForeColor = vbWhite
Me.lblGoToNCRBrowser.ForeColor = vbWhite
End Sub

How would I implement this in a module?

Joe.


Yes - put the procedure into a standard module - you can pass the
control object to the procedure so that the procedure can remain
ignorant about where the control actually exists.

public sub HoverLabel(ctlIn as control)
'your code goes here and instead of using syntax
'like me!MyLabel you refer directly to ctlIn
'for example:
ctlin.forecolor=vbyellow
End sub


--
Sandra Daigle
[Microsoft Access MVP]
For the benefit of others please post all replies to this
newsgroup.

Joe Best wrote:
I have 6 forms with a label on each called lblSaveDetails.

When the user hovers over the label, it's colour changes from
white to yellow. I have placed the code for this to happen within
each form as a sub procedure.

What I would like to do have this code appear only once in a
module. Do i need to create a public function to do this. Any
ideas please?

Many thanks,

Joe.
 
Back
Top