Reputation: 431
I have a form that does a simple calculation but the numbers are very large.
Example: xml 96276480 graphics 2931829760
I've declared the data types as Long and even did a CLng(Me.txtGraphics.value) but I'm still getting the over flow error These numbers are coming from text boxes. Here's my code:
Dim sgm As Long
Dim XML As Long
Dim prv As Long
Dim base As Long
Dim IETM As Long
Dim Graphics As Long
Dim total As Long
Dim pgCnt As Long
Const CXML = 2.5
Const CSGM = 6
Const CGraphic = 339
Dim pcSGM As Long
Dim pcXML As Long
Dim pcGraphic As Long
If Me.txtSGM.value = "" Then
sgm = 0
Else
sgm = Me.txtSGM.value
End If
If Me.txtXML.value = "" Then
XML = 0
Else
XML = Me.txtXML.value
End If
If Me.txtGraphics.value = "" Then
Graphics = 0
Else
Dim pics As Long
pics = CLng(Me.txtGraphics.value)
Graphics = CLng(pics)
End If
If Me.txtTotal.value = "" Then
base = 0
Else
base = Me.txtTotal.value
End If
prv = base
pcSGM = (CLng(sgm) / 1024) / CSGM
pcXML = (CLng(XML) / 1024) / CXML
pcGraphic = (CLng(Graphics) / 1024) / CGraphic
'total calculated newest SGM & XML values
total = (CLng(pcSGM)) + (CLng(pcXML)) + (CLng(pcGraphic))
Thank you for the help, Max
Upvotes: 0
Views: 3933
Reputation: 32632
A long in VBA is a 32-bit signed integer (as opposed to a long in VB.Net, which is 64-bits). As such, the maximum value it can hold is 2147483647.
2931829760 is clearly larger, thus you get an overflow.
Either use Decimal
and CDec
instead of Long
and CLng
to cast them to decimals, or use Double
and CDbl
to cast them to doubles.
CDbl
is likely faster, but can cause floating-point inaccuracy problems.
If you're sure your code will only be executed in the 64-bits variant of Access, you can also use CLngLng
, which casts it to the LongLong
data type, a 64-bits signed integer
Upvotes: 6