Fixing the Overflow Error in VB6

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.

When does the overflow-error occur?

1. 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

Overflow error in VB6

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

2. 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.

3. 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

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.

In this context, you should learn about:

Dim myResult As Long
myResult=CLng(878) * 8787
myResult=878 * 8787&

4. 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.

To know about operators:

Example: This code throws an overflow error.
Dim d As Double
d = 9999999999# Mod 2

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.

Learn how to write a user-defined function:

5. Overflow error caused by the Integer Division operator(“\”)

Dim d As Double
d = 7657887688# \ 4

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.