Consulting

Results 1 to 7 of 7

Thread: Pass Params on Activate event

  1. #1
    VBAX Regular
    Joined
    Aug 2019
    Posts
    54
    Location

    Pass Params on Activate event

    Hi everyone. How to pass some params in an activate event procedure? For example , in the Forms module i have this statement "Sub UserForm_Activate (param1,param2)" , now how can i call this procedure and pass the params from another module?
    Thank you for your time.

    Regards Kostas

  2. #2
    VBAX Master paulked's Avatar
    Joined
    Apr 2006
    Posts
    1,007
    Location
    Code module
    Option Explicit
    
    
    Public txt1 As String, txt2 As String
    
    
    Sub FrmLd()
        txt1 = "Hello"
        txt2 = "World"
        UserForm1.Show
    End Sub
    Userform module
    Option Explicit
    
    Private Sub UserForm_Activate()
        TextBox1 = txt1
        TextBox2 = txt2
    End Sub
    Semper in excretia sumus; solum profundum variat.

  3. #3
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,642
    You can't have arguments in Private Sub UserForm_Activate()

    Why do you need 'arguments' in Private Sub UserForm_Activate() ?
    I seldom use Private Sub UserForm_Activate().
    Use Private Sub UserForm_Initialize() instead.


    @Paulked

    Avoid Public variables.
    In the Userform module

    Dim c00
    Private Sub UserForm_Inirialize()
       c00="snb's suggestion"
    End Sub
    
    Private Sub UserForm_Activate()
       msgbox c00
    Edn Sub

  4. #4
    VBAX Master paulked's Avatar
    Joined
    Apr 2006
    Posts
    1,007
    Location
    I assumed he wanted to pass variables from his sub to the userform on showing it. But I agree to avoid public var's wherever possible. I could be done with Tag:

    'Code module
    Sub FrmLd()
        UserForm1.Tag = "Hello World"
        UserForm1.Show
    End Sub
    
    
    'Userform module
    Private Sub UserForm_Activate()
        Dim txt
        txt = Split(Tag, " ")
        TextBox1 = txt(0)
        TextBox2 = txt(1)
    End Sub
    Semper in excretia sumus; solum profundum variat.

  5. #5
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,642
    If you use one of the userform's properties I'd prefer:

    Private Sub UserForm_Inirialize()
       Tag="snb's suggestion"
    End Sub
    
    Private Sub UserForm_Activate()
       msgbox Tag
    Edn Sub

  6. #6
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    The proper Method is to use User Defined Form Properties.

    UserForm Code
    Option Explicit
    Dim fParam1 As Something
    Dim fParam2 as something
    '  It is customary to prefix these variables with "f" or "p" for Form or Property variables
    '  In Class modules, it is customary to prefix with "m", "c", or "p" for Class Module or Property Variable
    '  In Sheet Modules, use "s" or "p" as the prefix. Some coders like to add an underscore: "p_VarName".
    '  Whatever style you use, use it in all similar Property Variables
    
    Property Let Param1(Var as Something)
       fParam1 = Var
       'Do something with fParam1
       'Or call some sub
    End Property
    
    Property let Param2(var as something)
       fParam2 = Var
       Init_Me
    End Property
    
    Private Sub Init_Me()
    If fParam1 <> "" And fPAram2 <> "" Then
       'Do Something with fparam1 and fParam2
    End Sub
    Other module code
    Load Userform1
    '   Triggers Sub UserForm_initialize
    
    UserForm1.Param1 = something 'Form must be loaded so Property Lets are available
    UserForm1.Param2 = Something
    
    UserForm1.Show
    '   Triggers Sub UserForm_Activate
    Last edited by SamT; 11-20-2019 at 10:56 AM.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  7. #7
    VBAX Regular
    Joined
    Aug 2019
    Posts
    54
    Location
    OK thank you very much for your answer

Posting Permissions

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