Solved

# checking Integer uniqueness

Posted on 2004-04-15
161 Views
I am currently doing an assignment which i am creating a program which will check all Lotto draws from a comma delimeted text file.  On the GUI i have 6 Combo boxes which display numbers 1 - 45  somehow i need to come up with a way to check that when the user selects a number, it will then update the available comboboxes so the user cannot select the same number twice.  Does anyone have any ideas.

I as thinking of an array with a nested for loop which compares the values and then sets a boolen to false or true however i cannot do this  because it will check itself and therefore send false result.

Anyone have any ideas....

Thanks
0
Question by:innovisiontechnology
[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

LVL 52

Expert Comment

ID: 10831693
The easiest way maybe is using a ListBox, after you randomly get a Number, just delete the item from the ListBox, and then the same Number wont occurs twice. Understand?
0

LVL 52

Expert Comment

ID: 10831717
Example:

Private Sub Command1_Click()
Dim pos As Integer, myDraw As String
Randomize
myDraw = ""
For i = 1 To 6
pos = Int(Rnd * List1.ListCount)
myDraw = myDraw & List1.List(pos) & " "
List1.RemoveItem pos
Next i
myDraw = Trim\$(myDraw)
MsgBox "My Draw is : " & myDraw
End Sub

List1.Clear
For i = 1 To 45
Next i
List1.Visible = False
End Sub

cheers
0

LVL 5

Expert Comment

ID: 10831855
Or if you still want to use combo boxes:

Dim i As Integer
'Populate comboxes with numbers
For i = 1 To 30
Next i
End Sub

Private Sub Combo1_Click()
'Deletes item clicked in combo1 from combo2
Combo2.RemoveItem (Combo1.ListIndex)
End Sub

Does this help?
0

LVL 86

Accepted Solution

Mike Tomlinson earned 25 total points
ID: 10832968
I think removing the currently selected number from the other comboboxes/listboxes is a bad idea.

What happens when the user changes their mind?  Now you have to track what number was in each box before it was changed, and then put that old number back into all of their selection pools in the correct order!

The user needs to press a button to confirm their selections.  It is then that you should validate their selection choices with a function.

Private Function selectedNumbersAreValid() As Boolean
Dim i As Integer
Dim a As Integer

For i = Combo1.LBound To Combo1.UBound
For a = Combo1.LBound To Combo1.UBound
If a <> i Then
If Combo1(i).Text = Combo1(a).Text Then
selectedNumbersAreValid = False
Exit Function
End If
End If
Next a
Next i
selectedNumbersAreValid = True
End Function

I think the most intuitive way to do this is to use only two listboxes.  Load up the first listbox with your numbers.  Move the numbers back and forth between the boxes when they are double clicked like this:

Option Explicit

Dim i As Integer

For i = 1 To 45
Next i
End Sub

Private Sub List1_DblClick()
Dim selIndex As Integer
selIndex = List1.ListIndex
If selIndex <> -1 Then
If List2.ListCount < 6 Then
List1.RemoveItem selIndex
End If
End If
End Sub

Private Sub List2_DblClick()
Dim selIndex As Integer
Dim value As Integer
Dim i As Integer
Dim v As Integer

selIndex = List2.ListIndex
If selIndex <> -1 Then
value = CInt(List2.List(selIndex))
List2.RemoveItem selIndex
For i = 0 To List1.ListCount - 1
v = CInt(List1.List(i))
If value < v Then
Exit Sub
End If
Next i
End If
End Sub

Private Sub Command1_Click()
If List2.ListCount = 6 Then
MsgBox "Thank you"
Else
MsgBox "You must select six numbers"
End If
End Sub

Regards,

Idle_Mind
0

LVL 17

Assisted Solution

zzzzzooc earned 25 total points
ID: 10839203
or.. just prevent numbers from being selected that are currently in use. The Validate() event fires after focus has left and data is changed so you can check there. If you change the event's Cancel parameter to true, it'll force the user to select a different value.

Form1:
-----------------

Private Sub Combo1_Validate(Index As Integer, Cancel As Boolean)
Dim iNum As Integer, iLoop1 As Integer
'If the ComboBox value is numeric.. (ie: not "Select a number")
If IsNumeric(Combo1(Index).List(Combo1(Index).ListIndex)) = True Then
iNum = Combo1(Index).List(Combo1(Index).ListIndex)
'check if the new number is the same as any other
For iLoop1 = Combo1.lBound To Combo1.UBound
'If the current combobox isn't the same as the one
'that's changing.. (avoid checking itself)
If iLoop1 <> Index Then
'If a number was select in this box already, check it..
If Combo1(iLoop1).ListIndex <> -1 Then
'If the number is the same as the one we're using..
If CInt(Combo1(iLoop1).List(Combo1(iLoop1).ListIndex)) = iNum Then
'Cancel it..
Cancel = True
Call MsgBox("Select a different number.")
Exit Sub
End If
End If
End If
Next iLoop1
End If
End Sub
Dim iLoop1 As Integer, iLoop2 As Integer
For iLoop1 = Combo1.lBound To Combo1.UBound
Combo1(iLoop1).Text = "Select a number"
For iLoop2 = 1 To 10
Next iLoop2
Next iLoop1
End Sub
0

LVL 3

Expert Comment

ID: 10840589
Try this, just add a blank item at the combo boxes.

Private Sub cmbNumber_Click(Index As Integer)
Dim i               As Integer
Dim blnFound        As Boolean
i = cmbNumber.LBound
Found = False
While Not (i > cmbNumber.UBound) _
And Not blnFound
'Dont compare combobox against itself or if it space
If Not i = Index Then
If Not cmbNumber(i) = " " Then
If cmbNumber(i).Text = cmbNumber(Index).Text Then
cmbNumber(Index).Text = " "
blnFound = True
End If
End If
End If
i = i + 1
Wend
End Sub
0

## Featured Post

Question has a verified solution.

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

### Suggested Solutions

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
###### Suggested Courses
Course of the Month5 days, 5 hours left to enroll