Results 1 to 9 of 9
  1. #1
    bilalo is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2019
    Posts
    107

    vba click events

    I have a form with 30 buttons that have captions from 1 to 30. Each button should print its caption when clicked. Of course I can create 30 click events but the problem is if I need to change my code later I will have to change it 30 times. Is there a simpler way?

  2. #2
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,389
    You could do away with the buttons and use a combobox. The combobox would list the 20 captions in the dropdown and the afterupdate event would select that caption for whatever.

  3. #3
    bilalo is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2019
    Posts
    107
    Unfortunately I need to have 30 buttons on my form

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    How about

    Code:
    Public Function TestActiveControl()
      MsgBox Screen.ActiveControl.Caption
    End Function
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,737
    You go to design view on the form and select (drag over) the 30 controls. On the property sheet for the click event (if that's the event you want to use) you enter = FunctionName(), which of course is your function name, not what I wrote. That function will be called for all 30 controls. But this is not clear
    Each button should print its caption
    You are printing the form?
    Last edited by Micron; 01-31-2019 at 04:24 PM.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  6. #6
    bilalo is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2019
    Posts
    107
    Quote Originally Posted by pbaldy View Post
    How about

    Code:
    Public Function TestActiveControl()
      MsgBox Screen.ActiveControl.Caption
    End Function
    Nice idea but where do I call this method, I think I still need to call it 30 times

  7. #7
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    You would need to call it from every button, but if the code changed it would only be changed once, in the function.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  8. #8
    bilalo is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2019
    Posts
    107
    Yes I think this is the best choice for my case

  9. #9
    bilalo is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2019
    Posts
    107
    Quote Originally Posted by Micron View Post
    You go to design view on the form and select (drag) over the 30 controls. On the property sheet for the click event (if that's the event you want to use) you enter = FunctionName(), which of course is your function name, not what I wrote. That function will be called for all 30 controls. But this is not clearYou are printing the form?
    Thank you, it worked perfectly

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 4
    Last Post: 03-30-2018, 02:31 PM
  2. Replies: 3
    Last Post: 05-23-2013, 05:30 PM
  3. Different Events
    By jo15765 in forum Programming
    Replies: 2
    Last Post: 12-07-2010, 12:45 PM
  4. Events Confusion?
    By homerj56 in forum Programming
    Replies: 5
    Last Post: 10-27-2010, 01:45 PM
  5. Form_Load Events
    By TheDeceived in forum Programming
    Replies: 1
    Last Post: 09-20-2010, 12:19 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums