Solved

simple calculation wont work help please

Posted on 2001-06-19
12
152 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
 
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

746 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

15 Experts available now in Live!

Get 1:1 Help Now