We help IT Professionals succeed at work.
Get Started

How can I assign an onclick event on a command button at runtime?

jpav asked
Last Modified: 2013-11-27
I have a multi-tab form with several pages. In every page I have a button which I am trying to assing an onclick event procedure at runtime. For the name of the buttons I established a naming convention that is BtnAddTr#  where the # varies depending on the index of the page the button is. My problem is how to assign these buttons an on click event.
The section of the code is:

For numTab = 0 To AcctMultiTab.Pages.Count - 1
    'hide the tabs that are not needed
    If numTab > UBound(AcctNames, 1) - 1 Then
        AcctMultiTab.Pages(numTab).Visible = False
        'and hide the totals by account
        Me.Controls("TOTbyAcct" & numTab).Visible = False
        'if tab is needed, dsiplay it and set its name
        With AcctMultiTab.Pages(numTab)
            .Visible = True
            .Caption = AcctNames(numTab + 1, 2)
            ' and change the text in the labels within the page
            .Controls("LblTitleSbfrm" & numTab).Caption = AcctNames(numTab + 1, 2) & " - Account Register"
            .Controls("LblBoxes" & numTab).Caption = " Balance in " & AcctNames(numTab + 1, 2) & " by type:"
            'display the currency for account
            currentAccountID = AcctNames(numTab + 1, 3)
            .Controls("AcctCur" & numTab).Value = getCurrencySymbol(currentAccountID)
            .Controls("AcctCur" & numTab).ForeColor = 16711808
            .Controls("AcctCur" & numTab).FontWeight = 600
            'now set the source row of the SbfrmDetail to a query specific to that account
            'Structure the SQL string
            SQLstr = "SELECT * FROM tbl_AccountRegister " & _
                    "WHERE ClientNUM = " & ClientIDRegister & _
                    " AND ClientAccountID = " & AcctNames(numTab + 1, 1) & " ORDER BY Date"
            ' set the record source for the subform in that page
            .Controls("SbfrmDetail" & numTab).Form.RecordSource = SQLstr
            ' set the on click event of the Add transaction button
            ' so that when it is clicked it opens the  AcctTransJournal form
            .Controls("BtnAddTr" & numTab).OnClick = "[Event Procedure]"  '**** THIS IS WHERE I NEED HELP
Watch Question
President / Owner
Most Valuable Expert 2017
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE