How to Write Onclick Event for dynamically created buttons using VBA in MS Access

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Maya16
    New Member
    • Apr 2013
    • 7

    How to Write Onclick Event for dynamically created buttons using VBA in MS Access

    I am stuck with a Event Procedure Issue. I have created one Dynamic report in MS access using VBA. For the same I have created some dynamic controls like lables, Textbox, buttons, etc. I am able to create report successfully. The only issue is that I don't know how to create "Onclick" Event procedure for Dynamically created button. Every Time I click on that button it gives me an errors which says " No such Event found" kind off.
    Here is My Code...

    Code:
    Private Sub btn_createReport()
    
    Dim btnNew As Access.CommandButton
    Dim rpt As Report ' hold report object
    
    cmdSelect = "select * from xyz;"
    Set rpt = CreateReport
    
    ' set properties of the Report
    With rpt
    .Width = 12500
    .RecordSource = cmdSelect
    .Caption = title
    End With
    
    
    
    Set btnNew = CreateReportControl(rpt.Name, acCommandButton, acPageHeader, , , 7000, 50, 1500, 400)
    btnNew.Caption = "Back"
    btnNew.Name = "btnNew" 
    
    ' btnNew.OnClick = "[btn_Click]"
    'lblNew.FontUnderline = True
    lblNew.SizeToFit
    
    End Sub
    
    Private Sub btnNew_Click()
    DoCmd.OpenForm Form1, acNormal, , , , acWindowNormal
    MsgBox "Hello"
    End Sub
    Last edited by Rabbit; Apr 18 '13, 07:12 AM. Reason: Please use code tags when posting code.
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    I missed the Report part because I am running out the door, but here is how you can create an Event Procedure (Click()) for a Control (Command Button) named cmdDemo on the Current Form:
    Code:
    Dim ctl As Control
    Dim mdl As Module
    Dim frm As Form
    
    Set ctl = Me![cmdDemo]      'Set a Reference to the Command Button
    Set frm = Me                'Set a reference to the Form
    Set mdl = Me.Module         'Set a Reference to the Form's Code Module
    
    'Create a Click() Event Procedure for the Command Button cmdDemo
    lngReturn = mdl.CreateEventProc("Click", ctl.Name)
    
    'Insert a single Line of Code in the Event Procedure
    mdl.InsertLines lngReturn + 1, "Msgbox ""Create Procedure Demo"""

    Comment

    • Maya16
      New Member
      • Apr 2013
      • 7

      #3
      Thnx For The Reply....

      Comment

      Working...