Results 1 to 14 of 14

Thread: [RESOLVED] How to call a function from module2 ?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2005
    Posts
    570

    Resolved [RESOLVED] How to call a function from module2 ?

    Hi,

    How can I call this function in my one of the 5 forms? The following code is written in module2:

    VB Code:
    1. Public Function data_disable()
    2. cmdPrevious.Enabled = False
    3. cmdFirst.Enabled = False
    4. cmdNext.Enabled = False
    5. cmdLast.Enabled = False
    6. End Function

    When I call this function in Form1 like the following:
    data_disable
    it shows error: object required.

    I have all the following command buttons in my Form1:

    cmdPrevious
    cmdFirst
    cmdNext
    cmdLast

    Seema.

  2. #2
    Lively Member
    Join Date
    Feb 2006
    Posts
    114

    Re: How to call a function from module2 ?

    you have to put Call in front of it
    VB Code:
    1. Call data_disable

  3. #3
    Addicted Member BestS's Avatar
    Join Date
    Mar 2005
    Posts
    222

    Re: How to call a function from module2 ?

    ...and as it seems to me you do not need FUNCTION here
    Public Function data_disable()
    cmdPrevious.Enabled = False
    cmdFirst.Enabled = False
    cmdNext.Enabled = False
    cmdLast.Enabled = False
    End Function

    Functions return result, but you do not have anything assigned to it. Anyway basic allows that, but in this situation use
    Public SUB
    And also seems to me that the ERROR Object Required is firing because
    YOU NEED TO SPECIFY THE FORM!!!

    With Form1
    .cmdPrevious.Enabled = False
    .cmdFirst.Enabled = False
    .cmdNext.Enabled = False
    .cmdLast.Enabled = False
    End with

    Of course - the object is missing, it is not in the MODULE, how can you call it ??

  4. #4
    Frenzied Member
    Join Date
    Aug 2005
    Posts
    1,042

    Re: How to call a function from module2 ?

    seema s:

    I think BestS has given you the solution and he/she is correct that it should be a Sub Procedure and not a Function.

    Your current code would work if you just add Form1. before each of your cmdXXX statements, but that would not be proper VB Coding.

    Your current code:
    VB Code:
    1. Public Function data_disable()
    2. cmdPrevious.Enabled = False
    3. cmdFirst.Enabled = False
    4. cmdNext.Enabled = False
    5. cmdLast.Enabled = False
    6. End Function
    would change to be:
    VB Code:
    1. Public Sub data_disable()
    2. With Form1
    3. .cmdPrevious.Enabled = False
    4. .cmdFirst.Enabled = False
    5. .cmdNext.Enabled = False
    6. .cmdLast.Enabled = False
    7. End With
    8. End Sub
    Or if you prefer you could write it like this:
    VB Code:
    1. Public Sub data_disable()
    2. Form1.cmdPrevious.Enabled = False
    3. Form1.cmdFirst.Enabled = False
    4. Form1.cmdNext.Enabled = False
    5. Form1.cmdLast.Enabled = False
    6. End Sub
    However, the code using the "With" statement would be the better way to write it.

    Then as graphixphantix stated: Use the "Call" keyword to call your Sub Procedure.

    Good Luck

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2005
    Posts
    570

    Re: How to call a function from module2 ?

    Quote Originally Posted by AIS4U
    seema s:

    I think BestS has given you the solution and he/she is correct that it should be a Sub Procedure and not a Function.

    Your current code would work if you just add Form1. before each of your cmdXXX statements, but that would not be proper VB Coding.

    Good Luck
    Thanks. But I need the same code to call in all five forms. Because all my forms have the same commands. This means should I write Sub for all five forms in a module?

    Actually what I am thinking is, if I write the code in a module will work for all 5 forms. But if I write : Form1 like the following...
    VB Code:
    1. Public Sub data_disable()
    2. With Form1
    3. .cmdPrevious.Enabled = False
    4. .cmdFirst.Enabled = False
    5. .cmdNext.Enabled = False
    6. .cmdLast.Enabled = False
    7. End With
    8. End Sub

    Then I must write the other Sub for Form2:
    With Form2
    .cmdPrevious.Enabled = False
    .cmdFirst.Enabled = False
    .cmdNext.Enabled = False
    .cmdLast.Enabled = False
    End With
    End Sub[/Highlight]

    This means I have to write Sub for all my 5 forms. Then what is the use writing in a module I can do that in the same form also.

    Please clear my doubt.

    Seema.

  6. #6
    Addicted Member BestS's Avatar
    Join Date
    Mar 2005
    Posts
    222

    Re: How to call a function from module2 ?

    Write only one with parameter:
    Last edited by BestS; Apr 9th, 2006 at 06:16 AM.

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2005
    Posts
    570

    Re: How to call a function from module2 ?

    Quote Originally Posted by BestS
    Write only one with parameter:
    Could you please tell me how can I write using with parameters?

    Seema.

  8. #8
    Addicted Member BestS's Avatar
    Join Date
    Mar 2005
    Posts
    222

    Re: How to call a function from module2 ?

    That is what I am thinking now, give me some more minutes....

  9. #9
    Addicted Member BestS's Avatar
    Join Date
    Mar 2005
    Posts
    222

    Re: How to call a function from module2 ?

    Put this in your module
    Public Function FormByName(p_sName As String) As Form
    Dim oForm As Form
    For Each oForm In Forms
    If LCase$(oForm.Name) = LCase$(p_sName) Then
    Set FormByName = oForm
    Exit For
    End If
    Next
    End Function

    Then
    Public Sub data_disable(frmName as string)
    With FormByName(frmName)
    .cmdPrevious.Enabled = False
    .cmdFirst.Enabled = False
    .cmdNext.Enabled = False
    .cmdLast.Enabled = False
    End With
    End Sub

    And you call this SUB like this

    call Data_Disable("Form1") ' "form2"...etc

  10. #10
    Addicted Member BestS's Avatar
    Join Date
    Mar 2005
    Posts
    222

    Re: How to call a function from module2 ?

    Or if you like this one

    Function GetForm(formName As String) As Form
    Dim frm As Form
    For Each frm In Forms
    If StrComp(frm.Name, formName, vbTextCompare) = 0 Then
    Set GetForm = frm
    Exit Function
    End If
    Next
    End Function

    Then again ...
    With GetForm(frmName) ....

  11. #11
    Frenzied Member
    Join Date
    Aug 2005
    Posts
    1,042

    Re: How to call a function from module2 ?

    seema s:

    You might be able to make your module a class module, but if "I need the same code to call in all five forms. Because all my forms have the same commands" perhaps there is a better way to design your application.

    I don't have any idea what your application is supposed to do, but it seems kind of unusual that you would have five different forms that all have Previous, First, Next, and Last buttons unless you are duplicating some particular activity. And if you are duplicating some particular activity then there would be a better way to design your application.

    Perhaps BestS' parameter suggestion will work out, but I don't see how.

    I think you may have to dump your module, change your Public Sub to Private Sub and include that block of code in each form.

    Sorry I don't have a quick and easy solution for you.

    Good Luck

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2005
    Posts
    570

    Re: How to call a function from module2 ?

    Quote Originally Posted by BestS
    Put this in your module
    call Data_Disable("Form1") ' "form2"...etc
    Thanks a lot for the help.

    Regards.

    Seema.

  13. #13
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: How to call a function from module2 ?

    Here's an alternative version of BestS's code, which is a bit quicker and doesn't require the FormByName function (and can also cope with MDI child windows having the same name):
    VB Code:
    1. Public Sub data_disable(TheForm as Form)
    2.   With TheForm
    3.     .cmdPrevious.Enabled = False
    4.     .cmdFirst.Enabled = False
    5.     .cmdNext.Enabled = False
    6.     .cmdLast.Enabled = False
    7.   End With
    8. End Sub
    9.  
    10. 'usage (a) - specific code for each form:
    11. Call Data_Disable(Form1)
    12.  
    13. 'usage (b) - same code can be used in any form:
    14. Call Data_Disable(Me)

  14. #14

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2005
    Posts
    570

    Re: How to call a function from module2 ?

    Quote Originally Posted by si_the_geek
    Here's an alternative version of BestS's code, which is a bit quicker and doesn't require the FormByName function (and can also cope with MDI child windows having the same name):
    VB Code:
    1. Public Sub data_disable(TheForm as Form)
    2.   With TheForm
    3.     .cmdPrevious.Enabled = False
    4.     .cmdFirst.Enabled = False
    5.     .cmdNext.Enabled = False
    6.     .cmdLast.Enabled = False
    7.   End With
    8. End Sub
    9.  
    10. 'usage (a) - specific code for each form:
    11. Call Data_Disable(Form1)
    12.  
    13. 'usage (b) - same code can be used in any form:
    14. Call Data_Disable(Me)
    Wow! Wonderful. Thanks a lot for the help.

    Seema.

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