Tags: access, assign, button, command, create, event, form, microsoft, msdn, office, onclick, procedure, programmatically, running, software, vba

Access vba: how to programmatically create form with onclick event

On Microsoft » Microsoft Office Miscellaneous

7,003 words with 5 Comments; publish: Tue, 04 Dec 2007 06:58:00 GMT; (300109.38, « »)

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:

Dim frmWarning As Form

Dim btnOK As CommandButton

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

End With

Set btnOK = CreateControl(frmWarning.Name, acCommandButton, acDetail, , , 1500, 2700, 950, 450)

btnOK.Caption = "OK"

btnOK.OnClick = "[Event Procedure]" 'Here is the problem

DoCmd.OpenForm frmWarning.Name, acNormal

DoCmd.Restore

DoCmd.MoveSize 6000, 4000, 5500, 1800

'frmWarning.SetFocus

'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

All Comments

Leave a comment...

  • 5 Comments
    • 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.

      #1; Thu, 06 Dec 2007 11:32:00 GMT
    • 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?

      Option Compare Database

      Option Explicit

      public withevents btnOK 'States I need an AS clause, do not know how to do it.

      Public Function btnOK_Click()

      MsgBox "Here is where it does some stuff"

      End Function

      Private Sub Command0_Click()

      Dim frmWarning As Form

      Dim btnOK As CommandButton

      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

      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

      'frmWarning.SetFocus

      'frmWarning.Move 400, 400, 4000, 4000

      End Sub

      Thanks,

      Ranthalion

      #2; Thu, 06 Dec 2007 11:33:00 GMT
    • 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:

      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

      #3; Thu, 06 Dec 2007 11:34:00 GMT
    • 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.
      #4; Thu, 06 Dec 2007 11:35:00 GMT
    • 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

      #5; Thu, 06 Dec 2007 11:36:00 GMT