-
Apr 9th, 2006, 12:39 AM
#1
Thread Starter
Fanatic Member
[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:
Public Function data_disable()
cmdPrevious.Enabled = False
cmdFirst.Enabled = False
cmdNext.Enabled = False
cmdLast.Enabled = False
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.
-
Apr 9th, 2006, 01:41 AM
#2
Lively Member
Re: How to call a function from module2 ?
you have to put Call in front of it
-
Apr 9th, 2006, 02:17 AM
#3
Addicted Member
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 ??
-
Apr 9th, 2006, 03:09 AM
#4
Frenzied Member
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:
Public Function data_disable()
cmdPrevious.Enabled = False
cmdFirst.Enabled = False
cmdNext.Enabled = False
cmdLast.Enabled = False
End Function
would change to be:
VB Code:
Public Sub data_disable()
With Form1
.cmdPrevious.Enabled = False
.cmdFirst.Enabled = False
.cmdNext.Enabled = False
.cmdLast.Enabled = False
End With
End Sub
Or if you prefer you could write it like this:
VB Code:
Public Sub data_disable()
Form1.cmdPrevious.Enabled = False
Form1.cmdFirst.Enabled = False
Form1.cmdNext.Enabled = False
Form1.cmdLast.Enabled = False
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
-
Apr 9th, 2006, 05:25 AM
#5
Thread Starter
Fanatic Member
Re: How to call a function from module2 ?
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:
Public Sub data_disable()
With Form1
.cmdPrevious.Enabled = False
.cmdFirst.Enabled = False
.cmdNext.Enabled = False
.cmdLast.Enabled = False
End With
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.
-
Apr 9th, 2006, 06:02 AM
#6
Addicted Member
Re: How to call a function from module2 ?
Write only one with parameter:
Last edited by BestS; Apr 9th, 2006 at 06:16 AM.
-
Apr 9th, 2006, 06:19 AM
#7
Thread Starter
Fanatic Member
Re: How to call a function from module2 ?
Originally Posted by BestS
Write only one with parameter:
Could you please tell me how can I write using with parameters?
Seema.
-
Apr 9th, 2006, 06:24 AM
#8
Addicted Member
Re: How to call a function from module2 ?
That is what I am thinking now, give me some more minutes....
-
Apr 9th, 2006, 06:37 AM
#9
Addicted Member
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
-
Apr 9th, 2006, 06:50 AM
#10
Addicted Member
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) ....
-
Apr 9th, 2006, 06:55 AM
#11
Frenzied Member
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
-
Apr 9th, 2006, 07:04 AM
#12
Thread Starter
Fanatic Member
Re: How to call a function from module2 ?
Originally Posted by BestS
Put this in your module
call Data_Disable("Form1") ' "form2"...etc
Thanks a lot for the help.
Regards.
Seema.
-
Apr 9th, 2006, 11:10 AM
#13
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:
Public Sub data_disable(TheForm as Form)
With TheForm
.cmdPrevious.Enabled = False
.cmdFirst.Enabled = False
.cmdNext.Enabled = False
.cmdLast.Enabled = False
End With
End Sub
'usage (a) - specific code for each form:
Call Data_Disable(Form1)
'usage (b) - same code can be used in any form:
Call Data_Disable(Me)
-
Apr 9th, 2006, 11:32 AM
#14
Thread Starter
Fanatic Member
Re: How to call a function from module2 ?
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:
Public Sub data_disable(TheForm as Form)
With TheForm
.cmdPrevious.Enabled = False
.cmdFirst.Enabled = False
.cmdNext.Enabled = False
.cmdLast.Enabled = False
End With
End Sub
'usage (a) - specific code for each form:
Call Data_Disable(Form1)
'usage (b) - same code can be used in any form:
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|