Link to home
Start Free TrialLog in
Avatar of ptran2000
ptran2000

asked on

Another array question

I have an array of 32 textboxes,

txtOrder(0)
txtOrder(1)
txtOrder(2).....txtOrder(31)

Users can put a value in these textboxes ranging from 1-32.  However, no two textbox may have the same value once saved.  When a user clicks Save, if there are duplicate values I would like a msgbox stating which values are duplicated.  

If this question should have a higher point value, let me know and I will increase it to 200.

thanks much

Avatar of Erick37
Erick37
Flag of United States of America image

Try this:


Private Sub Command1_Click()
    Dim i As Long, j As Long
    For i = 0 To txtOrder.UBound
        For j = i To txtOrder.UBound
            If i <> j Then
                If (Val(txtOrder(i).Text) = Val(txtOrder(j).Text)) Then
                    MsgBox "Duplicate entries: " & CStr(i) & " " & CStr(j)
                    Exit Sub
                End If
            End If
        Next
    Next
End Sub
Forgot this:
>>I would like a msgbox stating which values are duplicated.

MsgBox "Duplicate entries: " & CStr(i) & " " & CStr(j) & ", Value: " & txtOrder(i).Text
Wont that pop up a message box for every occurrence of a duplicate?
Note
Exit Sub
exits on first occurance of duplicate.
ASKER CERTIFIED SOLUTION
Avatar of Erick37
Erick37
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Bit late this check afterwards. Why no check on the lostfocus or afterupdate event per text box.

1) You don't need a double loop.
2) You don't have to check on more then one duplicate entry
3) the user can react instantly and the cursor can be located on the erroneous field. (perhaps even with highlighting the duplicate!)
Avatar of wsh2
wsh2

ptran2000 writes: "Users can put a value in these textboxes ranging from 1-32'"

As the user is entering the data, you can test for duplicates as they out in.. Catch it at the source. No need to go through the table multi times!!

In the code below.. when a duplicate occurs.. it issues a message, returns focus to the textbox and selects the offending text.

<----- Code Begin ----->

Private Sub Text1_LostFocus(Index As Integer)
   
If Val(Text1(Index).Text) < 1 _
Or Val(Text1(Index).Text) > 32 _
Then
  Text1(Index).SelLength = Len(Text1(Index).Text)
  Text1(Index).SelStart = 0
  Text1(Index).SetFocus
  MsgBox "Values must be between 1 - 32"
  Exit Sub
End If

Dim lngIndex As Long
For lngIndex = 0 To Text1.Count - 1
  If lngIndex <> Index _
  Then
    If Text1(lngIndex).Text = Text1(Index).Text _
    Then
      Text1(Index).SelLength = Len(Text1(Index).Text)
      Text1(Index).SelStart = 0
      Text1(Index).SetFocus
      MsgBox "The Data You Just Entered Is A Duplicate, Please Re-Enter"
      Exit Sub
    End If
  End If
Next lngIndex

End Sub

<----- Code End ----->

A comment to the above solutions:
If the Text1 array is not yet full, then all initialized only (0) fields are gonna match out.. you have to test for this.
Avatar of ptran2000

ASKER

I am testing out these answers to see who gets the points
Thanks Erick37,

!This site has been most helpful!