Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Detect button click for dynamic button controls

Status
Not open for further replies.

RealKiwi

Technical User
Feb 26, 2004
41
NZ
I am writing a piece of VBA code for a dialog form.

The dialog form is populated with 1 to 30 buttons from the outcome of another form.

I know I can use the onclick event in each button, but the number of buttons varies, depending on the outcome from another form. The buttons are named & numbered incrementally, eg 10 buttons present: the first button is named CmdBtn1; the second CmdBtn2 and so on.

In this context the buttons are both temporary and dynamic; clicking button 1 will activate a report with characteristics a, b, c, but the next time button 1 is presented its report characteristics might be x, y, z. Characteristics a, b, and c might be associated with button 4 or 20 - it is totally arbitrary and random.

What I want to do is detect the click of one of these dynamic buttons, but I'm stuck at this point.

My thoughts so far are:

1. Use the Form_Click event (since the form is static)
2. In VBA module, cycle through each dynamic button control
Code:
For each ctl in Me.Controls
  If ctl.Controltype = acCommandButton then
  ...
  some code here to detect the button click,
  grab the button name/criteria
  ...
Next
...
do the other stuff here

Does anyone have a few clues about "button detection" events??

thanks, as always
RK
 
Hi

If the buttons are dynamic, then presumably you are creating them in code?, so why not create the code to hand the clicks in code too, then it can also be dynamic

Regards

Ken Reay
Freelance Solutions Developer
Boldon Information Systems Ltd
Website needs upgrading, but for now - UK
 
In the OnClick event for each button right? Yes, I was just curious if the "detect" click code could be outside the control object event properties ie. in the form properties.
 
Hi

Well - yes and no, you could have code in the onclick evnet of the button which called a function or subroutine which was in the form module (or even a module).

So you could for example have code like so in each on click event

HandleClick Me.ActiveControl.Name

with a routine

Public Sub HandleClick(strButtonName As String)
Select Case strButtonName
Case ...etc
End Select

End Sub

This is simulating in some ways the mechanism available in full blown VB where you can have an array of Controls (my memory is dim, but I think it is called a control group)


Regards

Ken Reay
Freelance Solutions Developer
Boldon Information Systems Ltd
Website needs upgrading, but for now - UK
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top