Solved

Using control created at runtime

Posted on 1998-10-14
3
143 Views
Last Modified: 2010-04-30
I have a form in VBA which has a command button. Every time I click it I create a new command button.
How do I attach an event to the new buttons, so that when I click any of themm, I can run some code?

The code so far is as follows:-

Dim Mycmd As Control

Private Sub CommandButton1_Click()
Static i&

i& = i& + 1
Set Mycmd = Controls.Add("Forms.CommandButton.1")
With Mycmd
  .Left = 12
  .Top = 25 * i& + 25
  .Caption = .Name
End With

End Sub
0
Comment
Question by:broadbent
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 3

Accepted Solution

by:
traygreen earned 50 total points
ID: 1439901
Create the button as the 0 item in a control array
eg name = cmdTest
   item = 0

replace your set code with
   iCurrentCont = cmdTest.count -1
   Load cmdTest(iCurrentCont)
   cmdTest(iCurrentControl).left = .......

This will allow all of the new controls to access the evnets set up for the intital command button.  You could build the logic into the event (say click) to account for there being more than one control element
0
 
LVL 5

Expert Comment

by:AnswerTheMan
ID: 1439902
traygreen's answer is correct but he did not explain it well.
if i was in a situation that i know nothing about it - i would not understand a thing from his answer.
traygreen,  if u AnswerTheMan - do it from your heart.
0
 
LVL 4

Author Comment

by:broadbent
ID: 1439903
sorry , but controls in VBA can't be indexed!
Thats the problem
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

729 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