3 Replies - 6478 Views - Last Post: 01 May 2011 - 04:19 PM Rate Topic: -----

#1 Cheater23  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 18-October 10

Using a variable for a textbox name

Posted 01 May 2011 - 03:48 PM

Is it possible to use a variable for text box names?
I have 25 student name, and grade boxes that need to appear and disappear depending on how large the student number was given to start with.

So far i have had to use this if\then statement 25 times.

If txtStudentNum.Text = "" Then
            txtStudent1.Hide()
            txtGrade1.Hide()
       ElseIf txtStudentNum.Text > 0 Then
            txtStudent1.Show()
            txtGrade1.Show()
End If



But i was Hoping I could do something like. Unfortunately this does not work because the name of the text box couldn't be declared with a variable.

for intX = 1 to 25
If txtStudentNum.Text = "" Then
            txtStudent(intX).Hide()
            txtGrade(intX).Hide()
       ElseIf txtStudentNum.Text > txtStudentNum.Text Then
            txtStudent(intX).Show()
            txtGrade(intX).Show()
End If
next



So, my question is, how would one go about cleaning up all of those if\then statements to clean up the clutter of the code?

Is This A Good Question/Topic? 0
  • +

Replies To: Using a variable for a textbox name

#2 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1043
  • View blog
  • Posts: 4,057
  • Joined: 02-July 08

Re: Using a variable for a textbox name

Posted 01 May 2011 - 03:59 PM

Me.Controls("txtStudent" & <student number>.ToString) would work you have the student number. This method is using a string name to find the control.
Was This Post Helpful? 0
  • +
  • -

#3 Cheater23  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 18-October 10

Re: Using a variable for a textbox name

Posted 01 May 2011 - 04:16 PM

Thank you very much =]
Was This Post Helpful? 0
  • +
  • -

#4 Fuingurth  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 118
  • Joined: 13-August 09

Re: Using a variable for a textbox name

Posted 01 May 2011 - 04:19 PM

As hawkvalley has stated, using the following code in my experience should work. This adds the value of the textbox to the the string that would be names "txtStudent" with the student number after it. This requires a new variable named studentNumber which you will dynamically assign values to. So if studentNumber was one (1) then the value of the current textbox would be assigned to the string txtStudent1. If studentNumber was two(2), three(3), etc then it would be assigned to textStudent2, textStudent3 and so on.

Me.Controls("txtStudent" & <studentNumber>.ToString

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1