Solved

Another array question

Posted on 2000-03-23
9
155 Views
Last Modified: 2010-05-18
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

0
Comment
Question by:ptran2000
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 32

Expert Comment

by:Erick37
ID: 2650731
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
0
 
LVL 32

Expert Comment

by:Erick37
ID: 2650739
Forgot this:
>>I would like a msgbox stating which values are duplicated.

MsgBox "Duplicate entries: " & CStr(i) & " " & CStr(j) & ", Value: " & txtOrder(i).Text
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2650793
Wont that pop up a message box for every occurrence of a duplicate?
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 32

Expert Comment

by:Erick37
ID: 2650804
Note
Exit Sub
exits on first occurance of duplicate.
0
 
LVL 32

Accepted Solution

by:
Erick37 earned 100 total points
ID: 2650840
New and improved:

Private Sub Command1_Click()
    Dim i As Long, j As Long
    For i = 0 To txtOrder.UBound - 1
        For j = i + 1 To txtOrder.UBound
            Debug.Print i; j
            If (Val(txtOrder(i).Text) = Val(txtOrder(j).Text)) Then
                MsgBox "Duplicate entries: " & CStr(i) & " & " & CStr(j) & ", Value: " & txtOrder(i).Text
                Exit Sub
            End If
        Next
    Next
End Sub
0
 
LVL 54

Expert Comment

by:nico5038
ID: 2651096
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!)
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2651136
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.
0
 

Author Comment

by:ptran2000
ID: 2651264
I am testing out these answers to see who gets the points
0
 

Author Comment

by:ptran2000
ID: 2651333
Thanks Erick37,

!This site has been most helpful!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How does CurrentUser work? 10 43
Search combo error "Data Type Mismatch in Criteria Expression" 2 94
Child Form in front 4 65
VB 6 error 5 in windows 10 but not in XP 7 72
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question