There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
access audio avg avg 8 bios blue screen boot bsod computer connection cpu crash css dell desktop dma driver drivers dvd email error excel explorer firefox firefox 3 freeze gimp graphics hard drive hardware hijackthis hjt install internet internet explorer itunes keyboard laptop macro malware monitor motherboard network networking outlook outlook 2003 outlook 2007 outlook express pio problem problems router seo server slow sound sp3 spyware trojan usb video virtumonde virus vista vundo windows windows vista windows xp winxp wireless
Software Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
What's Wrong with my Code? (VB6)


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

 
Thread Tools
THFS's Avatar
Senior Member with 253 posts.
 
Join Date: Feb 2004
Location: Queensland, Australia
Experience: Intermediate
27-Feb-2004, 09:41 PM #1
What's Wrong with my Code? (VB6)
I am working on an algebra program that will do quadratic formulas for me, but for some reason I keep getting this error:

Run-time error '5':

Invalid procedure call or argument

This is my code:

b = Val(before_x.Text)
a = Val(before_x_power.Text)
c = Val(non_x.Text)
x_value_1 = -b + Sqr(b ^ 2 - 4 * a * c) / 2 * a
X1.Text = Str$(x_value_1)

All the name values are correct, so that's not the problem. When I click debug it highlights the line with the formula, so I think it's something to do with that. Anyone see a problem with it?
AlbertB's Avatar
Distinguished Member with 2,432 posts.
 
Join Date: Nov 2002
Location: Hampshire, UK
27-Feb-2004, 10:43 PM #2
I am afraid I don't know VB, but I do know algebra. A suggestion! Have you rprogram detect a situation where:

( 4 * a * c ) > ( b ^ 2 )

This does happen with quadratics and will lead to a situation where you are trying to find the square root of a negative number, (easy for us if you know your complex numbers, but not for your computer).

Simply enclose your line of calculation in an "If - Then" statement to check for the condition and print an error message instead if it occurs.
__________________
1. "I make no personal claim to the truth, only the right to seek it, prove it in argument, and to be wrong many times in order to reach it."

2. "We have made a cage of words and placed our God inside, as boys trap a cricket, to make him sing for us alone."

Galileo Galilei
AlbertB's Avatar
Distinguished Member with 2,432 posts.
 
Join Date: Nov 2002
Location: Hampshire, UK
27-Feb-2004, 10:46 PM #3
Only a guess but I wonder if it does not like the way you have expressed your " -b " term. You could try ( -1 * b ) or even better rewrite it as:

x_value_1 = ( Sqr(b ^ 2 - 4 * a * c) - b ) / ( 2 * a )
coderitr's Avatar
Distinguished Member with 3,080 posts.
 
Join Date: Oct 2003
27-Feb-2004, 11:17 PM #4
Step through the code in the debugger and try to determine which line the error occurs on. Then, if possible, simplify that line of code into two or more separate lines. This will make your code easier to follow for others that may come behind you.
THFS's Avatar
Senior Member with 253 posts.
 
Join Date: Feb 2004
Location: Queensland, Australia
Experience: Intermediate
28-Feb-2004, 02:41 AM #5
I fixed the code, but now have another problem. Thanks for the If Then thing, that's helped with the negative squareroot thing, but now I want it to output text if it is impossible. I want it to say something like "Sorry, but this has no possible solution."
Here's the code now:

Private Sub first_x_value_Click()
a = Val(a.Text)
b = Val(b.Text)
c = Val(c.Text)
If (b ^ 2) > (4 * a * c) Then x_value_1 = (-b + Sqr(b ^ 2 - 4 * a * c)) / (2 * a)
If (b ^ 2) < (4 * a * c) Then x_value_1 = THIS IS WHERE I WANT THE TEXT
X1.Text = Str$(x_value_1)
End Sub

How would I get the text to be output into the textbox? I've tried a few things but keep getting errors. Once I get that done then the program will be complete.
AbvAvgUser's Avatar
Distinguished Member with 2,313 posts.
 
Join Date: Oct 2002
Location: Mumbai, India
Experience: Intermediate
28-Feb-2004, 04:46 AM #6
You can use msgbox functions. Or if you want it in a textbox, then textbox.text = "THIS IS WHERE I WANT THE TEXT"
AlbertB's Avatar
Distinguished Member with 2,432 posts.
 
Join Date: Nov 2002
Location: Hampshire, UK
28-Feb-2004, 01:56 PM #7
Another observation if I may

Remember the formula for calculating the roots of a quadratic is actually:

(-b ( + or - ) ( sqr( b^2 - 4*a*c ) ) / (2*a)

The term ( b^2 - 4*a*c ) is called the discriminant and the + or - generally gives rise to two equally valid and differing solutions.

You have only considered adding the value of the discriminant. It may not be necessary to consider both cases in the case of your problem, but if you wanted it to cover all eventualities you should offer another branch to your program to include both.
__________________
1. "I make no personal claim to the truth, only the right to seek it, prove it in argument, and to be wrong many times in order to reach it."

2. "We have made a cage of words and placed our God inside, as boys trap a cricket, to make him sing for us alone."

Galileo Galilei
THFS's Avatar
Senior Member with 253 posts.
 
Join Date: Feb 2004
Location: Queensland, Australia
Experience: Intermediate
28-Feb-2004, 07:04 PM #8
Thanks, but I just haven't implemented that part yet. I don't know how to combine two formulas so I'm just going to do a separate one for the subtraction.

Thanks for the textbox help, it's working great now.
AbvAvgUser's Avatar
Distinguished Member with 2,313 posts.
 
Join Date: Oct 2002
Location: Mumbai, India
Experience: Intermediate
29-Feb-2004, 02:08 AM #9
good to know its working as you want it to
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are Off
Refbacks are Off

You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 05:30 PM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.