Avatar of jpav
jpav

asked on 

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

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
     Else
        '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
Microsoft Access

Avatar of undefined
Last Comment
jpav

8/22/2022 - Mon