VB help (please!:p)

Currently reading:
VB help (please!:p)

Joined
Apr 21, 2003
Messages
22,169
Points
3,110
Location
Nottm
Cheers people :) I am just writing a program to work out the roots of a quadratic equation, easy enough BUT I have to do a sub procedure (part of the specification) which checks if a = 0 (where a is the x^2 coefficent) - if it is, the method won't work. Fair enough.

I know how to make the program etc. but just one problem, if I check if a = 0 and it is, I display a msg box saying "change it dumbass" (well, nearly ;)). Trouble is I then end that sub proc and it goes back to the original program so it then tries to solve it and obviously messes up, any ideas please?

Private Sub cmdProcess_Click()
'Defining variables as required'
Dim numA As Single 'define coefficient a as single'
Dim numB As Single 'define coefficient b as single'
Dim numC As Single 'define coefficient c as single'
numA = Val(txtA)
numB = Val(txtB)
numC = Val(txtC)
Call check(numA)
If ((numB ^ 2) - (4 * numA * numC)) < 0 Then
MsgBox "The program can only deal with real roots, the equation you have entered has 2 complex root answers"
Else
numRoot1 = (-numB + (numB ^ 2 - 4 * numA * numC) ^ 0.5) / (2 * numA)
numroot2 = (-numB - (numB ^ 2 - 4 * numA * numC) ^ 0.5) / (2 * numA)
picDisplay.Cls
picDisplay.Print "The roots for the equation"; numA; "X^2"; "+"; numB; "X"; "+"; numC; "are:"; x = "; numRoot1; " And x = "; numroot2"
End If


End Sub
Private Sub check(numA)
'Is a = 0'
If numA = 0 Then
MsgBox "Please enter an A value other than 0"
txtA.Text = ""
txtB.Text = ""
txtC.Text = ""
Else
End If
End Sub



PS I know the programming is rubbish and nothing is well defined and explained, I tend to do that once I make it work lol.
 
It's ok Gaz :)

I have gone further and tried:

Private Sub cmdProcess_Click()

'Defining variables as required'
Dim numA As Single 'define coefficient a as single'
Dim numB As Single 'define coefficient b as single'
Dim numC As Single 'define coefficient c as single'
Dim numroot1 As Single
Dim numroot2 As Single


numA = Val(txtA)
numB = Val(txtB)
numC = Val(txtC)

Call check(numA)


End Sub



Private Sub check(numA)
If numA = 0 Then
MsgBox "Please enter an A value other than 0"
Else
Call check2(numA, numB, numC)
End If
End Sub


Private Sub check2(numB, numA, numC)
If ((numB ^ 2) - (4 * numA * numC)) < 0 Then
MsgBox "Not real root answers"
Else
Call calculate(numA, numB, numC, numroot1, numroot2)
End If
End Sub



Private Sub calculate(numA, numB, numC, numroot1, numroot2)
numroot1 = (-numB + (numB ^ 2 - 4 * numA * numC) ^ 0.5) / (2 * numA)
numroot2 = (-numB - (numB ^ 2 - 4 * numA * numC) ^ 0.5) / (2 * numA)
picDisplay.Cls
picDisplay.Print "The roots for the equation"; numA; "X^2 +"; numB; "X +"; numC; "are: x = "; numroot1; " And x = "; numroot2
End Sub



Doesn't work though.

Alex knows his stuff with VB.
 
Triumphant enterance... "To the rescue!"

1st things first... I guess the maths works how you want it? So what are the errors you're getting?
 
Change check into a function:

Private function check(numA)
If numA = 0 Then
check=false
Else
check= true
End If
End function

Then use :

If NOT check(numA) then
msgbox("whatever")
else

If ((numB ^ 2) - (4 * numA * numC)) < 0 Then
MsgBox "The program can only deal with real roots, the equation you have entered has 2 complex root answers"
Else
numRoot1 = (-numB + (numB ^ 2 - 4 * numA * numC) ^ 0.5) / (2 * numA)
numroot2 = (-numB - (numB ^ 2 - 4 * numA * numC) ^ 0.5) / (2 * numA)
picDisplay.Cls
picDisplay.Print "The roots for the equation"; numA; "X^2"; "+"; numB; "X"; "+"; numC; "are:"; x = "; numRoot1; " And x = "; numroot2"
End If

