Public Sub or Function to manage Control settings

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

Guest

Thanks for taking the time to read my question.

I want to use labels for buttons instead of buttons. That way when the mouse
moves over the button, I can change its appearance.

That being said... instead of having code on all my label buttons, I'd like
to have a function or public sub that can take in the name of the control,
and adjust accordingly. I can't seem to get this to work. I can make it
work on the code for the form, but not in a public sub or function.

Here is what I have tried.

Click On The Label Button:
'Code on Form
Private Sub cmdOpenClientDatalbl_MouseDown()
LBLToButtonDown ("cmdOpenClientDatalbl")
End Sub

'Code in Module
Function LBLToButtonDown(TheControlName As String)
Dim ctl As Control
With ctl(TheControlName)
.SpecialEffect = 2
.BackStyle = 1
End With
End Function

Any ideas on how I can affect the settings of a control on a form, from a
function or public sub by passing the name of the control.

Thanks,

Brad
 
You should use the buttons. You can do the same thing with a command button.
Use the Mouse Move event.
 
I understand, but you can't make a button "Flat" then on the Mouse Move make
it raised.

Brad
 
Then try using a toggle button instead. Since a label has no events, you are
going to have a hard time making what you are trying to do work, if you can
at all.
Also, be more specific with your questions. You did not say that in your
original post.
 
Sorry I didn't mention that I want all my LabelButtons flat.

What I want is to have flat buttons, then when the user moves the mouse
over, the LabelButton becomes Raised, on Mouse Down, the LabelButton is
sunken, and on Mouse Up, the LabelButton returns to being raised. Moving the
mouse to the Detail section, makes all LabelButtons flat again.

I'm playing with this now. How can I get variables into this command?

CODE:
Forms!TheFormName!TheControlName.SpecialEffect = 1

I can get this to work, but it is static
Forms!MainForm!LabelButton1.SpecialEffect = 1

I don't want to have to write out every single control. I'd like to pass
the name of the control and form name and manipulate with one bit of code.

Thanks again for your help,

Brad
 
Look in the Object Browser for the Controls collection. I think you will
find some useful info there.
Too bad command buttons don't have special effects. It is leaving you with
some really hard work to get a simple visual effect.
BTW, had I read your code more closely, I could have figured out what you
wanted. My apologies.
 
Brad,

This is what I use on my forms for label control like you are asking:

Function LabelMouseAction(action As String, i As Integer)
On Error GoTo Err_Handler

Dim ctl As Control
Dim strPrevious As String

For Each ctl In Me.Form.Controls
If (ctl.ControlType = acLabel And ctl.Tag = i) Then
Select Case action
Case "mousedown"
ctl.SpecialEffect = 2
Case "mouseup"

Case "mousemove"
ctl.SpecialEffect = 1
ctl.BackStyle = 1
End Select
Else
With ctl
.SpecialEffect = 0
.BackStyle = 0
End With
End If
Next

Exit_Handler:
Exit Function

Err_Handler:
MsgBox Err.Number & " " & Err.Description
Resume Exit_Handler

End Function

Now for each label set the tag property to a number i.e. 1...2...etc for
each label you want the effect. Then in the Mouse Move event for each label
call the function like so
=LabelMouseAction("mousemove",1) for the first label
=LabelMouseAction("mousemove",2) for the second label and so forth

For the Mouse Down would be
=LabelMouseAction("mousedown",1) etc for each label on the mouse down

For the Detail section
=LabelMouseAction("mousemove",0)

This will make the label raised when the mouse is moved over the label and
appear sunken when you click down on the mouse. When you move to the detail
section away from the label it will restore back to the flat appearance.

I also added a "mouseup" to the select case, but you can add any number of
select items you wish.

Hope this helps
lwells
 
I just found that as soon as I was notified that you had replied.

Here is the solution I found.

Thanks again for your help.

Brad

Solution:
Function LBLToButtonOver(TheFormName As String, TheControlName As String)
Forms(TheFormName).Controls(TheControlName).SpecialEffect = 1
End Function
 
Brad said:
Thanks for taking the time to read my question.

I want to use labels for buttons instead of buttons. That way when
the mouse moves over the button, I can change its appearance.

That being said... instead of having code on all my label buttons,
I'd like to have a function or public sub that can take in the name
of the control, and adjust accordingly. I can't seem to get this to
work. I can make it work on the code for the form, but not in a
public sub or function.

Here is what I have tried.

Click On The Label Button:
'Code on Form
Private Sub cmdOpenClientDatalbl_MouseDown()
LBLToButtonDown ("cmdOpenClientDatalbl")
End Sub

'Code in Module
Function LBLToButtonDown(TheControlName As String)
Dim ctl As Control
With ctl(TheControlName)
.SpecialEffect = 2
.BackStyle = 1
End With
End Function

Any ideas on how I can affect the settings of a control on a form,
from a function or public sub by passing the name of the control.

Thanks,

Brad

Just pass a reference to the control itself, instead of its name:

Function LBLToButtonDown(ctl As Access.Control)

With ctl
.SpecialEffect = 2
.BackStyle = 1
End With

End Function

Your code to call the function then becomes:

Private Sub cmdOpenClientDatalbl_MouseDown()
LBLToButtonDown Me!cmdOpenClientDatalbl
End Sub

Or you can just set the OnMouseDown event property to

=LBLToButtonDown([cmdOpenClientDatalbl])
 
