Have you ever faced an overflow-error? While working with large data, generally large numbers, overflow error raises. I’m going to explain about the overflow-error to you in this lesson with some tips and tricks for fixing it.
Reasons for an Overflow error
Assigning large numbers
The overflow error occurs when you’re assigning a large number or a numeric expression to a variable of the data type having a smaller value range.
Example
This example throws an overflow error.
Dim var As Integer var = 98768787
Run-time error 6 occurs in this example as the variable var is not able to store the value of this size. The range of an integer data type is from -32,768 through +32,767. So assigning any value beyond this range will cause an overflow-error. Take a Long variable instead of Integer to overcome this error.
Solution
Here’s the solution of the problem. This code does not throw any error.
Dim var As Long var = 98768787
Assigning large property values
Overflow error results when you’re assigning a larger property value than it can accept.
Example
This throws an overflow error.
Text1.MaxLength = 999999999999
The MaxLength property of the TextBox control sets the maximum number of characters that can be entered in the textbox. This property accepts only Long values. So you need to assign a value within the Long range to fix the problem.
Range of a datatype
The overflow error occurs when you’re using integer constant values in an expression but the result outgrows the maximum range of an Integer.
Example
Overflow error occurs here.
Dim myResult As Long myResult=878*8787
Both the numeric constants 878 and 8787 are within the range of Integer, but the multiplication result of them are larger than an Integer. So you first have to convert the whole expression or either of the numeric constants into Long as the following example does.
For information about data type conversions, see this lesson in the tutorial.
Solution
Dim myResult As Long myResult=CLng(878*8787)
Or
myResult=CLng(878) * 8787
Or
myResult=878*CLng(8787)
Or
myResult=878 * 8787&
Overflow error caused by Mod operator
You face an overflow error if the operands of the Mod operator are larger than Long values, that means, when the operand outgrows the range -2,147,483,648 to 2,147,483,647.
If you’d like to know more about data type operators, see this lesson in the tutorial.
Example
This code throws an overflow error.
Dim d As Double d = 9999999999# Mod 2
Solution
Writing a function that performs mod operation on larger numbers solves this problem. So you can write your own function, and use it in your program. This is the solution.
How to write your own functions will be covered later in this tutorial.
Overflow error caused by the Integer Division operator(“\”)
Example
Dim d As Double d = 7657887688# \ 4
Solution
In this case also you can write your own function that will perform on larger values. So instead of using the inbuilt ‘\’ operator, you can then use your custom operator function.
Finally I want to recommend a few things. If you’re working with very large values, I suggest you to take Double data type, and Long data type. Double has the higher range than Long. The Decimal data type is also helpful for larger numbers.