code to help with maths problem

Posted on 2006-05-18
Last Modified: 2010-05-01
Hi experts,
I have a tricky problem that I don't know how to attack:

I need a routine that creates all the possible numbers from a set of individual digits.
For example, suppose you have the digits 1, 3 and 5.
Possible numbers are:
You can only use each digit once per number.

I'm not sure how to start - if you can point me in the right direction, I would be grateful.
I am comfortable using VB in MS Access - my preferred environment.

Thanks in advance.
Question by:ajdunne
    LVL 44

    Expert Comment

    will you always have 3 digits?  and will they always be different (Digit1 <> Digit2 <> Digit3)?


    Author Comment

    Ideally, I want a solution that I can extend to 3, 4, 5 or 6 digits, but each case is treated seperately, i.e.
    case 1 is always 3 digits
    case 2 is always 4 digits, etc

    In all cases, the digits are unique:  Digit1 <> Digit2 <> Digit3 [<> Digit4...]

    LVL 18

    Expert Comment

    Option Explicit

    Private Sub Form_Load()

    'here 4 digits, increase array to how many digits you want

    Dim digits(1 To 4) As String
    digits(1) = "3"
    digits(2) = "1"
    digits(3) = "3"
    digits(4) = "4"

    Dim out() As String

    digitizer digits, out

    Dim i As Integer
    For i = 1 To UBound(out)

        Debug.Print i, out(i)


    End Sub

    Private Sub digitizer(digits() As String, numbers() As String)
        Dim digits2() As String
        Dim numbers2() As String
        Dim numbers3() As String
        Dim i As Long
        Dim j As Long
        Dim k As Long
        Dim l As Long
        Dim ni As Long
        Dim sPivot As String
        Dim sNew As String
        If UBound(digits) = 1 Then
            ReDim numbers(1)
            numbers(1) = digits(1)
            ReDim digits2(1 To UBound(digits) - 1)
            ReDim numbers3(0 To 0)
            For i = 1 To UBound(digits)
                k = 0
                For j = 1 To UBound(digits)
                    If i <> j Then
                        k = k + 1
                        digits2(k) = digits(j)
                    End If
                sPivot = digits(i)
                digitizer digits2, numbers2
                For l = 1 To UBound(numbers2)
                    For j = 0 To UBound(digits2)
                        sNew = Left(numbers2(l), j) + sPivot + Mid(numbers2(l), j + 1)
                        ni = UBound(numbers3) + 1
                        ReDim Preserve numbers3(ni)
                        numbers3(ni) = sNew
            Dim bSwap As Boolean
            Dim sSwap As String
                bSwap = False
                For j = 1 To UBound(numbers3) - 1
                    If numbers3(j) > numbers3(j + 1) Then
                        sSwap = numbers3(j)
                        numbers3(j) = numbers3(j + 1)
                        numbers3(j + 1) = sSwap
                        bSwap = True
                    End If
            Loop Until Not bSwap
            k = 0
            For j = 1 To UBound(numbers3)
                If numbers3(j) <> numbers3(j - 1) Then
                    k = k + 1
                    ReDim Preserve numbers(k)
                    numbers(k) = numbers3(j)
                End If
        End If

    End Sub
    LVL 18

    Expert Comment

    if you run it with the setup below, you should get 120 combinations, which is 6!/3!=720/6=120

    Dim digits(1 To 6) As String
    digits(1) = "1"
    digits(2) = "3"
    digits(3) = "5"
    digits(4) = "8"
    digits(5) = "8"
    digits(6) = "8"

    Dim out() As String

    digitizer digits, out

    Dim i As Integer
    For i = 1 To UBound(out)

        Debug.Print i, out(i)


    LVL 18

    Expert Comment

    I feel my posting was a correct answer.

    Author Comment

    Hi deighton,
    Thanks for your feedback on this, but I could not make your code work.
    Sorry, probably my fault.

    I did finally find an excel spreadsheet that contained the vba code I needed.
    I found it at

    Experts, may I request a refund of these points.

    LVL 18

    Expert Comment

    sorry missed the comment about access

    Accepted Solution

    PAQed with points refunded (125)

    Community Support Moderator

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
    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…

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now