Aristomenis
Aristomenis

Reputation: 413

Event Handler for dynamically created controls causes crash in Access VBA

I have a form in which I have created some checkboxes. These checkboxes have been created dynamically, and their number may not be the same each time the user opens the form. Although my event listener works exactly in the way I want, when I press the "X" button to close the form, MS Access crashes. It does not hang, it crashes. I've tried decompiling and recompiling, and I have tried other steps too but nothing fixes the problem. When I comment the lines of code responsible for the event handler, the crashes stop.

Below is my Class Module 's code. The class module's name is EventClass.

Option Compare Database
Public WithEvents ct As Access.CheckBox

Public Sub ct_Click()
    MsgBox ct.Name & "pressed"
End Sub

Below is the Form's code:

Option Compare Database
Private listenerCollection As New Collection

Private Sub Form_Load()
    DoCmd.SetWarnings False
    Dim listener As EventClass
    For Each ctrl In Forms("test").TestTab.Pages(0).Controls
      If (TypeName(ctrl) = "CheckBox") Then
         'the created checkboxes should not be checked by default
         ctrl.Value = 0

         Set listener = New EventClass
         Set listener.ct = ctrl
         listener.ct.OnClick = "[Event Procedure]"
         listenerCollection.Add listener
      End If
   Next

End Sub

And here 's the code that makes the checkboxes (it belongs to another form,which closes before the controls are made and the form that is causing the issue shows up):

DoCmd.Close

DoCmd.OpenForm "test", acDesign, , , acFormEdit, acHidden

'delete the previously created controls
again:

For Each ctrl In Forms("test").TestTab.Pages(0).Controls
    DeleteControl "test", ctrl.Name
Next

'ensure that all the controls have been deleted
If (Forms("test").TestTab.Pages(0).Controls.Count > 0) Then GoTo again

Dim q As CheckBox
Dim usr As Label
Dim db As Database
Dim rs As Recordset
Top_ = 500
Left_ = 1000

'create new controls
Set db = CurrentDb
Set rs = db.OpenRecordset("SELECT USERNAME FROM T_USERS", dbOpenSnapshot)
With rs
    Do While Not .EOF
        Set usr = CreateControl("test", acLabel, acDetail, "Page1", , 0, Top_, 1100, 1000)
        usr.Caption = rs.Fields("USERNAME")
        Set q = CreateControl("test", acCheckBox, acDetail, "Page1", , 1500, Top_, 200, 200)

        Top_ = Top_ + 500
        Set q = Nothing
        Set usr = Nothing
        .MoveNext
    Loop
    .Close
End With
Set rs = Nothing
Set db = Nothing

'and here,the form which causes the problem shows up
DoCmd.OpenForm "test"

Crash information:

Problem signature:

Problem Event Name: APPCRASH

Application Name: MSACCESS.EXE

Application Version: 15.0.4665.1000

Application Timestamp: 54339a95

Fault Module Name: MSACCESS.EXE

Fault Module Version: 15.0.4665.1000

Fault Module Timestamp: 54339a95

Exception Code: c0000005

Exception Offset: 00000000003796f5

OS Version: 6.1.7601.2.1.0.256.48

Sometimes the application event name appears as BEX64

This problem has started making me really frustrated, as there 's no proper solution anywhere i 've searched. Code decompilation doesnt work, DEP turn-off doesnt work, references in vba editor are ok. I really dont know what to do.

Upvotes: 2

Views: 862

Answers (1)

Aristomenis
Aristomenis

Reputation: 413

I didnt actually manage to solve the problem,but it seems that it has to do with the form 's "X" button. So i did the following.I disabled the X button and inserted a custom button in my form. Below you can see its "_Click" sub.

Private Sub CloseButton_Click()
    DoCmd.Close
End Sub

The crashes stopped.

Upvotes: 1

Related Questions