Applecore Pages on Microsoft Access

Runtime Error: 6 - Overflow

This can be an annoying error to track down, as it is caused by several reasons, some obvious, and some not. This error is caused by Access trying to store a variable in a datatype that is too small for the variable, for example trying to store 300 in a byte field, which can accept values from 0 to 255.

The obvious cause is when you do declare a variable and then try to store a number that is too large, as below:

Sub sOverflow1()
    Dim byt As Byte
    Dim lng As Long
    lng = 5000
    byt = lng
End Sub

This will cause an error on the last line, byt=lng. This is an obvious case - normally you will be trying to store the results of a calculation into the variable and it might not be so obvious as you are writing the code. If you use a consistent naming convention, such as the Leszynski/Reddick guidelines where you prefix variables with a tag indicating the variable type, this does help when you are writing the code.

Another cause of this is caused by division, when both the numerator and denominator are 0 (if only the denominator is 0, then you get an error message of 11 - Division by Zero):

Sub sOverflow2()
    Dim byt1 As Byte
    Dim byt2 As Byte
    byt1 = byt1 / byt2
End Sub

The final reason that you can get this error within Access is caused by multiplying two numbers together, such as Bytes, and trying to store them in a Long Integer field as you know that the result may be anything up to 65025. Access tries to assign the result of the operation to a Byte because both of the multipliers are permissible as Bytes:

Sub sOverflow3()
    Dim byt1 As Byte
    Dim byt2 As Byte
    Dim lng As Long
    byt1 = 100
    byt2 = 100
    lng = (byt1 * byt2)
End Sub

The solution in this case is to convert at least one of the variables to the appropriate data type using something like CLng:

Sub sNoOverflow()
    Dim byt1 As Byte
    Dim byt2 As Byte
    Dim lng As Long
    byt1 = 100
    byt2 = 100
    lng = (CLng(byt1) * byt2)
End Sub

Top

 


HOME | NEW | TABLES | QUERIES | FORMS | REPORTS | GENERAL | API | DOWNLOADS | TUTORIAL | RESOURCES
E-MAIL
Copyright & Disclaimer

 

Last modified at 06/06/2006 14:58:07