|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Call Sub!A VB6 application has 2 Forms - frm1 & frm2. Assume that frm1 has a
sub-routine named Calculate() that gets invoked when the user clicks any of the various CommandButtons that frm1 encapsulates. Now I want to call this sub-routine from the second Form i.e. frm2. How do I do it? Thanks, Arpan "Arpan" <arpan***@hotmail.com> wrote in message Make the definition in frm1 public: Public Sub Calculate(...)news:1128717353.845829.179500@g44g2000cwa.googlegroups.com > A VB6 application has 2 Forms - frm1 & frm2. Assume that frm1 has a > sub-routine named Calculate() that gets invoked when the user clicks > any of the various CommandButtons that frm1 encapsulates. Now I want > to call this sub-routine from the second Form i.e. frm2. How do I do > it? from frm2 use: Call frm1.Calculate(...) -or- move it to a BAS code module, make it public there and call it directly from both forms -- Reply to the group so all can participate VB.Net: "Fool me once..." "Arpan" <arpan***@hotmail.com> wrote in news:1128717353.845829.179500 @g44g2000cwa.googlegroups.com:> A VB6 application has 2 Forms - frm1 & frm2. Assume that frm1 has a Arpan,> sub-routine named Calculate() that gets invoked when the user clicks > any of the various CommandButtons that frm1 encapsulates. Now I want to > call this sub-routine from the second Form i.e. frm2. How do I do it? > > Thanks, > > Arpan > > I think there may be two ways of doing this. The way I would do it is to include Calculate() in a module common to all so it could be called from within any form. It might be possible if form1 is still active and form2 is called over form1, to from form2 do form1.calculate. Arpan wrote:
> A VB6 application has 2 Forms - frm1 & frm2. Assume that frm1 has a Easiest method is to place it in a BAS module with a Public scope modifier.> sub-routine named Calculate() that gets invoked when the user clicks > any of the various CommandButtons that frm1 encapsulates. Now I want to > call this sub-routine from the second Form i.e. frm2. How do I do it? > > Thanks, > > Arpan > Public Sub Calc() ... End Sub Otherwise you need to make it a Public Sub hanging off the form (which is not recommended) and then call it using the formName.fnName() syntax. The two would use the same identical code, but by putting it into a BAS module you can simply call it by it's fnName without specifying the moduleName first. You are going to need to reference information from the first form I assume... You should pass that information into the sub instead of using references to the controls directly in the Sub. Public Sub Calc(amt1 as double, amt2 as double) ... End Sub Lastly, I would recommend figuring out how to return the values of your calculation by changing this to a function. Public Function Calc(amt1 as double, amt2 as double) as double ... End Function If you need to return more than one value you could consider passing in another value that tells you which type of value to return (i.e. "SubTotal" or "GrandTotal"), and then handle what to do inside of the Calc function. You could alternately make several functions to perform the diferent calcs, or even create a class that you initialize with the appropriate values, and then call new methods or interrogate propery values for your answers... Many ways to get the same thing done here... hth, D. Thanks all of you for your input.
I created a BAS module & made the Calculate() sub-routine Public. This is a part of the code in the BAS module: Public Sub Calculate() If (txtOperator.Text = "+") Then txtOutput.Text = CDbl(txtNum1.Text) + CDbl(txtNum2.Text) ElseIf (txtOperator.Text = "-") Then txtOutput.Text = CDbl(txtNum1.Text) - CDbl(txtNum2.Text) .................. .................. .................. End If End Sub & this is how I am invoking it from the 2 Forms (the following sub-routine exists in both the Forms): Private Sub cmdEgt_Click() .................. .................. .................. Calculate End Sub But when I run the application, irrespective of whichever Form I am in, clicking any CommandButton generates the "Object Required" error. So I qualified the object names in the BAS module with frm1: Public Sub Calculate() If (frm1.txtOperator.Text = "+") Then frm1.txtOutput.Text = CDbl(frm1.txtNum1.Text) + CDbl(frm1.txtNum2.Text) ElseIf (frm1.txtOperator.Text = "-") Then frm1.txtOutput.Text = CDbl(frm1.txtNum1.Text) - CDbl(frm1.txtNum2.Text) .................. .................. .................. End If End Sub but since I have qualified the object names with frm1 only, when I am in frm1, everything works fine but when I am in frm2, as expected, it doesn't work out. So how do I qualify the object names in the BAS module with the 2 Form names within this Calculate sub-routine in the BAS module? Thanks once again, Regards, Arpan Arpan wrote:
Show quoteHide quote > I would pass them as arguments to the procedure...then the value from> Thanks all of you for your input. > > I created a BAS module & made the Calculate() sub-routine Public. This > is a part of the code in the BAS module: > > Public Sub Calculate() > If (txtOperator.Text = "+") Then > txtOutput.Text = CDbl(txtNum1.Text) + CDbl(txtNum2.Text) > ElseIf (txtOperator.Text = "-") Then > txtOutput.Text = CDbl(txtNum1.Text) - CDbl(txtNum2.Text) > .................. > .................. > .................. > End If > End Sub > > & this is how I am invoking it from the 2 Forms (the following > sub-routine exists in both the Forms): > > Private Sub cmdEgt_Click() > .................. > .................. > .................. > Calculate > End Sub > > But when I run the application, irrespective of whichever Form I am in, > clicking any CommandButton generates the "Object Required" error. So I > qualified the object names in the BAS module with frm1: > > Public Sub Calculate() > If (frm1.txtOperator.Text = "+") Then > frm1.txtOutput.Text = CDbl(frm1.txtNum1.Text) + > CDbl(frm1.txtNum2.Text) > ElseIf (frm1.txtOperator.Text = "-") Then > frm1.txtOutput.Text = CDbl(frm1.txtNum1.Text) - > CDbl(frm1.txtNum2.Text) > .................. > .................. > .................. > End If > End Sub > > but since I have qualified the object names with frm1 only, when I am > in frm1, everything works fine but when I am in frm2, as expected, it > doesn't work out. > > So how do I qualify the object names in the BAS module with the 2 Form > names within this Calculate sub-routine in the BAS module? the proper control from the proper form is used w/o additional referencing "Arpan" <arpan***@hotmail.com> wrote in message divorce the common sub from the form-specific controls with something like:news:1128721747.845869.141420@g47g2000cwa.googlegroups.com > Thanks all of you for your input. > > I created a BAS module & made the Calculate() sub-routine Public. This > is a part of the code in the BAS module: > > Public Sub Calculate() > If (txtOperator.Text = "+") Then > txtOutput.Text = CDbl(txtNum1.Text) + CDbl(txtNum2.Text) Public Function Calculate(byval Operand1 As Double, _ byval Operand2 As Double,byval Operator As String) As Double Select Case Operator Case "+": Calculate = Operand1 + Operand2 Case "-": Calculate=Operand1 - Operand2 <etc> End Select End Function From the forms: txtOutput.text=cstr(calculate(cdbl(txtNum1.text),cdbl(txtNum2.text),"+")) -- Reply to the group so all can participate VB.Net: "Fool me once..."
Show quote
Hide quote
"Arpan" <arpan***@hotmail.com> wrote in message Does this 2nd form have those same controls with the same names (as the news:1128721747.845869.141420@g47g2000cwa.googlegroups.com... > > I created a BAS module & made the Calculate() sub-routine Public. This > is a part of the code in the BAS module: > > & this is how I am invoking it from the 2 Forms (the following > sub-routine exists in both the Forms): > > > But when I run the application, irrespective of whichever Form I am in, > clicking any CommandButton generates the "Object Required" error. So I > qualified the object names in the BAS module with frm1: > > but since I have qualified the object names with frm1 only, when I am > in frm1, everything works fine but when I am in frm2, as expected, it > doesn't work out. > > So how do I qualify the object names in the BAS module with the 2 Form > names within this Calculate sub-routine in the BAS module? first form has)? If so, then maybe the best thing for you to do is just create a 2nd instance of the first form (and not have frm2 at all). If the frm1 file is not too big, maybe you can post it? If so, open it in Notepad and copy and paste the whole thing. Another thought. Does frm2 provide any values at all used in this Calculate sub? You might want to pass these values individually to Calculate as separate arguments, or, another option would be to pass a reference to the form. For example (AIR CODE based on your posted code): Public Sub Calculate(ByRef frm As Form) If (frm.txtOperator.Text = "+") Then frm.txtOutput.Text = CDbl(frm.txtNum1.Text) + CDbl(frm.txtNum2.Text) ElseIf (frm.txtOperator.Text = "-") Then frm.txtOutput.Text = CDbl(frm.txtNum1.Text) - CDbl(frm.txtNum2.Text) .................. .................. .................. End If End Sub In the CommandButton_Click event of each form, you'd call this as such: Calculate Me or Call Calculate(Me) Now, of course this means that both forms need those same controls and named the same as well....which goes back to that maybe the best thing for you to do is just create a 2nd instance of frm1. -- Mike Microsoft MVP Visual Basic This is what I did finally (this is the code in the BAS Module):
Public Function Calculate(frmName As Form) If (frmName.txtOperator.Text = "+") Then frmName.txtOutput.Text = CDbl(frmName.txtNum1.Text) + CDbl(frmName.txtNum2.Text) ElseIf (frmName.txtOperator.Text = "-") Then frmName.txtOutput.Text = CDbl(frmName.txtNum1.Text) - CDbl(frmName.txtNum2.Text) .................. .................. End If End Sub & then made the 2 Forms invoke it in the following way: Private Sub cmdButton1_Click() 'frm1 .................. .................. Calculate frm1 End Sub Private Sub cmdButton1_Click() 'frm2 .................. .................. Calculate frm2 End Sub Though the above serves my purpose, is it an efficient approach? If not, then why? Thanks to all of you for investing your precious time & energy in guiding me. I have got to honestly confess one thing about this NewsGroup - apart from this NewsGroup, I use other Microsoft (Google) NewsGroups as well (like ASP, SQL Server, ASP.NET etc..) but without doubt, this NewsGroup is the BEST amongst all especially because of their attitude towards newbies like me (unlike other NewsGroups where experts are looking out for an opportunity to ridicule newbies & those people who, according to the experts, have asked silly questions) & of course, it goes without saying all the help & guidance the experts & MVPs of this NewsGroup provide! Cheers! Regards, Arpan ( see attached zip file [10k] Source only, no exe included )
I was feeling like an insomniac so I started building a house of bricks for you... theres tons of ways you could do this and mine is a bit overkill but it does demonstrate that if you build something to be re-usable and as long as you keep your abstractions simple, you'll end up with a component that you can test once and use in many places. It should also be fairly easy to extend this code to do multiplication, division or whatever else you can do with two numbers. I did not comment the code but I tried to use enumerations that would be easy to read so it should be fairly self documenting. I used the MZ Tools VB6 plug in to generate the procedure headers, error handlers, property functions and ultimately the docs.xml that you can display in IE. http://www.mztools.com/v3/download.htm I'm guessing you've never written a class before so you'll have questions... just come back and ask for anything that you need. D. "Arpan" <arpan***@hotmail.com> wrote in message That's fine but it limits the sub to use by forms that have those specificnews:1128748858.794233.86910@g14g2000cwa.googlegroups.com > This is what I did finally (this is the code in the BAS Module): > > Public Function Calculate(frmName As Form) > If (frmName.txtOperator.Text = "+") Then > frmName.txtOutput.Text = CDbl(frmName.txtNum1.Text) + control names. You should start thinking about abstracting the functionality so that your support routines become more generic and more resuable. Also, you've made this a Function but aren't returning a value. If you are going to do it the way you have it then a Public Sub would be more appropriate. If you do it the way I posted before then the Public Function makes sense. -- Reply to the group so all can participate VB.Net: "Fool me once..." "GrandNagel" <NOTGrandNa***@HotMail.com> wrote in message Why do you say it isn't recommended? In this particular case it turns outnews:ujr36H4yFHA.1032@TK2MSFTNGP12.phx.gbl > Otherwise you need to make it a Public Sub hanging off the form (which > is not recommended) not to be the best approach but it looks like your comment was made in general and for procedures that operate on the data held within the form it's a perfectly good option IMO. -- Reply to the group so all can participate VB.Net: "Fool me once..." "GrandNagel" <NOTGrandNa***@HotMail.com> wrote in message I wouldn't say that it's not recommended. There's nothing wrong with writing news:ujr36H4yFHA.1032@TK2MSFTNGP12.phx.gbl... > Otherwise you need to make it a Public Sub hanging off the form (which > is not recommended) and then call it using the formName.fnName() syntax. your own Public subs/functions in a form (they're just additional methods for instances of that particular form). Indeed, many times this is even advantageous (but as with anything else, it can be inappropriate, done incorrectly, or abused) It's really a question of whether it makes more sense to have the procedure be a method of the form or in a .bas module. Given that the procedure is named Calculate and is currently in a form, one can reasonably assume that it uses values from controls (textboxes or whatever) on that form. In that case, I'd say having it be a method of that form is the better choice. Of course, that's all conjecture since the OP didn't state any of this in his question. -- Mike Microsoft MVP Visual Basic |
|||||||||||||||||||||||