Hey that's really neat too.

I don't understand why you don't need to put () around Me!cmdOpenClientDatalbl
in
Private Sub cmdOpenClientDatalbl_MouseDown()
LBLToButtonDown Me!cmdOpenClientDatalbl
End Sub

Also, why is there an equals sign in your second option? Does the equals
sign equal Call?(That sentence is almost like "Who's on First")

Brad

Dirk Goldgar said:
Brad said:
Thanks for taking the time to read my question.

I want to use labels for buttons instead of buttons. That way when
the mouse moves over the button, I can change its appearance.

That being said... instead of having code on all my label buttons,
I'd like to have a function or public sub that can take in the name
of the control, and adjust accordingly. I can't seem to get this to
work. I can make it work on the code for the form, but not in a
public sub or function.

Here is what I have tried.

Click On The Label Button:
'Code on Form
Private Sub cmdOpenClientDatalbl_MouseDown()
LBLToButtonDown ("cmdOpenClientDatalbl")
End Sub

'Code in Module
Function LBLToButtonDown(TheControlName As String)
Dim ctl As Control
With ctl(TheControlName)
.SpecialEffect = 2
.BackStyle = 1
End With
End Function

Any ideas on how I can affect the settings of a control on a form,
from a function or public sub by passing the name of the control.

Thanks,

Brad

Just pass a reference to the control itself, instead of its name:

Function LBLToButtonDown(ctl As Access.Control)

With ctl
.SpecialEffect = 2
.BackStyle = 1
End With

End Function

Your code to call the function then becomes:

Private Sub cmdOpenClientDatalbl_MouseDown()
LBLToButtonDown Me!cmdOpenClientDatalbl
End Sub

Or you can just set the OnMouseDown event property to

=LBLToButtonDown([cmdOpenClientDatalbl])

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 
Brad said:
Thanks for taking the time to read my question.

I want to use labels for buttons instead of buttons. That way when the mouse
moves over the button, I can change its appearance.

That being said... instead of having code on all my label buttons, I'd like
to have a function or public sub that can take in the name of the control,
and adjust accordingly. I can't seem to get this to work. I can make it
work on the code for the form, but not in a public sub or function.

Here is what I have tried.

Click On The Label Button:
'Code on Form
Private Sub cmdOpenClientDatalbl_MouseDown()
LBLToButtonDown ("cmdOpenClientDatalbl")
End Sub

'Code in Module
Function LBLToButtonDown(TheControlName As String)
Dim ctl As Control
With ctl(TheControlName)
.SpecialEffect = 2
.BackStyle = 1
End With
End Function

Any ideas on how I can affect the settings of a control on a form, from a
function or public sub by passing the name of the control.


The With statement is out of whack. Your problem is in
referring to the control on different forms by just passing
the name of the controls. The with statement should be:
With Me(TheControlName)

But this will only work if the code is in the form's module,
which may be all you need in this case. If you want to
create a more general function, then it is better is to pass
the control object itself:

'Code on Form
Private Sub cmdOpenClientDatalbl_MouseDown()
LBLToButtonDown (Me.cmdOpenClientDatalbl)
End Sub

'Code in Module
Function LBLToButtonDown(TheControl As Control)
With TheControl
.SpecialEffect = 2
.BackStyle = 1
End With
End Function

This way the function can be in any module and it is not
tied to any one form.
 
Brad said:
Hey that's really neat too.

I don't understand why you don't need to put () around
Me!cmdOpenClientDatalbl in
Private Sub cmdOpenClientDatalbl_MouseDown()
LBLToButtonDown Me!cmdOpenClientDatalbl
End Sub

This is one of the alternative methods of calling a procedure and
passing parameters. You can use

Call ProcedureName(parameter1, parameter2, ...)

or you can use

ProcedureName parameter1, parameter2, ...

If you make a mistake, and write

ProcedureName (parameter1)

you're actually forcing the evaluation of parameter1, before its value
is passed to the procedure. This can cause problems sometimes; it
would in this case, because we want to pass the control itself, not its
value.
Also, why is there an equals sign in your second option? Does the
equals sign equal Call?(That sentence is almost like "Who's on First")

The equals sign there is the standard tag for an expression to be
evaluated in a control property; in this case, the event property
OnMouseDown. What I was talking about was not writing an event proedure
for the button's MouseDown event, but putting that function expression
into the OnMouseDown event property itself, just as you might put a
function expression in the ControlSource property of a calculated
control. This is a trick that Access supports.
 
Marshall Barton said:
The With statement is out of whack. Your problem is in
referring to the control on different forms by just passing
the name of the controls. The with statement should be:
With Me(TheControlName)

But this will only work if the code is in the form's module,
which may be all you need in this case. If you want to
create a more general function, then it is better is to pass
the control object itself:

'Code on Form
Private Sub cmdOpenClientDatalbl_MouseDown()
LBLToButtonDown (Me.cmdOpenClientDatalbl)
End Sub

While we're in agreement on the approach, Marsh, the parentheses in that
call will cause the value of the control to be passed, not the control
itself.
 
Dirk said:
"Marshall Barton" wrote

While we're in agreement on the approach, Marsh, the parentheses in that
call will cause the value of the control to be passed, not the control
itself.


Yes, of course. I going to go right now and make that darn
parenthesis gremlin promise to never do that again or he'll
sit in the corner all night ;-)
 
Back
Top