Solved

simple calculation wont work help please

Posted on 2001-06-19
12
155 Views
Last Modified: 2010-05-02
This just will not work in visual basic 5 on a windows
2000 pc with sp2 on it

Gtop=7680
Gleft=19200
Gwidth=0
Gheight=7680

If (Abs(Gtop) + Abs(Gleft) + Abs(Gwidth) + Abs(Gheight)) > 0 Then msgbox "OK"



I get a runtime error 6 - overflow
any thoughts???
many thanks
paul@pcdevelopments.co.uk
   
0
Comment
Question by:pcdev
  • 3
  • 2
  • 2
  • +4
12 Comments
 
LVL 22

Expert Comment

by:rspahitz
ID: 6206787
Are your variables defined as long, or integer?  If integer, then you're overflowing past the 32K limit.
0
 
LVL 3

Expert Comment

by:Maxim10553
ID: 6206811
you must define the variables of type long as rspahitz said
0
 
LVL 5

Accepted Solution

by:
jklmn earned 50 total points
ID: 6206823
Or, I quess what you need is:

If Abs(Gtop)>0 Or Abs(Gleft)>0 Or Abs(Gwidth)>0 Or Abs(Gheight))>0 Then msgbox "OK"



0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 5

Expert Comment

by:gbaren
ID: 6206826
Dim Gtop As Long
Dim Gleft As Long
Dim Gwidth As Long
Dim Gheight As Long

Gtop = 7680
Gleft = 19200
Gwidth = 0
Gheight = 7680

If (Abs(Gtop) + Abs(Gleft) + Abs(Gwidth) + Abs(Gheight)) > 0 Then MsgBox "OK"

0
 
LVL 5

Expert Comment

by:gbaren
ID: 6206829
oops, didn't mean to repeat an answer.
0
 
LVL 18

Expert Comment

by:deighton
ID: 6206855
Private Sub Command1_Click()
Gtop = 7680
Gleft = 19200
Gwidth = 0
Gheight = 7680

If (Abs(CLng(Gtop)) + Abs(Gleft) + Abs(Gwidth) + Abs(Gheight)) > 0 Then MsgBox "OK"




End Sub
0
 

Author Comment

by:pcdev
ID: 6206882
They were all dim as integer
changed them all to long same thing
also if you do this in the debug window
?7680+19200+7680
I get the same message, no dims, no variable etc??

So i have accepted the answer from gbaren

thanks very much
0
 

Author Comment

by:pcdev
ID: 6206893
They were all dim as integer
changed them all to long same thing
also if you do this in the debug window
?7680+19200+7680
I get the same message, no dims, no variable etc??

So i have accepted the answer from gbaren

thanks very much
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 6206894
deighton's comment is interesting because it applies the conversion to the 'if', thereby making the addition become long.

In some cases this would be appropriate, but in your case, pcdev, assuming that these variables represent screen dimensions, they should be defined as long.

Also, as jklmn commented, your 'if' would probably be better served as 4 separate comparisons rather than a comparison of the sums.  Besides, what if the gLeft is negative, which is possible for screen dimension.


0
 
LVL 6

Expert Comment

by:andyclap
ID: 6207867
>represent screen dimensions, they should be defined as long.
Although I agree with this, VB uses singles most of the time.

Interestingly enough doing it in the debug will give you an error becuase a 'nude' number is implicitly typed as the smallest type which can hold it (but not byte). If you want to force an long addition, just use

?7680&+19200+7680
Note you only need the & on the first term (as deighton pointed out), this is enough to switch the addition to longs.

If I remember correctly, & forces long, # double, % integer. Can't remember if there are any others offhand.

Just an aside really.
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 6207908
"VB uses singles most of the time"
Actually, you're right.  They should be defined as single!!!

"& forces long, # double, % integer"

Yes, and ! is single, and $ is string.

0
 
LVL 6

Expert Comment

by:andyclap
ID: 6208446
I use long a lot for internal calculations, because it's the quickest type. Forgot about single !s, but surely quotes are used for string constants ;)
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now