Results 1 to 9 of 9
  1. #1
    murfeezlaw is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2012
    Posts
    26

    Using Arguments in a Procedure to do calculation

    Hi all, i'm not the strongest of VBA programmers. I know there is an easier way to do this. I have a Loss Ratio field being calculated if the Loss field or Premium field is updated. I have 18 Loss Ratio fields that are going to be calculated so that means i would need 36 event procedures if i use an event procedure for each. I think i might be able to use my code w an Argument so i can call one procedure at each event procedure but i'm not sure how to do that. Any help would be very much appreciated.


    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Private Sub txtCurrentYearGLLosses_AfterUpdate()

    If Not IsNull(txtCurrentYearGLPremium) And Not IsNull(txtCurrentYearGLLosses) Then
    txtCurrentYearLostRatio = txtCurrentYearGLLosses / txtCurrentYearGLPremium
    End If

    End Sub
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Private Sub txtCurrentYearGLPremium_AfterUpdate()

    If Not IsNull(txtCurrentYearGLPremium) And Not IsNull(txtCurrentYearGLLosses) Then
    txtCurrentYearLostRatio = txtCurrentYearGLLosses / txtCurrentYearGLPremium
    End If

    End Sub
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  2. #2
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Try this (untested):

    Code:
    Private Sub txtCurrentYearGLLosses_AfterUpdate()
       If Not IsNull(txtCurrentYearGLPremium) And Not IsNull(txtCurrentYearGLLosses) Then
          ' Call CalcRatio( PremiunFieldName, LossFieldName, "PutInControlName")
          Call CalcRatio(txtCurrentYearGLPremium, txtCurrentYearGLLosses, "txtCurrentYearLostRatio")
       End If
    End Sub
    
    Private Sub txtCurrentYearGLPremium_AfterUpdate()
       If Not IsNull(txtCurrentYearGLPremium) And Not IsNull(txtCurrentYearGLLosses) Then
          Call CalcRatio(txtCurrentYearGLPremium, txtCurrentYearGLLosses, "txtCurrentYearLostRatio")
       End If
    End Sub
    
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    Sub CalcRatio(pPremium As Single, pLosses As Single, pCtrl As String)
    ' if code behind form
      ' Me(pCtrl) = pLosses / pPremium
    
       ' if code is in a standard module or control is on a different form
          Forms!YourFormName.Controls(pCtrl) = pLosses / pPremium
    
    End Sub
    Change the blue "YourFormName" to your form(s) name(s).....

  3. #3
    John_G is offline VIP
    Windows XP Access 2003
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Hi -

    One caveat to be aware of -

    You are checking for null values, which is good, but if you accidently enter 0 as a value for txtCurrentYearGLPremium, you will get an error for dividing by zero. You will have to handle that possibility as well. (Why would 0 not be a valid value for txtCurrentYearGLPremium ? ).

    John


  4. #4
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Good point John... I should have thought of that!!

  5. #5
    murfeezlaw is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2012
    Posts
    26
    I should have thought of that. Theoretically you can't have a GL claim on a policy with no GL premium however i have seen it entered in some cases by mistake. Good call! Thanks guys. i haven't tried ssanfu's code yet but i probably will later today. Thank you! it looks good!

  6. #6
    murfeezlaw is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2012
    Posts
    26

    Allllmost there

    Quote Originally Posted by ssanfu View Post
    Try this (untested):

    Code:
    Private Sub txtCurrentYearGLLosses_AfterUpdate()
       If Not IsNull(txtCurrentYearGLPremium) And Not IsNull(txtCurrentYearGLLosses) Then
          ' Call CalcRatio( PremiunFieldName, LossFieldName, "PutInControlName")
          Call CalcRatio(txtCurrentYearGLPremium, txtCurrentYearGLLosses, "txtCurrentYearLostRatio")
       End If
    End Sub
    
    Private Sub txtCurrentYearGLPremium_AfterUpdate()
       If Not IsNull(txtCurrentYearGLPremium) And Not IsNull(txtCurrentYearGLLosses) Then
          Call CalcRatio(txtCurrentYearGLPremium, txtCurrentYearGLLosses, "txtCurrentYearLostRatio")
       End If
    End Sub
    
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    Sub CalcRatio(pPremium As Single, pLosses As Single, pCtrl As String)
    ' if code behind form
      ' Me(pCtrl) = pLosses / pPremium
    
       ' if code is in a standard module or control is on a different form
          Forms!YourFormName.Controls(pCtrl) = pLosses / pPremium
    
    End Sub
    Change the blue "YourFormName" to your form(s) name(s).....
    Thank you, this is a big help. I know this is kind of a stupid question but now how do I pass the pCtrl varliable to the txtCurrentYearGLPremium_AfterUpdate procedure to set txtCurrentYearLostRatio equal to pCtrl? i tried using the Me(pCtrl) but it kept giving me a "Object doesn't support this property or method" so i tweaked it as below:

    Private Sub txtCurrentYearGLPremium_AfterUpdate()
    If Not IsNull(txtCurrentYearGLPremium) And Not IsNull(txtCurrentYearGLLosses) Then
    Call CalcRatio(txtCurrentYearGLPremium, txtCurrentYearGLLosses, txtCurrentYearGLLossRatio)
    End If

    txtCurrentYearGLLossRatio = pCtrl


    End Sub

    Sub CalcRatio(pPremium As Single, pLosses As Single, pCtrl As Currency)
    pCtrl = pLosses / pPremium
    End Sub

  7. #7
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    I know this is kind of a stupid question but now how do I pass the pCtrl varliable to the txtCurrentYearGLPremium_AfterUpdate procedure to set txtCurrentYearLostRatio equal to pCtrl? i tried using the Me(pCtrl) but it kept giving me a "Object doesn't support this property or method" so i tweaked it as below:

    Private Sub txtCurrentYearGLPremium_AfterUpdate()
    If Not IsNull(txtCurrentYearGLPremium) And Not IsNull(txtCurrentYearGLLosses) Then
    Call CalcRatio(txtCurrentYearGLPremium, txtCurrentYearGLLosses, txtCurrentYearGLLossRatio)
    End If

    txtCurrentYearGLLossRatio = pCtrl


    End Sub
    Not a stupid question.
    I was trying to get too fancy.

    You would use "Me(pCtrl) = pLosses / pPremium" if the "Sub CalcRatio" code was in the form module.
    You would use "Forms!YourFormName.Controls(pCtrl) = pLosses / pPremium" if the "Sub CalcRatio" code was in a standard module.

    You said
    I have 18 Loss Ratio fields
    so I was using "SUB CalcRatio" to do the calculation, then directly modify the control value.

    Here is new code using a function. It also takes care of Nulls and Zeros.
    ** Note the order of the fields : Losses first, Premium second
    Code:
    Option Compare Database
    Option Explicit  'always need this
    
    'my test 
    Private Sub Loss2_AfterUpdate()
       'control name
       Me.Ratio2 = CalcRatio(Me.Loss2, Me.Prem2)
    End Sub
    
    'my test
    Private Sub Prem2_AfterUpdate()
       'control name
       Me.Ratio2 = CalcRatio(Me.Loss2, Me.Prem2)
    End Sub
    
    
    Private Sub txtCurrentYearGLLosses_AfterUpdate()
       'control name
       Me.txtCurrentYearLostRatio = CalcRatio(Me.txtCurrentYearGLLosses, Me.txtCurrentYearGLPremium)
    End Sub
    
    
    Private Sub txtCurrentYearGLPremium_AfterUpdate()
       'control name
       Me.txtCurrentYearLostRatio = CalcRatio(Me.txtCurrentYearGLLosses, Me.txtCurrentYearGLPremium)
    End Sub
    
    
    '---------------------------
    Function CalcRatio(pLosses, pPremium) As Single
    'return value is type Single
       Dim tmp As Single
       Dim vPrem As Single
    
       'take care of denominator is NULL or zero
       If pPremium = 0 Or IsNull(pPremium) Then
          tmp = 0
       Else
          vPrem = pPremium
          tmp = Nz(pLosses, 0) / vPrem
       End If
    
       'return value
       CalcRatio = tmp
    
    End Function

  8. #8
    murfeezlaw is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2012
    Posts
    26
    Steve, WORKED LIKE A CHARM! Thank you so much for your help on this. I learned quite a bit about passing arguments also. Thank you!

  9. #9
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    You're welcome

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

Similar Threads

  1. Limit to Arguments?
    By ducecoop in forum Access
    Replies: 4
    Last Post: 11-01-2010, 01:52 PM
  2. Skipping optional arguments
    By canfish in forum Programming
    Replies: 3
    Last Post: 08-19-2010, 01:35 PM
  3. HELP - Invalid number of arguments
    By jgelpi16 in forum Programming
    Replies: 3
    Last Post: 07-22-2010, 09:38 AM
  4. Open form arguments
    By nkenney in forum Forms
    Replies: 4
    Last Post: 04-10-2009, 08:40 PM
  5. OpenForm Action and Arguments
    By nkenney in forum Forms
    Replies: 9
    Last Post: 04-05-2009, 09:33 AM

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