Event Handlers

B

Bunster147

I am using outlook 2007 (Latest version) with BCM and developing a small for
a home business that I run.
I am trying to create a form which shows a varying number of product items
on it and I need to handle the events of these product rows, e.g. when
clicked, show the products details.
As the number of lines on the report varies I am programmatically creating
the controls on the form using the visual basic software which comes with
Microsoft office 2007.
The version of VB shows as “Microsoft Visual Basic 6.5†on the “About
Microsoft Visual Basic†Help screen.
This is part of the code used to generate the controls.
Please note that the form has already been created in Project with no
controls. The following code is just test code I am using to test the event
handling problem.
Private Sub UserForm_Initialize()
Dim mycmd As Control
Dim strControl As String
strControl = "Forms.Label.1"
Set mycmd = TestScreen.Controls.Add(strControl)
mycmd.Left = 5
mycmd.Top = 5
mycmd.Width = 70
mycmd.Height = 12
mycmd.BackColor = vbRed
mycmd.TextAlign = 3
mycmd.Caption = "Test Label"
mycmd.Visible = True
mycmd.ForeColor = vbBlack
mycmd.Name = "TEST"
End Sub
On inspection of the code when debugging, the names of the controls (All
Labels) are Label1, Label2 etc.
My idea was that I would have a generic event handler to handle the events
for those controls that I need events for.
But here is the problem I have encountered.
Initially I inserted the code
Private Sub TEST_click()
MsgBox ("clicked")
End Sub
But no events were picked up on the new control.
I was thinking of adding a generic event handler but thought if the events
are not being generated then this would not help either.

All I need to know is how to have the created controls react to events.
 
E

Eric Legault [MVP - Outlook]

If you had a control variable named TEST the click event would work.
Try adding a module level variable for a Label object called myLabel:

Private WithEvents myLabel As Label

Then in the Initialize event, after you create the mycmd object (declare
it as a Label object, not a Control object, do:

Set myLabel = mycmd

Then you'll have a myLabel_Click() event.

--
Eric Legault [MVP - Outlook]
MCDBA, MCTS (Messaging & Collaboration, SharePoint Infrastructure, MOSS
2007 & WSS 3.0 Application Development)
President
Collaborative Innovations
-> Try Picture Attachments Wizard 2.0 For Microsoft Outlook <-
-> Take your SharePoint content offline <-
-> More info: http://www.collaborativeinnovations.ca <-
Blog: http://blogs.officezealot.com/legault
 
B

Bunster147

Thanks Eric. This seems fine if I have a single label but the number of
labels to display are unknown when the form is initialised so how do I go
about declaring more than one "Private WithEvents myLabel As Label".
Can I declare these on the fly, e.g. "Private withevents mylabelX as label,
where X is an integer.

Eric Legault said:
If you had a control variable named TEST the click event would work.
Try adding a module level variable for a Label object called myLabel:

Private WithEvents myLabel As Label

Then in the Initialize event, after you create the mycmd object (declare
it as a Label object, not a Control object, do:

Set myLabel = mycmd

Then you'll have a myLabel_Click() event.

--
Eric Legault [MVP - Outlook]
MCDBA, MCTS (Messaging & Collaboration, SharePoint Infrastructure, MOSS
2007 & WSS 3.0 Application Development)
President
Collaborative Innovations
-> Try Picture Attachments Wizard 2.0 For Microsoft Outlook <-
-> Take your SharePoint content offline <-
-> More info: http://www.collaborativeinnovations.ca <-
Blog: http://blogs.officezealot.com/legault



I am using outlook 2007 (Latest version) with BCM and developing a small
for
a home business that I run.
I am trying to create a form which shows a varying number of product
items
on it and I need to handle the events of these product rows, e.g. when
clicked, show the products details.
As the number of lines on the report varies I am programmatically
creating
the controls on the form using the visual basic software which comes
with
Microsoft office 2007.
The version of VB shows as "Microsoft Visual Basic 6.5" on the "About
Microsoft Visual Basic" Help screen.
This is part of the code used to generate the controls.
Please note that the form has already been created in Project with no
controls. The following code is just test code I am using to test the
event
handling problem.
Private Sub UserForm_Initialize()
Dim mycmd As Control
Dim strControl As String
strControl = "Forms.Label.1"
Set mycmd = TestScreen.Controls.Add(strControl)
mycmd.Left = 5
mycmd.Top = 5
mycmd.Width = 70
mycmd.Height = 12
mycmd.BackColor = vbRed
mycmd.TextAlign = 3
mycmd.Caption = "Test Label"
mycmd.Visible = True
mycmd.ForeColor = vbBlack
mycmd.Name = "TEST"
End Sub
On inspection of the code when debugging, the names of the controls (All
Labels) are Label1, Label2 etc.
My idea was that I would have a generic event handler to handle the
events
for those controls that I need events for.
But here is the problem I have encountered.
Initially I inserted the code
Private Sub TEST_click()
MsgBox ("clicked")
End Sub
But no events were picked up on the new control.
I was thinking of adding a generic event handler but thought if the
events
are not being generated then this would not help either.

All I need to know is how to have the created controls react to events.
 

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