Results 1 to 5 of 5
  1. #1
    mainerain is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Sep 2018
    Location
    Maine, USA
    Posts
    146

    Command Button that opens a Form based on its own Caption

    Is there a way to create generic vba that would be able to get the corresponding Command Buttons Caption? The On Click Event would trigger opening a Form based on that Command Buttons Caption. I have many similar Command Buttons and having generic vba would seem to be easier.



    Thanks

  2. #2
    ssanfu is offline Master of Nothing
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Would you provide examples of button captions and the associated forms that should be opened?

    Here is one way:
    For a button named "Command0", this will display the button caption, then open the form
    Code:
    Private Sub Command0_Click()
        MsgBox Me.Command0.Caption   'comment out or delete this line after debugging
        Call OpenMyFormByCaption(Me.Command0.Caption)
    End Sub
    You could write a sub that has the button caption as an argument, that opens the form.
    Code:
    Private Sub OpenMyFormByCaption(btnCaption As String)
        DoCmd.OpenForm btnCaption
    End Sub
    Note:
    You shouldn't use spaces in form names.
    The button caption must match the form name.

  3. #3
    mainerain is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Sep 2018
    Location
    Maine, USA
    Posts
    146
    i.e. Caption = LT-0100 Name = btnLT-0100 Private Sub btnLT_0100_Click() to open frmLTLoop via extracting the LT from the Caption with code

    edit
    I just realized that because each button has to have a unique name that it's easier to individualize the vba for each button.
    Code:
            DoCmd.OpenForm "frmLTLoop", acNormal, , "LoopTag = 'LT-0100'"

  4. #4
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,826
    Maybe you should be selecting the form from a combo rather than having many buttons? One column could show the form name by description, another (hidden) would be the object name?
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  5. #5
    mainerain is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Sep 2018
    Location
    Maine, USA
    Posts
    146
    Thanks for the input.
    I chose to use a listbox. Never tried that before but is working out well. Thanks.

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

Similar Threads

  1. Replies: 2
    Last Post: 09-20-2020, 10:32 AM
  2. Replies: 6
    Last Post: 07-28-2017, 06:02 PM
  3. Replies: 12
    Last Post: 11-22-2015, 02:34 PM
  4. Replies: 1
    Last Post: 02-27-2015, 10:01 AM
  5. Replies: 1
    Last Post: 09-12-2014, 06:09 AM

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