Passing Values to Function

  • Thread starter Thread starter Newbie
  • Start date Start date
N

Newbie

I have a set of textbox controls on a userform called n1,
n2 etc.

I have a common function that needs to handle each of
these controls after they are updated.

I want the name (n1, etc..) to be passed to the function
and substituted into the code that then runs against this
control. How to I pass the control name into the function?
What I have does not work!

' control n1
Private Sub n1_AfterUpdate()
ValidateNumber("n1")
End Sub

' control n2
Private Sub n2_AfterUpdate()
ValidateNumber("n2")
End Sub

'Function (example to test value passed
Private Function ValidateNumber(nBox)
MsgBox nBox.Value
End Function
 
--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

Newbie said:
I have a set of textbox controls on a userform called n1,
n2 etc.

I have a common function that needs to handle each of
these controls after they are updated.

I want the name (n1, etc..) to be passed to the function
and substituted into the code that then runs against this
control. How to I pass the control name into the function?
What I have does not work!


You have a couple of choices. You can pass the name to the function as you
currently do and access it through the controls collection, like

Private Function ValidateNumber(nBox)
MsgBox UserForm1.Controls(nBox).Value
End Function


or pass the textbox object and access the text property, like

' control n1
Private Sub n1_AfterUpdate()
ValidateNumber n1
End Sub

' control n2
Private Sub n2_AfterUpdate()
ValidateNumber n2
End Sub

Private Function ValidateNumber(nBox As MSForms.TextBox)
MsgBox nBox.Value
End Function

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
Thanks Bob, very helpful

Bob Phillips said:
--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)




You have a couple of choices. You can pass the name to the function as you
currently do and access it through the controls collection, like

Private Function ValidateNumber(nBox)
MsgBox UserForm1.Controls(nBox).Value
End Function


or pass the textbox object and access the text property, like

' control n1
Private Sub n1_AfterUpdate()
ValidateNumber n1
End Sub

' control n2
Private Sub n2_AfterUpdate()
ValidateNumber n2
End Sub

Private Function ValidateNumber(nBox As MSForms.TextBox)
MsgBox nBox.Value
End Function

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
Back
Top