Page 1 of 1

VB modules

Posted: 2004-04-06 07:32pm
by GoldenFalcon
Okey, I'm writing a program to be used to calculate force readings from given values of mass/acceleration (F=ma) or mass/velocity/time (F=m(v/t)). I placed the equations in a module and the command functions in the main (and only) form:

Form code:

Code: Select all

Private Sub cmdCalculate_Click()
    ' If F=ma is selected...
    If optMA.Value = True Then
    lblReading.Caption = MACalc()
    End If
    ' If F=m(v/t) is selected...
    If optMVT.Value = True Then
    lblReading.Captain = MVTCalc()
    End If
End Sub

Private Sub cmdExit_Click()
    Unload Me
End Sub
Module code:

Code: Select all

Public Sub MACalc()
    m = Val(txtMass.Text)
    a = Val(txtAcceleration.Text)
    F = m * a
End Sub

Public Sub MVTCalc()
    m = Val(txtMass.Text)
    v = Val(txtVelocity.Text)
    t = Val(txtTime.Text)
    F = m * (v / t)
End Sub
Now, when I try to execute the program and calculate using either of these options, it gives me a "Expected Function or variable" error. Why's this? I'm a newbie to VB, but shouldn't publicly-declared functions in modules be recognized by forms?

Thanks much.

ADDITION: Whoops, I forgot to say that it points to the MACalc() and MVTCalc() commands within the form code.

Posted: 2004-04-07 05:19am
by Sarevok
Use C++. It is a much better language than Visual Basic.

Posted: 2004-04-07 07:36am
by Super-Gagme
evilcat4000 wrote:Use C++. It is a much better language than Visual Basic.
Subjective to use, go spam another thread.

First you are not declaring the variables, which is just bad :p

Code: Select all

lblReading.Caption = MACalc()
Here you are setting the Caption property of a Label object (I assume based on your name) to the return of a Procedure call. Since Procedures are just Functions without a return, it isn't going to compile properly. What are you trying to set the caption to?

I'll assume the value of F, in which case try making the procedures, functions that return F. Let me just twiddle my fingers and show you what I mean. Ah hell, I'll just fix the entire thing.

Module Code re-work

Code: Select all

Public Function MACalc() As Double
    Dim m As Double
    Dim a As Double
    Dim F As Double

    m = Val(txtMass.Text) 
    a = Val(txtAcceleration.Text) 
    F = m * a 

    MACalc = F
End Function

Public Function MVTCalc() As Double
    Dim m As Double
    Dim a As Double
    Dim F As Double
    Dim t As Double

    m = Val(txtMass.Text) 
    v = Val(txtVelocity.Text) 
    t = Val(txtTime.Text) 
    F = m * (v / t) 

    MVTCalc = F
End Function
Don't have VB handy to test it, but sure that should cover it all.

EDIT:
Also you are referencing objects on a form from a module, so you want to properly reference or you will get another compiler error. Something like

Code: Select all

m = Val(frmMain.txtMass.Text)
(or whatever the name of the form is) for example.

Posted: 2004-04-07 02:05pm
by GoldenFalcon
Super-Gagme wrote:-snip-
I had a sneaking suspicion about those variables, but I thought I was confusing my C with my BASIC. Actually...I was confusing my C with my BASIC. :oops:

It worked like a charm, thanks much. :D