Determine which command button was clicked

Falcon88

Registered User.
Local time
Today, 17:08
Joined
Nov 4, 2014
Messages
309
i have 6 command buttons in a continous form, cmd buttons used for sorting some fields in my form. how to make the cmd button that user click it is determined to knows which cmd button is clicked (Ordered by which cmd).
 
Do your command buttons run code when you click them?
 
How do you NOT know which button was clicked? Like, say I have a button called cmdMyButton. Then I'll have code like . . .
Code:
Private Sub cmdMyButton_Click()
   msgbox "The button clicked is called " & me.cmdMyButton.Name
   msgbox "It's type is: " & typename(Me.cmdMyButton)
End Sub
. . . so the button clicked has its name in the event handler. I don't understand how you can not know which button it was.

Or have you set the OnClick property of the button to something like . . .
Code:
"=SomeOtherFunction()"
. . . rather than . . .
Code:
"[Event Procedure]"
In that case, check out the Access.Form.ActiveControl property, which returns a reference to the object on the form that has the focus.
Hope this helps,
 
I want to do some special effect on the clicked button.
 
can you not do whatever you want in the event code?
 
Maybe you can show your code . . .
 
Code:
Private Sub Form_Timer() 
   If Me.OffFilterBtn.ForeColor = vbRed Then 
      Me.OffFilterBtn.ForeColor = vbBlue 
      Me.OffFilterBtn.BackStyle = 1 
      Me.OffFilterBtn.FontSize = 18 
   Else 
      Me.OffFilterBtn.ForeColor = vbRed 
      Me.OffFilterBtn.BackStyle = 0 
      Me.OffFilterBtn.FontSize = 12 
   End If 
End Sub
and
Code:
Private Sub Form_Current()  
   Me.TimerInterval = 500 'twice per second 
End Sub
 
Last edited by a moderator:
These are events raised by the Form object. These are not button clicks.
 
How do I do like it, but it works when Click any button of the six buttons
 
I'm sorry but I don't really understand.

The code you posted is not run by a command button click. To identify a button click we need to find the code that runs when that button is clicked. That code will have a signature like . . .
Code:
Private Sub [I]ButtonName[/I]_Click()
. . . if the click is handled in VBA, but the button may also run a macro.
 

Users who are viewing this thread

Back
Top Bottom