Solved

Using control created at runtime

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
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…

758 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

21 Experts available now in Live!

Get 1:1 Help Now