dcsimg
Results 1 to 15 of 15

Thread: [RESOLVED] which button was clicked

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Italy-Napoli
    Posts
    1,925

    Resolved [RESOLVED] which button was clicked

    i have a sequence of buttons from A to Z.

    The sequence is:

    button1 is letter A button2 is letter B.... button26 is letter Z

    I need to check which button was clicked and get the caption name, for example assigng to MyVar="A" for button1

    Possible?

    the steep of sequence is by 1 from 1 to 26
    Attached Images Attached Images  
    Last edited by luca90; Nov 17th, 2019 at 01:09 PM.

  2. #2
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    7,222

    Re: which button was clicked

    If you use an array of that cmdButton, it would be much easier for you, AND make more sense.

    So, if command1(0) was your first button, command1(25) would be your last. On the click event of the button, simply check the Index. Index 0 would be A, index 1 would be B, and so forth.

  3. #3
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    18,527

    Re: which button was clicked

    Without a button array, if the clicked button still has focus when you are checking which button was clicked, it should be Me.ActiveControl
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  4. #4
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    12,995

    Re: which button was clicked

    At the risk of stating the obvious. The click event will fire when you click the button. Your code to detect which button could go there.
    If you are not using a control array then there will be 26 click events one for each button.
    If you are using a control array [which is what you should be doing] then you would have only one click event where you would simply check the index

    Code:
    SelectedCaption=Command1(Index).Caption

  5. #5
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    1,999

    Re: which button was clicked

    And all of those recommendations (which are true for VB6) don't work in VBA, since Office/VBA doesn't have a control-array,
    and Luca is known for working in VBA without stating it in the subject of the thread.
    I remember Elroy designing a "kinda"-control-array for VBA.

    In any case: if it's VBA the general workaround would be checking the controlname
    One System to rule them all, One IDE to find them,
    One Code to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    For health reasons i try to avoid reading unformatted Code

  6. #6
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    7,222

    Re: which button was clicked

    Correct on the VBA....guess that ALWAYS should be the obvious question with a lot of folks here.
    So, if VBA, you will need to capture EACH button like this:


    Code:
    Private Sub Command1_Click()
        Debug.Print Command1.Caption
    End Sub
    Code:
    Private Sub Command2_Click()
        Debug.Print Command2.Caption
    End Sub
    etc.

    But, if NOT VBA, that is, IS VB6, see post #4.

  7. #7
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    5,835

    Re: which button was clicked

    Here's my link to how to do control arrays in the VBA, just in case you'd like to use that.
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  8. #8
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,706

    Re: which button was clicked

    VBA belongs in the Office forum.

    Why wouldn't the macro be running the event handler for the clicked button? At that point you know which button was clicked and you can just look at its Caption.

  9. #9
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    12,995

    Re: which button was clicked

    Agreed VBA doesn't belong here so if the OP is really coding in VBA then he/she needs to start posting the the office forum where VBA questions belong.
    When posting questions in a VB6 forum you get answers that assume you are using VB6 and those may or may not apply to Office VBA or any other form of VBA from that matter.

  10. #10

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Italy-Napoli
    Posts
    1,925

    Re: which button was clicked

    Quote Originally Posted by SamOscarBrown View Post
    If you use an array of that cmdButton, it would be much easier for you, AND make more sense.

    So, if command1(0) was your first button, command1(25) would be your last. On the click event of the button, simply check the Index. Index 0 would be A, index 1 would be B, and so forth.
    HI bro...
    used your seggest and now work!
    Tks

    but now my problem is to fill the MYArray () as String, with the Caption of button.

    Actually i have CMD(0) to CMD(35)

    How to loop this array button?

    To the end of code, i need:
    MyArray=Array("A","B"... "0")
    Last edited by luca90; Nov 21st, 2019 at 10:58 AM.

  11. #11
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    18,527

    Re: which button was clicked

    Something like this, without using the Array() method?
    Code:
    Dim n As Long, MyArray() As Variant
    
    ReDim MyArray(0 to 35) ' or ReDim MyArray(CMD.LBound To CMD.UBound)
    For n = 0 To 35 ' or use CMD.LBound to CMD.UBound if applicable
        MyArray(n) = CMD(n).Caption
    Next
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  12. #12
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    7,222

    Re: which button was clicked

    If you already have an array of command buttons, why are you wanting to create another array just to put the buttons' captions in it? Purpose?

  13. #13

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Italy-Napoli
    Posts
    1,925

    Re: which button was clicked

    Quote Originally Posted by LaVolpe View Post
    Something like this, without using the Array() method?
    Code:
    Dim n As Long, MyArray() As Variant
    
    ReDim MyArray(0 to 35) ' or ReDim MyArray(CMD.LBound To CMD.UBound)
    For n = 0 To 35 ' or use CMD.LBound to CMD.UBound if applicable
        MyArray(n) = CMD(n).Caption
    Next
    Nice solution bro!
    Pizza?

  14. #14
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    12,995

    Re: [RESOLVED] which button was clicked

    I'm not sure why you would need another array. If you have a control array then you have all you need.
    When the button is clicked an index is passed to the click event. Using that index you can retrieve the caption directly without needing to place all of the captions in another array.
    Makes no sense to add another array when you can do what you want with literally one line of code like I showed in my earlier post.

  15. #15
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    7,222

    Re: [RESOLVED] which button was clicked

    @DM---believe I said that earlier...maybe OP misunderstood????

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width