Pass Userform as argument to a subroutine

  • Hi everyone,


    I'm doing a project and found a little problem (I'm very new to vba).
    I'm trying topass a userform as an argument to another subroutine, but I can't seem to do it correctly. (Run-time error '438'. Object doesn't support this property or method)



    Sub to create the userform:



    Subroutine:


    Code
    Sub GetElecInfoFromUserForm(ByRef Myform as Object)
    
    
         (...)
    
    
    End sub


    Thanks in advance

  • Re: Pass Userform as argument to a subroutine


    There is nothing wrong with the code you posted, but you are actually passing a VBComponent, not a Userform. I imagine it's the code inside the GetElecInfoFromUserForm that is actually causing the problem if it expects a Userform object.

    Rory
    Theory is when you know something, but it doesn’t work. Practice is when something works, but you don’t know why. Programmers combine theory and practice: nothing works and they don’t know why

  • Re: Pass Userform as argument to a subroutine


    You're probably right Rory.


    Here's an example of what's inside GetElecInfoFromUserForm:


    Code
    For i = LBound(ElecArr, 1) To UBound(ElecArr, 1)
        For Each cCont In Myform.Controls
       aux = "ElecNameTextBox" & CStr(i)
       If TypeName(cCont) = "TextBox" Then
          If cCont.Name = aux Then
             ElecArr(i, 1) = cCont.Text
          End If
       End If
        Next
    Next i


    Basically what I want to do is:
    - Create a userform with code (Userform has a variable number of textboxes),populated with values from a worksheet
    - View the form and possibly change some values
    - Save the changes to the Worksheet

  • Re: Pass Userform as argument to a subroutine


    Then you'll need to use Userforms.Add to actually load your new userform and then pass the variable returned by that method to your GetElecInfoFromUserForm routine.

    Rory
    Theory is when you know something, but it doesn’t work. Practice is when something works, but you don’t know why. Programmers combine theory and practice: nothing works and they don’t know why

  • Re: Pass Userform as argument to a subroutine


    I will try what you've suggested.


    In the meanwhile i've found a workaround for this situation that allows me to do what what I intend to do. I've created a command button inside the Userform code module.


    Code
    With TempForm.CodeModule
            .InsertLines .CountOfLines + 1, "Private Sub " & "CommandButton3" & "_Click()"
            .InsertLines .CountOfLines + 1, "Call GetElecInfoFromUserForm(Me)"
            .InsertLines .CountOfLines + 1, "End Sub"
    End With


    Code
    Sub GetElecInfoFromUserForm(Myform As UserForm)
       (...)
    End Sub


    Maybe it's not not the correct way to do this, but it works.
    If you have any suggestions I will apreciate. Thanks for the help Rory :)

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!