lv2424
lv2424

Reputation: 25

How can I call this sub without passing in actual parameters but using the parameters it already has?

Good afternoon everyone! I've been having difficulty being able to call a sub that has 2 parameters whenever an Option Button is updated because every other time I call this sub I pass in the parameters.

This is the original sub that takes in 2 parameters from a Check Box after update sub which I will add below

     Private Sub showCosts(HS As String, ID As Integer)

One of the subs that pass in the 2 parameters to showCosts

 Private Sub chkH7_AfterUpdate()
 If Me.chkH7.Value <> 0 Then
     Call showCosts("H", 7)
 Else
     Call clearValues
 End If
 End Sub

The Option Button after update sub

 Private Sub optInflated_AfterUpdate()
 'call the function to show the costs when the inflation toggle is updated/clicked

 Call showCosts

 End Sub

What I need this to do is run the sub "showCosts" again after I click the option button with the same parameters it already has. I have started to think maybe I need to incorporate a Dlookup of some sort but after beginning to do some research I found out about ByVal and now I am just all sorts of confused.

Upvotes: 0

Views: 469

Answers (1)

FaneDuru
FaneDuru

Reputation: 42256

Please, try the next way:

  1. Declare two variables on top of a standard module (in the declarations area):
Private prevHS As String, prevID As Integer
  1. Adapt the existing called Sub to accept Optional parameters. Copy the next code in the same standard module where the above variables have been declared. Otherwise, they must be declared As Public:
Private Sub showCosts(Optional HS As String, Optional ID As Integer)
   If HS = "" Then HS = prevHS
   If ID = 0 Then ID = prevID
   prevHS = HS: prevID = ID
   'do here whatever you need...
   Debug.Print HS & " - " & ID
End Sub
  1. Test the idea using the next Sub:
Sub testshowCosts()
   showCosts "Test", 12: Stop 'press F5 when stopped here
   showCosts
End Sub

You can call it with only a parameter, too, the missing one being 'taken' from variables declared on module level, using the previous used value.

Upvotes: 3

Related Questions