Results 1 to 7 of 7
  1. #1
    Sierra Mike is offline Novice
    Windows 8 Access 2010 64bit
    Join Date
    Feb 2014
    Posts
    8

    VBA code for probability

    Dear all,

    I have a table with three fields FR (Field Type Double Scientific), Time (Field type long integer general), Quantity (Field Type long integer general).
    Now i have made a form which has all the fields of the table bound to it. in this form i have made a button called calculate.

    what i want is .......
    when i enter the FR and Time values and click the calculate button it should display the x value in the quantity field on the form. the formula is

    summation (from x=1 to n)[ (FR*Time)x * (e(-FR*time))/ x!] >= 0.9
    (sigma function)

    basically, the code should return the value of x the moment expression value goes above 0.9.

    the code i have used is as follows:

    Private Sub Command7_Click()

    Dim Prob As Double
    Dim x, fact As Long


    x = 1
    fact = 1




    For i = 1 To x
    fact = fact * i
    Next i


    Prob = ((Me.FR * Me.Time) ^ x) * Exp(-(Me.FR * Me.Time)) / fact


    Do While Prob < 0.9
    x = x + 1


    For i = 1 To x
    fact = fact * i
    Next i


    Prob = Prob + ((Me.FR * Me.Time) ^ x) * Exp(-(Me.FR * Me.Time)) / fact


    Loop


    Me.Quantity = x

    End Sub


    I am getting an error in the bold underlined line of the code as overflow.

    Can anyone help. this is really very urgent and important.

    thanks

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    What do the variables contain at the point it errors?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,632
    Provide examples of FR and Time data. I tested with values 0.1 and 4. The variable fact reaches 597196800 and errors long before Prob could reach 0.9.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  4. #4
    Sierra Mike is offline Novice
    Windows 8 Access 2010 64bit
    Join Date
    Feb 2014
    Posts
    8
    thanks for your help............some of the sample values of Time are 25920 (for all) and FR are

    FR

    4.21*10^-6
    4.16*10^-6
    4.80*10^-6
    8.66*10^-6

    and X value i.e answer should be (approx)
    0
    0
    3
    1

  5. #5
    Sierra Mike is offline Novice
    Windows 8 Access 2010 64bit
    Join Date
    Feb 2014
    Posts
    8
    dear June7,
    Request if you could plz send me the code you wrote............even i can try at my end....
    thanks

  6. #6
    orange's Avatar
    orange is online now Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,849
    Mike,

    Small points

    Dim x, fact As Long
    This does not do what you think.

    You must explicitly define variables.

    Dim x as Long, fact As Long OR

    Dim x as Long
    Dim fact as Long

    Time is a reserved word in Access

    You might add a Debug.Print to monitor some variables.

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,632
    All I did was copy/paste your procedure then replace all the Me.FR and Me.Time references to declared variables and set the variables. And changed Me.Quantity = to Debug.Print.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 2
    Last Post: 06-28-2013, 12:58 PM
  2. Report Code is not allowing return to main code
    By rcwiley in forum Programming
    Replies: 2
    Last Post: 06-16-2013, 10:31 AM
  3. Replies: 7
    Last Post: 05-28-2013, 09:11 AM
  4. Replies: 1
    Last Post: 05-04-2013, 12:19 PM
  5. assigning probability
    By TheShabz in forum Programming
    Replies: 1
    Last Post: 05-06-2010, 12:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums