VB Student needs help, please. 
Author Message
 VB Student needs help, please.

I am a VB6 student and am working a simple, do-nothing program for my class.
It's basically just practice for myself, it's not even an assignment.  Heres
the issue:

Private Sub cmdSummary_Click()

    Summary  <--Calling this procedure
End Sub
------------------------------------------
Private Sub Summary(ByVal intTotal As Integer, intUsers As Integer)
'these variables are calculated in another procedure

    frmSummary.Show
    frmSummary.lblTotal = intTotal
    frmSummary.lblUsers = intUsers
    frmSummary.lblAverage = intTotal / intUsers
End Sub
------------------------------------------

Why is that when I click the Summary button  I get a compile error "Argument
not optional"?   Everything else in the program works fine,  the calculate
procedure works fine.  Why wont the values pass?  Keep in mind I'm doing
this to practice passing values and calling sub procedures and functions and
the fact that this is totally rediculous is not important.  Please, help.



Mon, 12 Apr 2004 08:28:32 GMT  
 VB Student needs help, please.
In your sample Summary is called without parameters. Is it a typo?

===================================================

Quote:
> I am a VB6 student and am working a simple, do-nothing program for my
class.
> It's basically just practice for myself, it's not even an assignment.
Heres
> the issue:

> Private Sub cmdSummary_Click()

>     Summary  <--Calling this procedure
> End Sub
> ------------------------------------------
> Private Sub Summary(ByVal intTotal As Integer, intUsers As Integer)
> 'these variables are calculated in another procedure

>     frmSummary.Show
>     frmSummary.lblTotal = intTotal
>     frmSummary.lblUsers = intUsers
>     frmSummary.lblAverage = intTotal / intUsers
> End Sub
> ------------------------------------------

> Why is that when I click the Summary button  I get a compile error
"Argument
> not optional"?   Everything else in the program works fine,  the calculate
> procedure works fine.  Why wont the values pass?  Keep in mind I'm doing
> this to practice passing values and calling sub procedures and functions
and
> the fact that this is totally rediculous is not important.  Please, help.



Mon, 12 Apr 2004 09:10:54 GMT  
 VB Student needs help, please.
It's no typo.  I thought all I had to was call the procudure by name, in
this case just plain Summary.  What parameters do I need?  Thanks.


Mon, 12 Apr 2004 09:16:33 GMT  
 VB Student needs help, please.
Unless I am missing something, you do not specify the parameters you wish to
pass. How will Summary know values of intTotal and intUsers?

========================================================

Quote:
> It's no typo.  I thought all I had to was call the procudure by name, in
> this case just plain Summary.  What parameters do I need?  Thanks.



Mon, 12 Apr 2004 09:36:11 GMT  
 VB Student needs help, please.
intTotal and intUser are figured under a different procedure, under
cmdCalculate that I didn't list here because I didn't want to have to copy
over the whole program.  the cmdCalculate procedure works just fine and will
figure intTotal and intUser, then using a command button called Summary I
want to call the Summary procedure which will have passed to it the values
for intTotal and intUser that were previously calculated under cmdCalculate.


Mon, 12 Apr 2004 09:36:58 GMT  
 VB Student needs help, please.
Procedure Summary is defined with two parameters. You actually need to pass
them. For example, it could look something like this:

Dim iT1 As Integer
Dim iT2 As Integer

Summary(iT1, iT2)
=======================================

Quote:
> intTotal and intUser are figured under a different procedure, under
> cmdCalculate that I didn't list here because I didn't want to have to copy
> over the whole program.  the cmdCalculate procedure works just fine and
will
> figure intTotal and intUser, then using a command button called Summary I
> want to call the Summary procedure which will have passed to it the values
> for intTotal and intUser that were previously calculated under

cmdCalculate.


Mon, 12 Apr 2004 09:56:36 GMT  
 VB Student needs help, please.
Yup, that's exactly what I did, but I still get this error, "argument not
optional", when I press the Summary button during run-time".  The three
procedures in question look like this:

Private Sub cmdSummary_Click()

    Summary
End Sub
-------------------------------------------

Private Sub Calculate()

    Dim intTotal     As Integer
    Dim intUsers     As Integer

    If Not IsNumeric(txtNumber1.Text) Then
        MsgBox "Please Enter a Valid number", vbOKOnly, "Error"
        With txtNumber1
                       .Text = ""
                       .SetFocus
        End With
    ElseIf Not IsNumeric(txtNumber2.Text) Then
        MsgBox "Please Enter a Valid Number.", vbOKOnly, "Error"
        With txtNumber2
                       .Text = ""
                       .SetFocus
        End With
    Else
        intTotal = intTotal + Val(txtNumber1.Text) + Val(txtNumber2.Text)
        intUsers = intUsers + 1
        lblSum.Caption = Val(txtNumber1.Text) + Val(txtNumber2.Text)
        cmdSummary.Enabled = True
        mnuSummary.Enabled = True
    End If
End Sub
---------------------------------------------------------------

Private Sub Summary(ByVal intTotal As Integer, intUsers As Integer)

    frmSummary.Show
    frmSummary.lblTotal = intTotal
    frmSummary.lblUsers = intUsers
    frmSummary.lblAverage = intTotal / intUsers
End Sub
--------------------------------------------------
I cut and pasted this right out of the program so everything here is how it
actually is in the program.  So any thoughts?  What the hell am I doing
wrong?



Mon, 12 Apr 2004 09:59:42 GMT  
 VB Student needs help, please.
1. Variables intTotal and intUsers are not visible outside Calculate.
2. Summary expects a call with two parametrs since you defined it this way.
When calling  the procedure you, however, pass none.

Try the following in a test project:

Dim iT1 as Integer
Dim iT2 as Integer

iT1 = 6
iT2 = 3

Summary(iT1, iT2)

============================================================================
============

Quote:
> Yup, that's exactly what I did, but I still get this error, "argument not
> optional", when I press the Summary button during run-time".  The three
> procedures in question look like this:

> Private Sub cmdSummary_Click()

>     Summary
> End Sub
> -------------------------------------------

> Private Sub Calculate()

>     Dim intTotal     As Integer
>     Dim intUsers     As Integer

>     If Not IsNumeric(txtNumber1.Text) Then
>         MsgBox "Please Enter a Valid number", vbOKOnly, "Error"
>         With txtNumber1
>                        .Text = ""
>                        .SetFocus
>         End With
>     ElseIf Not IsNumeric(txtNumber2.Text) Then
>         MsgBox "Please Enter a Valid Number.", vbOKOnly, "Error"
>         With txtNumber2
>                        .Text = ""
>                        .SetFocus
>         End With
>     Else
>         intTotal = intTotal + Val(txtNumber1.Text) + Val(txtNumber2.Text)
>         intUsers = intUsers + 1
>         lblSum.Caption = Val(txtNumber1.Text) + Val(txtNumber2.Text)
>         cmdSummary.Enabled = True
>         mnuSummary.Enabled = True
>     End If
> End Sub
> ---------------------------------------------------------------

> Private Sub Summary(ByVal intTotal As Integer, intUsers As Integer)

>     frmSummary.Show
>     frmSummary.lblTotal = intTotal
>     frmSummary.lblUsers = intUsers
>     frmSummary.lblAverage = intTotal / intUsers
> End Sub
> --------------------------------------------------
> I cut and pasted this right out of the program so everything here is how
it
> actually is in the program.  So any thoughts?  What the hell am I doing
> wrong?



Mon, 12 Apr 2004 10:17:53 GMT  
 VB Student needs help, please.
Hi,

In case you have not yet figured out your problems, let me add the following:

1. - If you want the calculated values of intTotal and intUsers to be available
to other methods and functions in your Form then you will have to declare the
variables at the module level, not in the Sub itself:

Dim intTotal As Integer
Dim intUsers As Integer

Private Sub Calculate()
    If Not IsNumeric(txtNumber1.Text) Then
        MsgBox "Please Enter a Valid number", vbOKOnly, "Error"
        With txtNumber1
                       .Text = ""
                       .SetFocus
        End With
    ElseIf Not IsNumeric(txtNumber2.Text) Then
        MsgBox "Please Enter a Valid Number.", vbOKOnly, "Error"
        With txtNumber2
                       .Text = ""
                       .SetFocus
        End With
    Else
        intTotal = intTotal + Val(txtNumber1.Text) + Val(txtNumber2.Text)
        intUsers = intUsers + 1
        lblSum.Caption = Val(txtNumber1.Text) + Val(txtNumber2.Text)
        cmdSummary.Enabled = True
        mnuSummary.Enabled = True
    End If
End Sub

2. - If you do this, intTotal and intUsers will be automatically available to
your Summary method, so you won't need to use any arguments:

