If i run the following code it gives an error. why?
Private Sub Command1_click()
Dim b as Long
b=30*4000
MsgBox b
End Sub
Printable View
If i run the following code it gives an error. why?
Private Sub Command1_click()
Dim b as Long
b=30*4000
MsgBox b
End Sub
Ok boss
But
tell me the reason why mine is not working
or u could use the integer declaration
it does not work, because the value of which you are trying the set to the "long" variable, is bigger than the maximum alllowed size :)Code:Dim a As Integer, b As Integer, c As Integer
a = 30
b = 4000
c = a * b
MsgBox c
oh yeah, you can do it this way too:
:)Code:Dim x As Long
x = CLng(4000) * 30
MsgBox x
it's shorter than the other methods :)
you got a lot of quite confused with your problem, but one at my office gave me the reason
Dim b as Long
b=30*4000
you try to multiply two constants,
Visual Basic uses the smallest datatype for each constant, which is Integer, for calculating the result
that's the reason why 30*1092=32670 works, and 30*1093=32790 gives you an overflow, because it's bigger than the maximum Integer(32768)
I consider it good practice to specify the datatype of constants. You don't get into these problems if you do this.
eg
dim x as long
x = 30& * 4000&
what's the matter with my way?
Thanks Nina. You cleared why it is not working.
and Thanks to Frans C for giving correct solution
da_silvy,
Nothing is wrong with your way, I just wanted to show an alternative.