6 Replies - 5720 Views - Last Post: 18 October 2009 - 04:03 PM Rate Topic: -----

#1 Mezzeric  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 26-July 09

Runtime Error-6 Overflow (Factorial)

Posted 18 October 2009 - 03:38 AM

hello to everyone! :D anyway here's my prob: :P

my professor want us to create a program in vb with factorial computation (which i already know the formula!).
my problem is when i input a bigger number there's an "error runtime-6 overflow". how can i fix these? TY in advance. B)

here's my code so far.

Public t
Dim i As Currency
Dim z As Integer
Dim m As Integer
Dim fact As Integer
Private Sub Command1_Click()
t = 10
fact = 1
For i = 1 To Text3 Step 1
List1.AddItem i
fact = fact * i
Text2 = fact
Next
List1.AddItem i
End Sub

Private Sub Form_Load()
Timer1.Enabled = True
t = 10
End Sub

Private Sub Text1_Change()
Text1.Enabled = True
End Sub

Private Sub Timer1_Timer()
Countdown.Text = t
If t = 0 Then
Text3.Enabled = False
Timer1.Enabled = False
End If
t = t - 1
End Sub



Is This A Good Question/Topic? 0
  • +

Replies To: Runtime Error-6 Overflow (Factorial)

#2 Philip Munce  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 14-October 09

Re: Runtime Error-6 Overflow (Factorial)

Posted 18 October 2009 - 04:46 AM

Hi Mezzeric,

My initial suspicion is that the data type of "fact" is causing the problem. I have had similar problems in the past with VB6 or VBA not handling larger numbers. I got around it by declaring the variable as a type Long. The reason for this is that Integer stores values in the range of (-32,768) - (+ 32,767) and Longs store values in the range of (- 2,147,483,468) - (+ 2,147,483,468).

try:

Dim fact As Long


and see if it fixes the problem.

Hope this helps.

Phil

This post has been edited by Philip Munce: 18 October 2009 - 04:48 AM

Was This Post Helpful? 1
  • +
  • -

#3 Mezzeric  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 26-July 09

Re: Runtime Error-6 Overflow (Factorial)

Posted 18 October 2009 - 05:26 AM

View PostPhilip Munce, on 18 Oct, 2009 - 03:46 AM, said:

Hi Mezzeric,

My initial suspicion is that the data type of "fact" is causing the problem. I have had similar problems in the past with VB6 or VBA not handling larger numbers. I got around it by declaring the variable as a type Long. The reason for this is that Integer stores values in the range of (-32,768) - (+ 32,767) and Longs store values in the range of (- 2,147,483,468) - (+ 2,147,483,468).

try:

Dim fact As Long


and see if it fixes the problem.

Hope this helps.

Phil


Thanks Mr. Phil for the help. Still there's a
Runtime Error-6 - Overflow
Posted Image :crazy:
and when i click the "DEBUG" it highlights:

fact = fact * i



but the good thing is i can now view the correct answer of the inputted number not the default 5040 . :D
Was This Post Helpful? 0
  • +
  • -

#4 vb5prgrmr  Icon User is offline

  • D.I.C Lover

Reputation: 109
  • View blog
  • Posts: 1,016
  • Joined: 21-March 09

Re: Runtime Error-6 Overflow (Factorial)

Posted 18 October 2009 - 06:30 AM

If not long then look in help for data type summary. Perhaps double or currency will do...



Good Luck
Was This Post Helpful? 1
  • +
  • -

#5 Mezzeric  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 26-July 09

Re: Runtime Error-6 Overflow (Factorial)

Posted 18 October 2009 - 06:48 AM

View Postvb5prgrmr, on 18 Oct, 2009 - 05:30 AM, said:

If not long then look in help for data type summary. Perhaps double or currency will do...



Good Luck



Wooha.. double did the job :wub: Thankies.

but now my last problem is the answer: if i input 55 there's a letter(e.g 1.425324E43) on the answer BOX. :crazy:
Was This Post Helpful? 0
  • +
  • -

#6 ravmonster  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 162
  • Joined: 20-July 09

Re: Runtime Error-6 Overflow (Factorial)

Posted 18 October 2009 - 08:48 AM

View PostMezzeric, on 18 Oct, 2009 - 05:48 AM, said:

View Postvb5prgrmr, on 18 Oct, 2009 - 05:30 AM, said:

If not long then look in help for data type summary. Perhaps double or currency will do...



Good Luck



Wooha.. double did the job :wub: Thankies.

but now my last problem is the answer: if i input 55 there's a letter(e.g 1.425324E43) on the answer BOX. :crazy:



Bos galing nila no...
Was This Post Helpful? 0
  • +
  • -

#7 Mezzeric  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 26-July 09

Re: Runtime Error-6 Overflow (Factorial)

Posted 18 October 2009 - 04:03 PM

@ravmonster LOL i'm still a newbie, so yeah!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1