Results 1 to 6 of 6

Thread: Access vba: how to programmatically create form with onclick event

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    33

    Access vba: how to programmatically create form with onclick event

    Hi all,

    I'm trying to programmatically create a form, add a command button, and assign an event procedure for the button. I'm running into a problem with adding the event procedure.

    Here is the code I currently have:

    VB Code:
    1. Dim frmWarning As Form
    2.     Dim btnOK As CommandButton
    3.    
    4.     Set frmWarning = CreateForm
    5.     With frmWarning
    6.         .Caption = "Warning"
    7.         .AllowFormView = True
    8.         .PopUp = True
    9.         .ScrollBars = 0 'no scrollbars
    10.         .RecordSelectors = False
    11.         .NavigationButtons = False
    12.         .DividingLines = False
    13.         .Modal = True
    14.         .Width = 4000
    15.         .GridX = 40
    16.         .GridY = 40
    17.         .Moveable = True
    18.         .AutoCenter = True
    19.         .CloseButton = False
    20.         .ControlBox = False
    21.     End With
    22.    
    23.     Set btnOK = CreateControl(frmWarning.Name, acCommandButton, acDetail, , , 1500, 2700, 950, 450)
    24.     btnOK.Caption = "OK"
    25.     btnOK.OnClick = "[Event Procedure]"     'Here is the problem
    26.            
    27.     DoCmd.OpenForm frmWarning.Name, acNormal
    28.    
    29.     DoCmd.Restore
    30.     DoCmd.MoveSize 6000, 4000, 5500, 1800
    31.    
    32.     'frmWarning.SetFocus
    33.     'frmWarning.Move 400, 400, 4000, 4000

    I read in msdn that I should have "[Event Procedure]" as the OnClick property, but I can't figure out how to tell it which function to run.

    Does anyone have any ideas on this?

    Thanks,
    Ranthalion

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Access vba: how to programmatically create form with onclick event

    You will also need to have a Public WithEvents btnOK declaration at the top of the class module that is behind the form that you also need to add.

    It would be called btnOK_Click procedure.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    33

    Re: Access vba: how to programmatically create form with onclick event

    Thanks for your reply. I'm not sure I understand what you mean.
    I have a form which has code in its module as follows. When you click on Command0 on the actual form, it should generate the new form, which should pop up the message box when the ok button is pressed.
    I'm not sure on where to put the withevents statement or how to code it. Can you clarify, please?

    VB Code:
    1. Option Compare Database
    2. Option Explicit
    3.  
    4. public withevents btnOK 'States I need an AS clause, do not know how to do it.
    5.  
    6. Public Function btnOK_Click()
    7.     MsgBox "Here is where it does some stuff"
    8. End Function
    9.  
    10. Private Sub Command0_Click()
    11.     Dim frmWarning As Form
    12.     Dim btnOK As CommandButton
    13.    
    14.     Set frmWarning = CreateForm
    15.     With frmWarning
    16.         .Caption = "Warning"
    17.         .AllowFormView = True
    18.         .PopUp = True
    19.         .ScrollBars = 0 'no scrollbars
    20.         .RecordSelectors = False
    21.         .NavigationButtons = False
    22.         .DividingLines = False
    23.         .Modal = True
    24.         .Width = 4000
    25.         .GridX = 40
    26.         .GridY = 40
    27.         .Moveable = True
    28.         .AutoCenter = True
    29.         .CloseButton = False
    30.         .ControlBox = False
    31.     End With
    32.    
    33.     Set btnOK = CreateControl(frmWarning.Name, acCommandButton, acDetail, , , 1500, 2700, 950, 450)
    34.     btnOK.Caption = "OK"
    35.     btnOK.OnClick = "[Event Procedure]"
    36.            
    37.     DoCmd.OpenForm frmWarning.Name, acNormal
    38.    
    39.     DoCmd.Restore
    40.     DoCmd.MoveSize 6000, 4000, 5500, 1800
    41.    
    42.     'frmWarning.SetFocus
    43.     'frmWarning.Move 400, 400, 4000, 4000
    44.    
    45.        
    46. End Sub

    Thanks,
    Ranthalion

  4. #4

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    33

    Re: Access vba: how to programmatically create form with onclick event

    OK,
    I've been working on this and still haven't really come up with any progress. I've read about the withevents keyword, and found some examples of its use with VBA, and I thought I understood how it should work. I have a form with a commandButton on it. The commandButton has an event procedure which should create a new form and generate a commandbutton on the new form. The second form is generated just fine, but it doesn't respond to any of the events I try to define for it. Here is the current version of my code:

    Code:
    Option Compare Database
    Option Explicit
    
    Public WithEvents frmWarning As Form
    Public WithEvents btnOK As CommandButton
    
    
    Public Sub btnOK_Click()
        MsgBox "hello there"
    End Sub
    
    
    Public Sub frmWarning_Load()
        MsgBox "load"
    End Sub
    
    
    Private Sub Command0_Click()
    On Error GoTo Err_Command0_Click
        
        Set frmWarning = CreateForm
        With frmWarning
            .Caption = "Warning"
            .AllowFormView = True
            .PopUp = True
            .ScrollBars = 0 'no scrollbars
            .RecordSelectors = False
            .NavigationButtons = False
            .DividingLines = False
            .Modal = True
            .Width = 4000
            .GridX = 40
            .GridY = 40
            .Moveable = True
            .AutoCenter = True
            .CloseButton = False
            .ControlBox = False
            .OnLoad = "[Event Procedure]"
        End With
        
        Set btnOK = CreateControl(frmWarning.Name, acCommandButton, acDetail, , , 1500, 2700, 950, 450)
        btnOK.Caption = "OK"
        btnOK.OnClick = "[Event Procedure]"
        
               
        DoCmd.OpenForm frmWarning.Name, acNormal
        
        DoCmd.Restore
        DoCmd.MoveSize 6000, 4000, 5500, 1800
        
    Exit_Command0_Click:
        Exit Sub
        
    Err_Command0_Click:
        MsgBox (Err.Number & ": " & Err.Description)
        GoTo Exit_Command0_Click
            
    End Sub
    This is contained the module connected to the main form. It is called Form_Form1. I did not create a class module for the second form. As far as I have learned, the above code should work fine, but the events are never fired when the second form load, nor when I press the command button on the second form (btnOK). Any ideas or advice on what I need to do to get the events to fire correctly, or to be able to handle them?

    Thanks,
    Ranthalion

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Access vba: how to programmatically create form with onclick event

    If you manually create one and check out how its designed, you will see that it (btnOk) may have to be in the class file of the form in order for the [Event Procedure] to link up.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    33

    Re: Access vba: how to programmatically create form with onclick event

    Thank you very much for your help RobDog.
    I tried creating a class for my form and putting the btnOK code and form creation code in the class module, then declaring a variable of my class' type from the form Module, but I still couldn't get it to work.

    I've looked online for some examples of using the withevents keyword and the onClick property, but I haven't come across any good tutorials on it.

    The purpose of my project is to have everything contained on one form. Other Access database designers would import my form and set it to show up in the OnOpen event of their startup form. My imported form is hidden and contains a timer that will generate a form dynamically from code.

    -Ranthalion

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width