473,395 Members | 1,670 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Loop through textboxes to find empty then add text

I have a form to assign students to a classroom. On the form is an unbound combobox (cbo_StudentList) that allows user to choose a student. I created a command button (command_AddStudent). I also have 40 textboxes (bound to a table) where I want students assigned to the class to be listed.

I need to figure out how to add the combobox values to the next empty textbox when the command button is clicked. I tried loop and if statements based on a tag assigned to the 40 textboxes but it adds the student name to all 40 textboxes. If I select a different student and click the command button it overwrites all 40 with the new name.

Is there a way to keep the students assigned to the class in textboxes, loop through them to find the next empty box and fill it (and only it) with the new name. See code below. Note I am recording the first and last names of the students that appear in column 3 & 2 of the query for the combobox.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command_AddStudent_Click()
  2. Dim ctl As Control
  3. Dim addstudent As String
  4. Dim student As String
  5.  
  6. addstudent = Me.Cbo_StudentList.Column(3) & " " & Me.Cbo_StudentList.Column(2)
  7.  
  8. For Each ctl In Me.Controls
  9.      If ctl.tag = "SeatsAvailable" Then
  10.         If ctl.Value = Not Null Then
  11.           ctl.Value = student
  12.           Else
  13.           ctl.Value = addstudent
  14.         End If
  15.     End If
  16. Next ctl
  17. End Sub
Apr 29 '13 #1

✓ answered by TheSmileyCoder

Hi and Welcome to Bytes

Nice and detailed explanation for a first time poster, I hope we will hear more from you :)
First:
You cannot write this:
Expand|Select|Wrap|Line Numbers
  1. If ctl.Value = Not Null
You can try either:
Expand|Select|Wrap|Line Numbers
  1. If not IsNull(ctl.value) then
or
Expand|Select|Wrap|Line Numbers
  1. If ctl.Value & ""<>"" then
The latter of the two also checks for zero length strings.

Secondly, I hope you realize that it seems your design does not follow best practice in regards to Database Normalization. I strongly urge you to take the time to read through the article, it will save you alot of grief down the road.

Third your code used the string variable Student, yet it is never assigned a value.

Now finally moving on to your question, looping through the controls should certainly be an option.

Expand|Select|Wrap|Line Numbers
  1. 'Loop through all controls on form
  2.   For Each ctl In Me.Controls
  3.     'Check for seat tag
  4.     If ctl.tag = "SeatsAvailable" Then
  5.       'Is seat taken?
  6.         if ctl.value & ""<>"" Then
  7.           'Seat available, fill seat
  8.           ctl.value=AddStudent
  9.         End If
  10.     End If
  11.   Next ctl

4 7937
TheSmileyCoder
2,322 Expert Mod 2GB
Hi and Welcome to Bytes

Nice and detailed explanation for a first time poster, I hope we will hear more from you :)
First:
You cannot write this:
Expand|Select|Wrap|Line Numbers
  1. If ctl.Value = Not Null
You can try either:
Expand|Select|Wrap|Line Numbers
  1. If not IsNull(ctl.value) then
or
Expand|Select|Wrap|Line Numbers
  1. If ctl.Value & ""<>"" then
The latter of the two also checks for zero length strings.

Secondly, I hope you realize that it seems your design does not follow best practice in regards to Database Normalization. I strongly urge you to take the time to read through the article, it will save you alot of grief down the road.

Third your code used the string variable Student, yet it is never assigned a value.

Now finally moving on to your question, looping through the controls should certainly be an option.

Expand|Select|Wrap|Line Numbers
  1. 'Loop through all controls on form
  2.   For Each ctl In Me.Controls
  3.     'Check for seat tag
  4.     If ctl.tag = "SeatsAvailable" Then
  5.       'Is seat taken?
  6.         if ctl.value & ""<>"" Then
  7.           'Seat available, fill seat
  8.           ctl.value=AddStudent
  9.         End If
  10.     End If
  11.   Next ctl
Apr 29 '13 #2
NeoPa
32,556 Expert Mod 16PB
You may also want to introduce a concept of the order the fields, or controls on the form, appear in. The order from the collection may not match that which you want to assign values in.
Apr 30 '13 #3
Thanks for the advice and help. I was struggling on how to structure this table among the others I created. The article made a lot of sense so back to the drawing board for me.
Apr 30 '13 #4
TheSmileyCoder
2,322 Expert Mod 2GB
Your welcome.
Its a pleasure answering structured and detailed questions.
Apr 30 '13 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: tony collier | last post by:
hi i have various labels on my page with ID's label_1, label_2 , label_3 etc. How can i write a loop that sequentially changes their text values to values i have in an array eg;. for...
1
by: JenHu | last post by:
Hi experts, I want to create a new empty text file after I upload a file to the desination. Then I need to read each line from the uploaded file and write the lines which first character <>'6'...
1
by: amber.inc | last post by:
HI, I would like to know how can i find a text of a PDF file and change the forecolor of the text to blue with the help of JAVASCRIPT or any other language so that it can work on Acrobat 5.0....
1
Steve Kiss
by: Steve Kiss | last post by:
I have a site in which there are two asp textboxes as follows: <asp:TextBox runat="server" ID="UserName" /> <asp:TextBox id="Email" runat="server" ...
1
by: gravikrishna | last post by:
hello sir pls tell me the code of my question how do u find total text of a html page in .net
8
by: daitasri | last post by:
Hi I m using javascript in HTML. I want to know how to validate the text entered in a textbox (for empty strings). My requirement is that my code should not allow empty text (not even spaces...
14
by: Mikhail Teterin | last post by:
Hello! What's would be the syntax for a query, which would allow me to get only the elements with non-empty text-nodes? For example, from: <a><b></b></a> <c/> <d><e>meow</e></d>
1
by: Malvika Shrivastava | last post by:
Hi everyone, I want to find the text which is highlighted with a particular colour in a word document. Thanks, Malvika.
3
by: Laneyshia | last post by:
Hello, So I finished my code but I have this one problem. The Table That Is To Start The Loop Is Reading Empty. Below is all the code to the program.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.