End If
 
Ben, not allowed to do a function :( Must be a sub routine or I get marked zero (Literally! - weird lecturer)

I have fixed it:

Private Sub cmdProcess_Click()

'Defining variables as required'
Dim numA As Single 'define coefficient a as single'
Dim numB As Single 'define coefficient b as single'
Dim numC As Single 'define coefficient c as single'
Dim numroot1 As Single
Dim numroot2 As Single


numA = Val(txtA)
numB = Val(txtB)
numC = Val(txtC)

Call check(numA, numB, numC, numroot1, numroot2)


End Sub



Private Sub check(numA, numB, numC, numroot1, numroot2)
If numA = 0 Then
picDisplay.Cls
MsgBox "Please enter an A value other than 0"
Else
Call check2(numA, numB, numC, numroot1, numroot2)
End If
End Sub


Private Sub check2(numA, numB, numC, numroot1, numroot2)
If ((numB ^ 2) - (4 * numA * numC)) < 0 Then
picDisplay.Cls
MsgBox "Not real root answers"
Else
Call calculate(numA, numB, numC, numroot1, numroot2)
End If
End Sub



Private Sub calculate(numA, numB, numC, numroot1, numroot2)
numroot1 = (-numB + (numB ^ 2 - 4 * numA * numC) ^ 0.5) / (2 * numA)
numroot2 = (-numB - (numB ^ 2 - 4 * numA * numC) ^ 0.5) / (2 * numA)
picDisplay.Cls
picDisplay.Print "The roots for the equation"; numA; "X^2 +"; numB; "X +"; numC; "are: x = "; numroot1; " And x = "; numroot2
End Sub



BUT the only thing I have done is stuck all the variables in each () after calling the routines and at the top of the routine, is this normal?

Cheers for the help :)
 
*Loud coughing*

Sorry paul, but what is the actual error you're getting?

also, shouldnt these lines be like this?
Code:
numRoot1 = (-numB + ([color=red]([/color]numB ^ 2[color=red])[/color] - 4 * numA * numC) ^ 0.5) / (2 * numA)
numroot2 = (-numB - ([color=red]([/color]numB ^ 2[color=red])[/color] - 4 * numA * numC) ^ 0.5) / (2 * numA)
 
No, bidmas/bodmas means that it does the squaring first. You can put the bracket but it doesn't make any difference (assuming VB follows rules of maths).

order of maths is always:

brackets (why you said it), indices (powers), division or multiplication, addition or subtraction.


The actual error was overflow error 6 or something but I know it's because it was doign a calculation on numbers it can't do them on. Now I have only got the next section of the program being called IF the calculation can be done I have solved that problem, only trouble is I don't understand why I have to put everything within the brackets (if I do at all).
 
Last edited:
I realised that just after I pressed submit, but as a rule of thumb, I always split my calculations up into smaller parts separated by brackets, so I understand whats going on.

EDIT: a good forum for stuff like this is http://forums.ASPFree.com, people haven't laughed at any of my silly questions on there yet!
 
Me too but I was told off during our excel work for using too many needless brackets. - I tend to avoid them a little now if not mathematically necessary.
 
The Negotiator said:
Cheers Alex, will look at the forum, that's my worry, people will laugh at me :(

I wouldnt worry about that, i wouldnt dream of laughing at someone who understood all of what i see as jibberish above.
 
But you know what it's like with things like that, be it programming, computer parts, bikes or camping, if you don't know it inside out they wonder why you're bothering to try!

Seriously, anybody who wants to learn some programming should get this VB 6.0 book, it's quite logical and fairly easy to follow. I know people will say "don't learn x learn y" but if you just want to get the idea about programming I think it's a good start.
 
i tried teaching myself once....gave up. a guy at work who really knows his s**t (works for microsoft as a hobby) said i could really do with a project to learn programming for. still havent thought of anything yet. *sigh*
anyway if its problem solving youre after why doesnt the right hand wing or underwing fuel tank transfer in normal on a tornado at work? bloody got me stumped.
 
Easy to get used to. Now a fair bit of code from just using it from courseworks. Does any1 know the code for exporting data from a table in access (which has a parameter) to a specific file in excel, which is automatically updated each time you open it. lol It needs to refresh tooas i need to display a graph from it. :D
 
Back
Top