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!

Re-using code where only variable is the control

Status
Not open for further replies.

xyxex

Technical User
Sep 4, 2008
10
GB
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

 
If you change your code to:
Code:
Private Sub ErrChk(TextBoxName As [red]TextBox[/red])
With [red]TextBoxName[/red]
    If IsNumber(TextBoxName) = True Or [red].Text[/red] = "" Then GoTo oUT
    [red].Text[/red] = "0"
That should enable you to do what you're after.

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.
 

I would just add:
Code:
Private Sub ErrChk([blue]ByRef [/blue]TextBoxName As TextBox)
With TextBoxName
    If IsNumber(TextBoxName) = True Or .Text = "" Then GoTo oUT
    .Text = "0"

Have fun.

---- Andy
 
Have tried your suggestions but I am getting an "object" Required error "424".

Appearing here "errchk(txttargetinc)"

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
 

The code you have posted is Excel code, and as such we won't be of too much help here.

I suggest you consult forum707 for an answer to this problem.



I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top