Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use a Textbox name in code as a variable

Status
Not open for further replies.

xyxex

Technical User
Sep 4, 2008
10
GB
xyxex (TechnicalUser) 19 Mar 09 7:51
I have a piece of code that checks a value entered in a textbox is numeric.

Private Sub txtTargetInc_Change()
If HasNumber(txtTargetInc) = True Or txtTargetInc = "" Then GoTo OUT
numerror
OUT:
End Sub

I want to re-use this code for all numeric fields by bringing the control name into the procedure as a variable

I have attempted this below but I cannot figure out the syntax for it to work.

Can anyone help?


Textbox is the name of the tetxtbox that i want to test contents of

Private Sub ErrChk(TextBoxName As String)
With Me.Controls(TextBoxName)
If IsNumber(TextBoxName) = True Or .Value = "" Then GoTo oUT
.Value = "0"
numerror ' Calls msgbox
End With
oUT:
PopulateClmLoad
End Sub


Private Sub txtTargetInc_Exit(ByVal Cancel As MSForms.ReturnBoolean)

ErrChk (txtTargetInc)
If txtTargetInc = "" Then GoTo oUT
Range("expprem7").Value = Range("expprem").Value * (1 + Range("TargetInc").Value / 100)
expprem7.Value = Format(Range("expprem7").Value, "£#,###.00")
Adj1.Value = Format(Range("adjq1").Value, "0.00%")
Adj2.Value = Format(Range("adjq2").Value, "0.00%")
Adj3.Value = Format(Range("adjq3").Value, "0.00%")
Adj4.Value = Format(Range("adjq4").Value, "0.00%")
txtuwadjsumm.Value = Format(Range"TotUWadj1").Value, "0.00")
oUT:
End Sub

 
Amend your ErrChk function declaration to:
Code:
Private Sub ErrChk(TextBoxName As MsForms.TextBox)
And then call it like this:
Code:
ErrChk txtTargetInc
Hope this helps

HarleyQuinn
---------------------------------
The most overlooked advantage to owning a computer is that if they foul up there's no law against wacking them around a little. - Joe Martin

Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
what about this ?
Code:
Private Sub ErrChk(TextBoxName As String)
With Me.Controls(TextBoxName)
  If Not IsNumeric(.Value) And .Value <> "" Then
    .Value = "0"
    numerror ' Calls msgbox
  End If
End With
PopulateClmLoad
End Sub

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top