To access a previous object via class module

P

Pierre Archambault

Hi,

I have a UserForm wich contains 20 Labels that should react to the Click
event. All those Labels have the special effect "Raised". When the user
clicks on one of them, it sinks like a button "Sunken". This works fine but
what I want is that when the user clicks on another Label, the previous one
returns to its original state (Raised) while the actuel Label sinks
"Sunken".

First I wrote some code for each of the Labels to obtain the desired effect:

In the UserForm module:

Public MyObject As Control

Private Sub LabelA1_Click() 'For each Label (20 times)
Set MyObject = LabelA1
Call SinkLabel
End Sub

Then a procedure to go from "Raised" to "Sunken":

Public Sub SinkLabel()
Static PrevObject As Control

If MyObject.SpecialEffect = fmSpecialEffectSunken Then Exit Sub

MyObject.SpecialEffect = fmSpecialEffectSunken

If PrevObject Is Nothing Then
'Do nothing
Else
PrevObject.SpecialEffect = fmSpecialEffectRaised
End If
Set PrevObject = MyObject

End Sub


Now I want to use a class module to acheive the same goal, But it works only
for the Label on wich the users clicks. It looks like it's impossible to get
the previous Label regain the "Raised" state.


If someone has an idea... Thank you very much.

Pierre
 
L

Leith Ross

Hello Pierre,

Add the following code to the General declarations section of your user
form. Add the following call to each Label's MouseDown event code...

EXAMPLE:
Private Sub Label1_MouseDown(ByVal Button As Integer, ByVal Shift As
Integer, ByVal X As Single, ByVal Y As Single)

Call ToggleState("Label1")

End Sub
______________________________

USERFORM CODE:

Code:
--------------------

Public LastLabel As String

Public Sub ToggleState(ByVal Label_Name As String)

Dim Label As Label

Set Lbl = Controls(Label_Name)

Lbl.SpecialEffect = fmSpecialEffectSunken

If LastLabel <> "" Then
Set Lbl = Controls(LastLabel)
Lbl.SpecialEffect = fmSpecialEffectRaised
End If

LastLabel = Label_Name

End Sub

--------------------

_____________________________

Sincerely,
Leith Ross
 
A

Archampi

Thanks

I tried your solution but it does not work.

Here is my code:

In a standard module:
========================================
Public PrevLabel As String

In the class module:
========================================
Private Sub MyLabels_Click()
Dim PrevLbl As Label

If MyLabels.SpecialEffect = fmSpecialEffectSunken Then Exit Sub

MyLabels.SpecialEffect = fmSpecialEffectSunken

If PrevLabel <> "" Then
Set PrevLbl = UserForm1.Controls(PrevLabel)
PrevLbl.SpecialEffect = fmSpecialEffectRaised
End If

PrevLabel = MyLabels.Name

End Sub
========================================

This line stops with error 13:

Set PrevLbl = UserForm1.Controls(PrevLabel)



*** Sent via Developersdex http://www.developersdex.com ***
 
L

Leith Ross

Hello Pierre,

You didn't follow my directions. The UserForm code is to be placed in
the General Declarations section of the UserForm not in a standard VBA
module. This is the area above all the event (or as you refer to class)
code for the UserForm. Then place the following line in each of the
Label's MouseDown event code section. The default is the Click event.
Go to the top of the code area and click on the top right Drop Down
box. In the list you will see MouseDown listed. Click on it and then
type the following code...

Example for Label1:

Private Sub Label1_MouseDown(ByVal Button As Integer, ByVal Shift As
Integer, ByVal X As Single, ByVal Y As Single)
Call ToggleLabel(Label1)
End Sub

Repeat this for each label you use. Besure to change the Label name in
the function to match the label it will modify.

Place this code in the General Declarations section of the UserForm:

Code:
--------------------

Public Sub ToggleLabel(ByVal Lbl As MSForms.Label)

Dim ThisLabel As String

Lbl.SpecialEffect = fmSpecialEffectSunken
ThisLabel = Lbl.Name

If LastLabel <> "" And LastLabel <> ThisLabel Then
Set Lbl = Controls(LastLabel)
Lbl.SpecialEffect = fmSpecialEffectRaised
End If

LastLabel = ThisLabel

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top