Private Sub Summary
    frmSummary.Show
    frmSummary.lblTotal = intTotal
    frmSummary.lblUsers = intUsers
    frmSummary.lblAverage = intTotal / intUsers
End Sub

3. -  What you will now need is to call the Calculate method to actually
calculate your values, and then the Summary method to use them:

Private Sub cmdSummary_Click()
    Calculate
    Summary
End Sub

4. - Alternatively, you could put the whole thing in the cmdSummary_Click()
event:

Private Sub cmdSummary_Click()
    Dim intTotal As Integer
    Dim intUsers As Integer

    If Not IsNumeric(txtNumber1.Text) Then
        MsgBox "Please Enter a Valid number", vbOKOnly, "Error"
        With txtNumber1
                       .Text = ""
                       .SetFocus
        End With
    ElseIf Not IsNumeric(txtNumber2.Text) Then
        MsgBox "Please Enter a Valid Number.", vbOKOnly, "Error"
        With txtNumber2
                       .Text = ""
                       .SetFocus
        End With
    Else
        intTotal = intTotal + Val(txtNumber1.Text) + Val(txtNumber2.Text)
        intUsers = intUsers + 1
        lblSum.Caption = Val(txtNumber1.Text) + Val(txtNumber2.Text)
        cmdSummary.Enabled = True
        mnuSummary.Enabled = True
    End If
    frmSummary.Show
    frmSummary.lblTotal = intTotal
    frmSummary.lblUsers = intUsers
    frmSummary.lblAverage = intTotal / intUsers
End Sub

Regards,

John..............

Quote:

> Yup, that's exactly what I did, but I still get this error, "argument not
> optional", when I press the Summary button during run-time".  The three
> procedures in question look like this:

> Private Sub cmdSummary_Click()

>     Summary
> End Sub
> -------------------------------------------

> Private Sub Calculate()

>     Dim intTotal     As Integer
>     Dim intUsers     As Integer

>     If Not IsNumeric(txtNumber1.Text) Then
>         MsgBox "Please Enter a Valid number", vbOKOnly, "Error"
>         With txtNumber1
>                        .Text = ""
>                        .SetFocus
>         End With
>     ElseIf Not IsNumeric(txtNumber2.Text) Then
>         MsgBox "Please Enter a Valid Number.", vbOKOnly, "Error"
>         With txtNumber2
>                        .Text = ""
>                        .SetFocus
>         End With
>     Else
>         intTotal = intTotal + Val(txtNumber1.Text) + Val(txtNumber2.Text)
>         intUsers = intUsers + 1
>         lblSum.Caption = Val(txtNumber1.Text) + Val(txtNumber2.Text)
>         cmdSummary.Enabled = True
>         mnuSummary.Enabled = True
>     End If
> End Sub
> ---------------------------------------------------------------

> Private Sub Summary(ByVal intTotal As Integer, intUsers As Integer)

>     frmSummary.Show
>     frmSummary.lblTotal = intTotal
>     frmSummary.lblUsers = intUsers
>     frmSummary.lblAverage = intTotal / intUsers
> End Sub
> --------------------------------------------------
> I cut and pasted this right out of the program so everything here is how it
> actually is in the program.  So any thoughts?  What the hell am I doing
> wrong?



Mon, 12 Apr 2004 11:07:22 GMT  
 VB Student needs help, please.
Thank you both.  I guess what I'm not understanding here is why and how to
pass variables ByVal and ByRef.  Our textbook explanation is one paragraph
and basically just says that it's a good idea to keep the scope of variables
small, so declaring a variable to be local and then passing it on to other
procedures via ByVal and ByRef is a good idea so that the variables aren't
seen by anthing but what I say.  ByVal wont let the procedure change the
original variable, and ByRef will send the memory address with the varible
so the procedure can change the originals value.  So far I haven't found a
practical reason why ever to pass variables like that, and even in this
simple program I just made up, I can't seem to get it right.  I rewrote the
program using module level variables and it works just fine, but was kinda
of self-defeating when I wanted to practice and learn passing variables
ByVal and ByRef.  I knew from the begining it would work with module level
variables.  It was still good practice for other stuff, but I am still
clueless on how to pass variables ByVal and ByRef.
I have class tomarrow night.  My teacher seems to have just about an
infinite amount of patients and is pretty good.  But it's one of those
things where she understands it but can't seem to explain it very well and I
get the feeling I'm not the only student lost on this.  Please if either of
you, or anyone else for that matter, can explain how, why, and when to pass
ByVal and ByRef it would be very much appriciated.  Especially since the
teacher wants us to be using this method for variables regularly.  Thank you
for your help.


