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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

onclick event

Status
Not open for further replies.

claudiu

Technical User
Sep 29, 2001
4
RO
hi,

This is what I'm trying to accomplish programaticaly:
1. create a form
2. add some buttons on the form
3. set the onclick property of the buttons so that, when clicked, they run different functions

I did all the above but...
What I don't know is how to write the button function so that the action reffers to each button separately.
something like:
public function buton()
msgbox button.name

'i.e. display the name of the clicked button
end function


any suggestions?
thanks
Claudiu
 
Hi,
use the function in a module.

Public Sub displayButtonName(ByVal button as Control)
msgBox button.Properties("Name")
End Sub

I ahve used 2 command buttons to demonstrate the code:

Private Sub Command0_Click()
Call displayButtonName(Me.Command0)
End Sub

Private Sub Command1_Click()
Call displayButtonName(Me.Command1)
End Sub Hope it helps. Let me know what happens.
With regards,
PGK
 
Hi PGK!

thanks for your reply.
the problem is:
I create the buttons (a variable number of them) from code.
I want to have it all done with a single code routine. That is: the form, the buttons and the possibility to run different commands by clicking different buttons.

So, I can't write the onclick routines you suggested.
I need some (compact) code that can "capture" the name of any of the buttons clicked - because, as mentioned - their number is variable.

anyway. I need to search information about ByVal because it might be the key of the problem.

thanks,
Claudiu
 
Try this:
Create the form in code. Then use the CreateEventProc method to assign the following code to an event of each command button:

[tt]Call yoursub(Me.ActiveControl.Properties("Name"))[/tt]

This will cause whatever event you assign it to to fire the name of the control to the subroutine. Then just make your code actually run from the sub. I think that does what you need.
 
I forgot to mention you'd need to change your function or sub to accept an argument:

[tt]public function buton(controlname)
msgbox controlname
'i.e. display the name of the clicked button
end function[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top