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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

refering to a field in a variable 1

Status
Not open for further replies.

simian336

Programmer
Sep 16, 2009
723
US
vb 2010 - How do you refer to a form field in a variable.

If I have 10 fields like Me.SpreadAmt1, Me.SpreadAmt2, etc. and I want to blank them (or whatever) without typing each one out..

Dim x As Integer
Dim ha As Object '(just guessing here)

x = 1
For x = 1 To 10
ha = "Me.SpreadAmt" + x.ToString + ".Text"
(ha) = ""
Next

Hope this is clear enough.

Thanks

Simi
 

This FAQ:

faq796-5698

Shows how to get a control by its name. Using this you can do what you want

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!
 
Whilst jebenson is correct, there is a much easier way since you know the names of the controls you wish to work on.

I've created a simple form with three text boxes named TextBox1, TextBox2 & TextBox3 and a button. The following codes puts the text "This works" in TextBox1 and TextBox2 only, leaving TextBox3 blank.

Code:
	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

		For a As Integer = 1 To 2
			Me.Controls.Item("TextBox" & a.ToString).Text = "This works"
		Next

	End Sub

If the text boxes you wish to control are in a container control then just extend the [tt]Me.Controls[/tt] to incorporate your container control before the [tt].Item("TextBox" & a.ToString).Text = "This works"[/tt]
 
Well I'm a dummy... Any hour later, I figured out that my textbox was in a tabbed page...

myControl = Me.TabPage1.Controls.Item(ControlName)

Thank you very much, you got me going down the correct path.

Simi

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top