440,780 Members | 1,118 Online
Need help? Post your question and get tips & solutions from a community of 440,780 IT Pros & Developers. It's quick & easy.

# Checking if multiple textboxes have equal value??

 P: 5 How can i check if multiple textboxes have the same value?? Sure, i can do a bazillion if and else commands, but im sure it can be done in some easier way. For example, i have 3 (lets say three to make it easy atm) textboxes where the user inputs a value in each, and it checks if any of these are equal to eachother? This is something that i have been wondering about for quite some time =) Nov 7 '06 #1
7 Replies

 100+ P: 1,646 How can i check if multiple textboxes have the same value?? Sure, i can do a bazillion if and else commands, but im sure it can be done in some easier way. For example, i have 3 (lets say three to make it easy atm) textboxes where the user inputs a value in each, and it checks if any of these are equal to eachother? This is something that i have been wondering about for quite some time =) Hi. You can use the Select Case test which would be easier to read but it would only return the first equality and ignore the rest. You would need n - 1 Select blocks for n textboxes. e.g. 4 textboxes... Expand|Select|Wrap|Line Numbers Select Case CInt(txt1.Text)    Case CInt(txt2.Text)        blah blah blah    Case CInt(txt3.Text)       blah blah blah    Case CInt(txt4.Text)       blah blah blah End Select   Select Case CInt(txt2.Text)    Case CInt(txt3.Text)       blah blah blah    Case CInt(txt4.Text)       blah blah blah End Select   Select Case CInt(txt3.Text)    Case CInt(txt4.Text)       blah blah blah End Select   Nov 7 '06 #2

 Expert 5K+ P: 8,434 Hi. You can use the Select Case test which would be easier to read but it would only return the first equality and ignore the rest. You would need n - 1 Select blocks for n textboxes. e.g. 4 textboxes... Blah blah blah... :) If the textboxes are in an array then you could use a couple of nested loops. The advantage is that the code is not dependent on the number of controls. This isn't tested, just typed straight here, but I'm thinking something like Expand|Select|Wrap|Line Numbers Dim I as Long, J as long ' Assuming number of textboxes is in variable Count... For I = 1 to Count - 1   For J = I + 1 to Count     If TextBox(J) = TextBox(I) Then       ' They are equal - do something.     End If   Next Next Note, the I + 1 prevents you from repeating the same comparisons. Wouldn't do any harm, it's just inefficient. Note also, I've started my numbering from 1, but in fact constrol arrays normally start with index = 0. I just hate numbering from zero. :) Nov 7 '06 #3

 P: 5 Thanks alot, the arrays worked great :D Nov 8 '06 #4

 P: 1 How can I find out how many textboxes I have? Can you count them during runtime? Nov 24 '06 #5

 Expert 5K+ P: 8,434 How can I find out how many textboxes I have? Can you count them during runtime? If you mean the number of them in the array, you can just retrieve it from the .Count property. For example, if the text boxes are called Text1, then Text1.Count will tell you the number of occurences. Keep in mind that by default, the indexes start from 0, so if .Count is 3 for instance, then they will be numbered from 0 to 2. If you mean the number of textboxes in general on the form, then while there are probably better ways, you could try this sample code I just threw into a form in a test project. Expand|Select|Wrap|Line Numbers Private Sub Form_Click()   Dim c As Control, i As Long   For Each c In Me.Controls     If TypeOf c Is TextBox Then       Debug.Print "Here's one: "; c.Name       i = i + 1     End If   Next   Debug.Print "Found "; Format(i); ' text box(es).' End Sub Nov 25 '06 #6

 100+ P: 1,646 How can I find out how many textboxes I have? Can you count them during runtime? Of course the easy way is to look at the form and count them :) Nov 25 '06 #7

 Expert 5K+ P: 8,434 Of course the easy way is to look at the form and count them :) Of course, but it's so primitive. :) Nov 25 '06 #8