Solved

simple calculation wont work help please

Posted on 2001-06-19
12
159 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How does CurrentUser work? 10 44
IF ELSE Statement in Excel Macro VBA 16 103
checkbox to hide entire section 10 76
VBA: loop recent folder and copy txt file. 8 56
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

752 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