VB modules

GEC: Discuss gaming, computers and electronics and venture into the bizarre world of STGODs.

Moderator: Thanas

Post Reply
User avatar
GoldenFalcon
Jedi Knight
Posts: 551
Joined: 2004-03-01 11:08pm
Location: Busy practicing with a bokken, come near me and I'll whack you with it.

VB modules

Post 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.
Babylon 5: In the Beginning quote:

General Lefcourt: "My people can handle themselves. We took care of the Dilgar. We can take care of the Minbari."
Londo Mollari: "Ahh, arrogance and stupidity all in the same package. How efficient of you."


Coming soon: Firebird Productions
User avatar
Sarevok
The Fearless One
Posts: 10681
Joined: 2002-12-24 07:29am
Location: The Covenants last and final line of defense

Post by Sarevok »

Use C++. It is a much better language than Visual Basic.
I have to tell you something everything I wrote above is a lie.
Super-Gagme
Little Stalker Boy
Posts: 1282
Joined: 2002-10-26 07:20am
Location: Lincoln, UK
Contact:

Post 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.
History? I love history! First, something happens, then, something else happens! It's so sequential!! Thank you first guy, for writing things down!

evilcat4000: I dont spam

Cairbur: The Bible can, and has, been used to prove anything and everything (practically!)
StarshipTitanic: Prove it.
User avatar
GoldenFalcon
Jedi Knight
Posts: 551
Joined: 2004-03-01 11:08pm
Location: Busy practicing with a bokken, come near me and I'll whack you with it.

Post 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
Babylon 5: In the Beginning quote:

General Lefcourt: "My people can handle themselves. We took care of the Dilgar. We can take care of the Minbari."
Londo Mollari: "Ahh, arrogance and stupidity all in the same package. How efficient of you."


Coming soon: Firebird Productions
Post Reply