Solved

Using control created at runtime

Posted on 1998-10-14
3
139 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

943 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now