简体   繁体   中英

VBA Runtime/automation error in UserForm

I'm trying to code a dynamic/conditional userform that changes the controls on the userform based on what is entered into certain input fields of the userform. The whole idea of my VBA Excel project is to take 2 data series (say, x and y) and manipulate them according to functionalities queried by the user. The specific part of my userform that causes the problem is that part where the user can choose which type of the series he/she wants to pick for either variable (x or y).

For you to understand my issue as quickly as possible, I uploaded an excel file to the following link: http://www26.zippyshare.com/v/51719800/file.html To keep things simple, I isolated the problem causing code, cut out all other macros and limited the userform to the controls that cause the error. To run it, click on the "Initialize" button located in the top left corner of the first tab.

When initialized, the userform contains 6 controls: 2 labels, 2 comboboxes, and 2 frames. The functionality of the uploaded test file is limited: For both series (independent series (or x) and dependent series (y)) when you click on "Replacement" a number of controls should be added to the frame corresponding to the series.

The weird thing is that this works without a problem for the first(/independent) series, but for the second(/dependent) series I get

Run-time-error '-2147417848(80010108): Automation error. The object invoked has disconnected from its clients.

even though both instances call the very same procedure and in both instances the arguments passed to the function are valid. I tried the code on coworkers' laptops as well, but get the same error.

The code of the procedure that causes the error is given below. The procedure simply adds a control to a certain target frame based on arguments passed to the function. The exact line causing the crash is:

Set daFrame = dest.Controls.Add("Forms.Frame.1", name)

What is also weird is that the error only occurs for the case where we are creating a frame. Adding all the other controls eg ListBoxes, Labels, etc. (even for the second series) is not a problem.

Public Sub add_control(dest As MSForms.Frame, ht As Integer, wdt As Integer, tp As Integer, lft As Integer, typ As String, _
    name As String, Optional capt As String)

Dim daFrame As MSForms.Frame
Dim daButton As MSForms.OptionButton
Dim daList As MSForms.ListBox
Dim daLabel As MSForms.Label
Dim daBox As MSForms.ComboBox

Select Case typ
    Case "Frame"
        Set daFrame = dest.Controls.Add("Forms.Frame.1", name)
        With daFrame
            .Height = ht
            .Width = wdt
            .Top = tp
            .Left = lft
            .Caption = capt
        End With
    Case "ListBox"
        Set daList = dest.Controls.Add("Forms.Listbox.1", name)
        With daList
            .Height = ht
            .Width = wdt
            .Top = tp
            .Left = lft
        End With
    Case "OptionButton"
        Set daButton = dest.Controls.Add("Forms.OptionButton.1", name)
        With daButton
            .Height = ht
            .Width = wdt
            .Top = tp
            .Left = lft
            .Caption = capt
        End With
    Case "Label"
        Set daLabel = dest.Controls.Add("Forms.Label.1", name)
        With daLabel
            .Height = ht
            .Width = wdt
            .Top = tp
            .Left = lft
            .Caption = capt
        End With
    Case "ComboBox"
        Set daBox = dest.Controls.Add("Forms.ComboBox.1", name)
        With daBox
            .Height = ht
            .Width = wdt
            .Top = tp
            .Left = lft
        End With
End Select

End Sub

Please help me guys, I really appreciate any input. Also, if there is a way/programming style to guard against such weird behavior/erros, please let me know. Of course, I searched this forum and google for any hints, but it seems that this problem is rather code specific. In any case, I couldn't find anything helpful on the web.

Cheers

I haven't really figured out what is the real cause, but it is not the code - the real issue must be in the DepFrame. If you simply delete it and copy&rename IndFrame all works fine. But you probably figured out that already - I also wanted to tell you that you have a mistake in Dependent_change sub. You are using value of Independent instead of Dependent in the last ElseIf.

    Private Sub Dependent_Change()

    If Dependent.Value = "Replacement" Then
        DepFrame.Caption = "Replacement"
        Call rearrange_frame("Replacement2")
    ElseIf Dependent.Value = "Futures" Then
        DepFrame.Caption = "Futures"
        Call rearrange_frame("Futures2")

here: ElseIf Independent.Value = "Spread" Then

        DepFrame.Caption = "Spread"
        src = 2
    End If

    End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM