Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > General > Strange... Computer or VB?


Reply
 
Thread Tools Display Modes
  #1  
Old 11-21-2009, 03:18 PM
dan58 dan58 is offline
Newcomer
 
Join Date: Apr 2004
Posts: 2
Default Strange... Computer or VB?

Having written thousands of lines of code in VB5 since 1997, this is one of the few times that I'm completely baffled. I'm not sure what's causing this problem and I'm seeking perhaps a PC expert, but I'm not sure.

I get an overflow - just occurred today, there has never been this problem before. I closed the project I was working on and started a new one just to see if the symptoms returned - and they did.

Here's the code - simple project with just a form and the following code:

'Long data type = 2,147,483,647 - none of this code should exceed such...
'The following commented lines will generate Run-time error 6: Overflow
Private Sub Form_Load()
Dim DumbTest As Long
Dim i As Integer

'no overflow
DumbTest = 1234567 * 2

'no overflow
DumbTest = 1234567 * 12

'overflow when i = 130
'For i = 0 To 1000 Step 10
' DumbTest = i * 255
'Next i

'overflow when i = 135
'For i = 0 To 1000 Step 15
' DumbTest = i * 255
'Next i

'overflow when i = 140
'For i = 0 To 1000 Step 20
' DumbTest = i * 255
'Next i

'overflow when i = 150
'For i = 0 To 1000 Step 25
' DumbTest = i * 255
'Next i

'overflow when i = 260
'For i = 0 To 1000 Step 10
' DumbTest = i * 128
'Next i

'overflow when i = 340
For i = 0 To 1000 Step 20
DumbTest = i * 100
Next i


End Sub

'End of Code

Anyone have an idea? Thanks
Reply With Quote
  #2  
Old 11-21-2009, 04:36 PM
dan58 dan58 is offline
Newcomer
 
Join Date: Apr 2004
Posts: 2
Default

Resolved...

Found this:

In Microsoft Excel, when you run a Microsoft Visual Basic for Applications procedure that contains a mathematical calculation, you may receive the following error message:
Run-time error '6':
Overflow
Back to the top
CAUSE
This error message appears when the mathematical calculation involves numbers o...
This error message appears when the mathematical calculation involves numbers or variables of one data type, such as Integer, and you assign the result of the calculation to a variable of a different data type, such as Double or Long, even if the result of the calculation is within the range of the data type for the resulting variable. For example, you receive this error message when you run the following procedure:

Sub Test()
Dim MyVarInteger As Integer
Dim MyVarDouble As Double
MyVarInteger = 256
MyVarDouble = 256 * MyVarInteger
End Sub


The error message occurs in this case because the number 256 is a constant of Integer data type. Because the variable MyVarInteger is also a value of Integer data type, the multiplication calculation is performed as an Integer calculation. The error message occurs because the result of the calculation, 65536, is larger than the range for an Integer data type (which must be between -32768 and 32767).

By declaring the result, MyVarDouble, as Double data type, the calculation multiplies the two Integer data types and then attempts to convert the result to a Double data type. Because the result is not within the range for an Integer data type, the error occurs before the result is converted to the Double data type.
'****************************

Sure enough... if you change the variables to long clng(VarName) the problem is fixed!

Wow... I can't believe I never noticed this before!

Close the thread.
Reply With Quote
Reply


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

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 Off
HTML code is Off

Forum Jump

Advertisement:

Powered by liquidweb