Mon, 12 Apr 2004 14:11:28 GMT  
 VB Student needs help, please.
You have defined your Summary subroutine as having two arguments -- intTotal and intUsers.
Because you specified them in the declaration statement for your subroutine, two arguments
MUST be supplied EVERY time you call this subroutine. The arguments are not optional
(hence, your error message), they are required.

When you declare a subroutine with one or more arguments, you are telling VB to supply a
one-to-one mapping of each variable;s memory location between your subroutine (where you
will either be using a value passed into your subroutine from the code calling it; or you
will communicate a calculated value back to the code that called it from your subroutine).

This passageway between the calling code and the subroutine's code is what makes
subroutines so useful. When you provide the argument(s) in the declaration of the
subroutine, you are telling VB to create this passageway where you will either provide
values for the subroutine to process or where you return values for the calling code to
use, or both. If you do not need this passage way, then don't specify the subroutine as
requiring any arguments.

Now, here is where the explanation gets a little murky. You stated that intTotal and
intUsers was being handled elsewhere. By this, I assume you mean they are globally defined
somewhere within your program. First, let me state that you should strive to minimize the
use of globally defined variables. At your present skill level, they will seem quite
seductive -- no matter where you are in your program, you can assign them values and
retrieve values from them -- and on a small program, somewhat manageable. But when you
start writing large programs, they will start to cause you all kinds of maintenance
headaches. That is not to say never use them, there ARE circumstances where their use is
quite helpful, just try not to use them everywhere. Anyway, if I'm correct and the
intTotal and intUsers are global, then the subroutine can see them automatically and you
didn't have to place them in the subroutine's argument list.

Rick


Quote:
> I am a VB6 student and am working a simple, do-nothing program for my class.
> It's basically just practice for myself, it's not even an assignment.  Heres
> the issue:

> Private Sub cmdSummary_Click()

>     Summary  <--Calling this procedure
> End Sub
> ------------------------------------------
> Private Sub Summary(ByVal intTotal As Integer, intUsers As Integer)
> 'these variables are calculated in another procedure

>     frmSummary.Show
>     frmSummary.lblTotal = intTotal
>     frmSummary.lblUsers = intUsers
>     frmSummary.lblAverage = intTotal / intUsers
> End Sub
> ------------------------------------------

> Why is that when I click the Summary button  I get a compile error "Argument
> not optional"?   Everything else in the program works fine,  the calculate
> procedure works fine.  Why wont the values pass?  Keep in mind I'm doing
> this to practice passing values and calling sub procedures and functions and
> the fact that this is totally rediculous is not important.  Please, help.

______________________________________________________________________________
Posted Via Binaries.net = SPEED+RETENTION+COMPLETION = http://www.binaries.net


Mon, 12 Apr 2004 21:20:57 GMT  
 VB Student needs help, please.
Values are passed by default ByRef, so specifying that keyword is not
necessary unless you feel the need to make your code more readable. ByVal
passing is useful when the receiving routine may or will make changes to the
value passed, but you do not want that value changed in the calling sub. For
example...

Private Sub1(astring as string)

    astring =astring & "Frost"

End sub

Private Sub2(ByVal astring as string)

    astring =astring & "Frost"
   Print "In Sub2 = " & astring

End sub

Sub Command1_Click

    dim sName as string
    sName = "Robert"
    Sub1 sName
    Print sName
    Sub2 sNme
    Print sName

End Sub

Because sName was passed ByRef to Sub1, the sub changed the string passed
from "Robert" to "Robert Frost". But the subsequent passing of sName to Sub2
generated an undesirable side effect -- it appended the last name, but to
the altered string, resulting in a printout of the double last name. Back in
the calling procedure, because the param to Sub2 was ByVal, sName remained
unaltered.

'printout results
Robert Frost
In Sub2 = Robert FrostFrost
Robert Frost

Note as well that passing a variable enclosed in brackets also creates a
ByVal pass, i.e...

  Sub1 sName             'ByRef pass
  Sub1 ByVal sName   'ByVal pass
  Sub1 (sName)           'ByVal pass

.. but NOT if the Call syntax is used:

  Call Sub1(sName)             'ByRef pass, not ByVal
  Call Sub1(ByVal sName)   'ByVal pass
  Call Sub1((sName))           'ByVal pass, double brackets

