Question : Very Basic: Run same section of code when clicking on several buttons

I know this is going to be very easy for most, but I've never learned how to put several lines of code somewhere, and then have those lines of code run when clicking on any one of several buttons. All buttons and this section of code only pertain to a pop-up form.

I'm sure it may need to go in some public place with a given name for the code. Then for each button in the OnClick event I would simply tell it somehow to run that code.

The code that I have so far is attached. There will be a few more lines.

Please "dumb-down" your solution. Although I don't consider myself an Access newbie, I can be very dangerous in the VB section.

Thanks, Dale
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
mdWhatIfTI = Me.SliderTI.Value / 100
  Me.TINonSS.Value = mdWhatIfTI
  
  If Me.TINonSS.Value < 1 Then
    mdWhatIfBelow1 = Me.TINonSS.Value * mdOneTrait
  Else
    mdWhatIfBelow1 = mdOneTrait
  End If

  If Me.TINonSS.Value < 1 Then
    mdWhatIfBelow2 = 0
  Else
    If Me.TINonSS.Value < 2 Then
      mdWhatIfBelow2 = (Me.TINonSS.Value - 1) * (mdTwoTrait - mdOneTrait)
    Else
      mdWhatIfBelow2 = mdTwoTrait - mdOneTrait
    End If
  End If

  If Me.TINonSS.Value < 2 Then
    mdWhatIfBelow3 = 0
  Else
    mdWhatIfBelow3 = (mdWhatIfTI - 2) * (mdThreeTrait - mdTwoTrait)
  End If
  Me.WhatIFAPS.Value = Me.SliderAPS / 100
  Me.MonsantoAcres.Value = Me.CurrentAcresTotal * (1 - Me.WhatIFAPS)
  Me.WhatIfAcresSmartStax.Value = Me.MonsantoAcres.Value * Me.SliderSS.Value / 100
  Me.WhatIfAcresNonSmartStax.Value = Me.MonsantoAcres.Value - Me.WhatIfAcresSmartStax.Value
  Me.SmartStaxTotalTraitMargin.Value = Me.WhatIfAcresSmartStax.Value * 0.95 * Me.SmartStaxUnitTraitMargin.Value
  Me.SmartStaxTotalTraitMarginForRefugeAcres = Me.WhatIfAcresSmartStax.Value * 0.05 * mdOneTrait
  Me.SmartStaxTotalTraitMarginPLUSRefugeTraitMargin.Value = Me.SmartStaxTotalTraitMargin.Value + Me.SmartStaxTotalTraitMarginForRefugeAcres
  Me.NonSmartStaxUnitTraitMargin.Value = mdWhatIfBelow1 + mdWhatIfBelow2 + mdWhatIfBelow3
  Me.NonSmartStaxTraitMargin.Value = Me.NonSmartStaxUnitTraitMargin * Me.WhatIfAcresNonSmartStax.Value
  Me.AvgTraitDollarsPerAcre.Value = Me.TotalTraitDollars / Me.MonsantoAcres
  Me.Change.Value = Me.TotalTraitDollars.Value - mdCurrentTotal + mdGPDollars

Answer : Very Basic: Run same section of code when clicking on several buttons

Hello dlogan7,

VBA procedures can call each other.  So...

1) Put your common code into separate procedure:

Sub CommonCode()

'code goes here

End Sub

2) Have your button click events call that sub:

Private Sub MyButton_Click()

    CommonCode
    'or if you prefer:            Call CommonCode

End Sub

Regards,

Patrick
Random Solutions  
 
programming4us programming4us