Solved

Handlers for dynamically created buttons

Posted on 2007-03-29
5
152 Views
Last Modified: 2011-09-20
Dear Experts,

Im working on a VBA program where on the form I create some textboxes and buttons dynamically.
I want to handle events for the dynamically created objects.
I think Addhandler is the way to do this but I can't seem to figure out exactly how it works, even after extensive googling. Any help would be much appreciated.

Here is a test setup of my code:

On the form:

Public i As Integer
Private Sub Form_Load()
End Sub

Private Sub CommandButton1_Click()

Set cmbobject = UserForm1.Controls.Add("forms.commandbutton.1")
With cmbobject
.Name = "textbox" & i
.Caption = "text" & i
.Width = 50
.Left = 10
.Top = 10 + (i * 20)
.Height = 20
End With
   
    AddHandler cmbobject.Click, AddressOf cmbobject_Click

i = i + 1
End Sub


and in a separate module in the same project:


Public Sub cmdobject_click()
MsgBox ("test")
End Sub


The AddressOf keeps giving me the error message : expected sub, function or property
and if I change it to:
AddHandler cmbobject.Click, AddressOf Module1.cmdobject_click
it says: sub or function not defined

Thanks in advance

Andreas
0
Comment
Question by:FEWEB
  • 3
  • 2
5 Comments
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 18815677
Hi,
AddHandler does not exist in VBA. In order to do what you want, you will need to create a class, add a commandbutton variable declared Public WithEvents and create the click event for it. Then when the form loads, you create a new instance of this class for each commandbutton you create and set the cb variable equal to the commandbutton you created. If you need totally different routines for each commandbutton, you may want to create separate classes for each.
HTH
Rory
0
 

Author Comment

by:FEWEB
ID: 18822362
Dear Rory,

I'm reading up on Class modules now as i am not familiar with them but if you could give me some pointers or a bit of sample code that would be awesome.

The routine for each commandbutton would be the same.

Thanks again

Andreas
0
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 500 total points
ID: 18822435
Andreas,
I have put together a demonstration workbook and uploaded it here: https://filedb.experts-exchange.com/incoming/ee-stuff/3033-button_class.zip
Hopefully it is clear what is happening in the code, but if you have any questions, let me know.
Rory
0
 

Author Comment

by:FEWEB
ID: 18822446
Thank you very much!, I should be able to work it out from here on.

Thanks for the quick response as well!

Kind regards,

Andreas

0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 18822467
Happy to help - thanks for the grade.
Rory
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question