'--------------------

Quote:
>So far I haven't found a
> practical reason why ever to pass variables like that,

Consider this ...

Dim result as integer
Dim param as integer

   param = 25
   result = CalcA(param)

Private function CalcA(sDataIn as Integer) as Integer
   sDataIn = sDataIn * 2
   CalcA =sDataIn
Exit Function

Now, in addition to result holding the value 50, param is now equal to 50.
If you later wanted to use param again in another call or method, expecting
its value to be 25, you would now have an error in your program.

'--------------------

So ... for your app, you want:

Private Sub cmdSummary_Click()

   <code to calc values for intUsers/intTotal>
    Summary intTotal, intUsers

End Sub

"Argument not optional" means just that .. you defined the Summary sub to
accept and expect two values to be passed. In your initial code, you passed
none, hence the error.

If the passing of parameters was not mandatory, you would use the Optional
keyword in defining the sub ...

Private Sub Summary(Optional intTotal As Integer, Optional intUsers As
Integer)

This would allow you to call Summary without parameters as you initially
did, but of course the results would be meaningless since Summary has no
knowledge of what intTotal or intUsers was, let alone their value.

--

Randy Birch
MVP Visual Basic

http://www.mvps.org/vbnet/

Please respond only to the newsgroups so all can benefit.


Quote:
> Thank you both.  I guess what I'm not understanding here is why and how to
> pass variables ByVal and ByRef.  Our textbook explanation is one paragraph
> and basically just says that it's a good idea to keep the scope of
variables
> small, so declaring a variable to be local and then passing it on to other
> procedures via ByVal and ByRef is a good idea so that the variables aren't
> seen by anthing but what I say.  ByVal wont let the procedure change the
> original variable, and ByRef will send the memory address with the varible
> so the procedure can change the originals value.  So far I haven't found a
> practical reason why ever to pass variables like that, and even in this
> simple program I just made up, I can't seem to get it right.  I rewrote
the
> program using module level variables and it works just fine, but was kinda
> of self-defeating when I wanted to practice and learn passing variables
> ByVal and ByRef.  I knew from the begining it would work with module level
> variables.  It was still good practice for other stuff, but I am still
> clueless on how to pass variables ByVal and ByRef.
> I have class tomarrow night.  My teacher seems to have just about an
> infinite amount of patients and is pretty good.  But it's one of those
> things where she understands it but can't seem to explain it very well and
I
> get the feeling I'm not the only student lost on this.  Please if either
of
> you, or anyone else for that matter, can explain how, why, and when to
pass
> ByVal and ByRef it would be very much appriciated.  Especially since the
> teacher wants us to be using this method for variables regularly.  Thank
you
> for your help.



Tue, 13 Apr 2004 00:19:31 GMT  
 VB Student needs help, please.
This is to Rick, Randy, and Eugene,
Thank you!  I worked on that all day and I've read what you guys wrote it
made so much more sence. (it probably also helped that I took some time away
from it  working on it all day and had finally had gotten some sleep).  I
printed off everything you guys have and plan to share it with my classmates
because you guys made a whole lot more sence than my textbook.  Thanks
again.


Tue, 13 Apr 2004 12:15:16 GMT  
 VB Student needs help, please.


Quote:
> This is to Rick, Randy, and Eugene,
> Thank you!  I worked on that all day and I've read what you guys wrote it
> made so much more sence. (it probably also helped that I took some time
away
> from it  working on it all day and had finally had gotten some sleep).  I
> printed off everything you guys have and plan to share it with my
classmates
> because you guys made a whole lot more sence than my textbook.  Thanks
> again.

I'd only re-enforce what the others ably described for you and add that one
prime use for subroutines and code modularization is to be able to write
general purpose routines that can subsequently be used over again.  While
you don't say what sort of applications you might be into, this is
particularly true in many mathematical applications for solving systems of
equations, etc.


Wed, 14 Apr 2004 05:46:19 GMT  
 
 [ 14 post ] 

 Relevant Pages 

1. VB student needs some help, please.

2. VB Student needs help with one question, Please?

3. Student needing Help Please

4. Student in need of help Please

5. !!!Please help vb student!!!

6. !!!Please help vb student!!!

7. Help: Please recommend a VB textbook for business major students

8. Student new to vb needs urgent help!

9. Students needing help in VB?

10. Students needing help with VB Homework?

11. Students Needing VB help?

12. Student needs help with VB API

 

 
Powered by phpBB® Forum Software