On Jul 27, 10:07 am, Bails <andrewgbail...@gmail.comwrote:
On Jul 23, 5:33 pm, The Frog <Mr.Frog.to....@googlemail.comwrote:
Hi Bails,
This is some code I wrote a long time ago in MS Access. It is a
function that creates all combinations of n objects from m objects and
places them into a table using classic ADO. It also returns the total
to the whatever called the function.
You would need to update this a bit if you want to use .Net, as the
ADO is quite different. Anyway, it should give you the logic needed to
solve your problem. You could always just run it in MS Access too....
Function Spin(n As Byte, m As Byte, ByRef rs As ADODB.Recordset) As
Long
Dim base() As Byte
Dim limit() As Byte
Dim Terminate As Boolean
Dim increment As Byte
Dim total As Long
ReDim base(n)
ReDim limit(n)
For i = 1 To n
base(i) = i
Next i
counter = m
For i = n To 1 Step -1
limit(i) = counter
counter = counter - 1
Next i
Do While Terminate = False
increment = 0
total = total + 1
rs.AddNew
For i = 1 To n
With rs.Fields
snarf$ = "Ball" & i
.Item(snarf) = base(i)
End With
Next
rs.Update
For i = 1 To n
If base(i) = limit(i) Then
increment = increment + 1
End If
Next
If increment = n Then
Terminate = True
ElseIf increment 0 Then
base(n - increment) = base(n - increment) + 1
For i = (n - increment + 1) To n
base(i) = base(i - 1) + 1
Next
Else
base(n) = base(n) + 1
End If
Loop
Spin = total
End Function
Enjoy, and good luck. If you manage to win at lotto then buy me a beer
next time I'm in Aus :-)
Cheers
The Frog
Thanks Frog, if I actually do win anything substantial, I'll buy you
more than a beer - lol.
Now for another DUMB question!!!!!!
I havent used access in a long time, where should I actually place the
code?? I tried adding it to a command button on a user form but only
get errors. Im using MS Access 2003 with SP2.
Cheers- Hide quoted text -
- Show quoted text -
Hi, someone suggested to generate ALL the combinations that I use 6
for next loops and check for duplicates. I thought that sounded
simple, but I cant seem get it right. I have posted the code below, if
anyone can tell me what I am doing wrong I would appreciate it. Ignore
the timer code, I was just checking to see how long it takes to
generate the numbers.
I used a text box to input the number of balls (ultimately it will be
45), but as it takes Sooooooooo long to process, I thought this would
be easier so that I could test with a smaller number say 10 - even
this takes a while so I might need to streamline the code to do 45.
Anyway here it is - any help would be greatly appreciated..........
Public Class Form1
Dim nCount As Integer
Dim startTime As DateTime
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
startTime = DateTime.Now()
'Me.Text = "Click to Stop"
Timer1.Start()
'Dim nCount As Long
Dim N1 As Long
Dim N2 As Long
Dim N3 As Long
Dim N4 As Long
Dim N5 As Long
Dim N6 As Long
ListBox1.Items.Clear()
nCount = Int(Val(TextBox1.Text))
If nCount < 6 Then nCount = 6
For N1 = 1 To nCount
For N2 = 1 To nCount
If N2 = N1 Then N2 = N2 + 1
If N2 >= nCount Then N2 = nCount
For N3 = 1 To nCount
If N3 = N2 Or N3 = N1 Then N3 = N2 + 1
If N3 >= nCount Then N3 = nCount
For N4 = 1 To nCount
If N4 = N3 Or N4 = N2 Or N4 = N1 Then N4 = N3
+ 1
If N4 >= nCount Then N4 = nCount
For N5 = 1 To nCount
If N5 = N4 Or N5 = N3 Or N5 = N2 Or N5 =
N1 Then N5 = N4 + 1
If N5 >= nCount Then N5 = nCount
For N6 = 1 To nCount
If N6 = N5 Or N6 = N4 Or N6 = N3 Or N6
= N2 Or N6 = N1 Then N6 = N5 + 1
If N6 >= nCount Then N6 = nCount
Application.DoEvents()
ListBox1.Items.Add(N1 & ", " & N2 & ",
" & N3 & ", " & N4 & ", " & N5 & ", " & N6)
Next
Next
Next
Next
Next
Next
Timer1.Stop()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Timer1.Tick
Dim span As TimeSpan = DateTime.Now.Subtract(startTime)
Label31.Text = span.Minutes.ToString & ":" & _
span.Seconds.ToString & "." & span.Milliseconds
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button2.Click
Exit Sub
End Sub