troubleshooting Question

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

Avatar of jpav
jpav asked on
Microsoft Access
2 Comments1 Solution396 ViewsLast Modified:
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
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros