Bit of a generic VBA question but I've yet to discover the answer I'm looking for.
- I have a form with 3 textbox's for RGB input values (TB_CustomRed, TB_CustomGreen and TB_CustomBlue).
- I have the following function which validates that the values entered are only numbers and have a max value of 255:
Private Sub ValidateRGB() Dim lRGB_Max As Long lRGB_Max = 255 With Me.Controls("TB_CustomRed") If Not IsNumeric(.Value) And .Value <> vbNullString Then MsgBox "Sorry, only numbers allowed" ElseIf Val(.Value) > lRGB_Max Then MsgBox "Enter a number between 0 and 255" End If End With End Sub
I call the subroutine in the change event handler like so:
Private Sub TB_CustomRed_Change() ValidateRGB End Sub Private Sub TB_CustomGreen_Change() ValidateRGB End Sub Private Sub TB_CustomBlue_Change() ValidateRGB End Sub
The problem is I have hard-coded the textbox object name at the start of the With statement (obviously it will have no effect for the green and blue text box's) and to avoid duplicating the sub a further 2 times ( or manually writing the control objects name in its change event) I want to instead pass the name of the object as an argument to ValidateRGB however I am unsure I determine the name of the object. Some search results advise to use Screen.ActiveControl.Name however I think that's either part of the office products object libraries or only works within the IDE of those applications so I'm looking for an alternative.