Solved

Using control created at runtime

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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
DIR issue 7 54
Excel object stays open 19 76
Excel VBA, find a string in a column, update a cell 7 81
MsgBox 4 61
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…
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.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

832 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