Results 1 to 7 of 7

Thread: Passing a control to a module

  1. #1

    Thread Starter
    Addicted Member Gavin_Mannion's Avatar
    Join Date
    Aug 2001
    Location
    London, UK
    Posts
    214

    Question Passing a control to a module

    Hi All,

    Just a quick question.

    I have a module which I wish to pass the name of a control to so that it can influence that control. How would I go about doing this?

    What I have at the moment is

    ::Form::
    Private Sub Command2_Click()
    `ButtonChange is the module
    `test is the function
    ButtonChange.test (Form2.Command2)
    End Sub

    ::Module::
    Public Function test(cbutton)
    cbutton.caption = "test"
    End Function

    So basically what I want it to do is to change the caption of form2.command2 to "test".

    I get the error message of "object required" but it is not making any sense to me.

    Thanks,
    Gavin


  2. #2
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     SetText Text1, "This is a test"
    5. End Sub
    6.  
    7. Private Sub SetText(txtBox As TextBox, txt As String)
    8.     txtBox.Text = txt
    9. End Sub
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  3. #3
    Lively Member mrdarkwarez's Avatar
    Join Date
    Feb 2000
    Location
    Australia
    Posts
    113
    asd
    'in module

    public sub ChangeCommandNamne(aButton as vbButton, strName as String)

    aButton.caption = strName

    end sub




    'in form
    call ChangeCommandNamne(me.command1, "a new name")




    hope it works

  4. #4

    Thread Starter
    Addicted Member Gavin_Mannion's Avatar
    Join Date
    Aug 2001
    Location
    London, UK
    Posts
    214
    You have lost me a bit here,

    Isn't this code for a Textbox? I am looking for how to change the caption on a button, or better yet change any details of a button.

    My function now looks like this

    Public Function test(cbutton As CommandButton)
    cbutton.Caption = "test"
    End Function

    But when I call is using

    ButtonChange.test (Form2.Command2)

    I get "Type Mismatch"

    Why

    Thanks,
    Gavin

  5. #5

    Thread Starter
    Addicted Member Gavin_Mannion's Avatar
    Join Date
    Aug 2001
    Location
    London, UK
    Posts
    214
    Originally posted by mrdarkwarez
    asd
    'in module

    public sub ChangeCommandNamne(aButton as vbButton, strName as String)
    When I run this I get "User defined type not defined", Where would I have to define this?

    Thanks,
    Gavin

  6. #6
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    hi
    VB Code:
    1. 'Form level
    2. Private Sub Command1_Click()
    3.     ChangeCaption Command1
    4. End Sub
    5.  
    6. 'Module
    7. Sub ChangeCaption(lCommand As CommandButton)
    8.     lCommand.Caption = "Changed"
    9. End Sub
    10.  
    11. 'I dont think u need a function cos there is no return value
    Regards
    Stuart
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  7. #7
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Originally posted by Gavin_Mannion
    You have lost me a bit here,

    Isn't this code for a Textbox? I am looking for how to change the caption on a button, or better yet change any details of a button.
    Yes the code I posted is for a textbox, but is easily changed to a commandbutton :

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     SetText Command1, "This is a test"
    5. End Sub
    6.  
    7. Private Sub SetText(cmdBtn As CommandButton, txt As String)
    8.     cmdBtn.Caption = txt
    9. End Sub
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

Posting Permissions

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



Click Here to Expand Forum to Full Width