Solved

simple calculation wont work help please

Posted on 2001-06-19
12
153 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Pull multiple cvs files into one access table 28 64
SSRS expression Issue finding a string 10 65
Validating VB6 Function 19 56
Advice in Xamarin 21 48
Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…

919 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

14 Experts available now in Live!

Get 1:1 Help Now