Search Tools Links Login

VB6 Tutorial 45: Overflow Errors

Posted: 2018-05-07
By: vb6boy
Viewed: 1,988

Filed Under:

VB6 Tutorials

No attachments for this post

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.


This example throws an overflow error.

Dim var As Integer
var = 98768787

run time error 6

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.


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.


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.


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.

For information about data type conversions, see this lesson in the tutorial.

Dim myResult As Long


myResult=CLng(878) * 8787




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.


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.

How to write your own functions will be covered later in this tutorial.

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.

Comments on this post

No comments have been added for this post.

You must be logged in to make a comment.