Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7021251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:25:32+00:00 2026-05-27T23:25:32+00:00

Background: I’m pulling all of the field names from a database into an array

  • 0

Background: I’m pulling all of the field names from a database into an array – I’ve got this part done without a problem, so I already have an array containing all the fields (allfields()) and I have a count of how many fields there are (numfields).

I am now trying to compile all of the unique combinations that can be made from those various field names. For example, if my three fields are NAME, DESCR, DATE, I would want to return the following:

  • NAME, DESCR, DATE
  • NAME, DESCR
  • NAME, DATE
  • DESCR, DATE
  • NAME
  • DESCR
  • DATE

I’ve tried a few different things for this, including multiple nested loops, and modifying the answer here: How to make all possible sum combinations from array elements in VB to fit my needs, but it appears as though I do not have access to the necessary libaries (System or System.Collections.Generic) on my work PC, as it only has VBA.

Does anyone have a bit of VB code kicking around that would fulfill this purpose?

Thanks a lot!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:25:32+00:00Added an answer on May 27, 2026 at 11:25 pm

    I had a similar requirement some years ago. I do not remember why and I no longer have the code but I do remember the algorithm. For me this was a one-off exercise so I wanted an easy code. I did not care about efficiency.

    I will assume one-based arrays because it makes for a marginally easier explanation. Since VBA supports one-based arrays, this should be OK although it is an easy adjustment to zero-based arrays if that is what you want.

    AllFields(1 To NumFields) holds the names.

    Have a Loop: For Inx = 1 To 2^NumFields – 1

    Within the loop consider Inx as a binary number with bits numbered 1 to NumFields. For each N between 1 and NumFields, if bit N is one include AllFields(N) in this combination.

    This loop generates the 2^NumFields – 1 combinations:

    Names: A B C
    
    Inx:          001 010 011 100 101 110 111
    
    CombinationS:   C  B   BC A   A C AB  ABC
    

    The only difficulty with VBA is getting the value of Bit N.

    Extra section

    With everyone having at go at implementing bits of my algorithm, I thought I had better show how I would have done it.

    I have filled an array of test data with an nasty set of field names since we have not been told what characters might be in a name.

    The subroutine GenerateCombinations does the business. I am a fan of recursion but I do not think my algorithm is complicated enough to justify its use in this case. I return the result in a jagged array which I prefer to concatenation. The output of GenerateCombinations is output to the immediate window to demonstrate its output.

    Option Explicit
    

    This routine demonstrates GenerateCombinations

    Sub Test()
    
      Dim InxComb As Integer
      Dim InxResult As Integer
      Dim TestData() As Variant
      Dim Result() As Variant
    
      TestData = Array("A A", "B,B", "C|C", "D;D", "E:E", "F.F", "G/G")
    
      Call GenerateCombinations(TestData, Result)
    
      For InxResult = 0 To UBound(Result)
        Debug.Print Right("  " & InxResult + 1, 3) & " ";
        For InxComb = 0 To UBound(Result(InxResult))
          Debug.Print "[" & Result(InxResult)(InxComb) & "] ";
        Next
        Debug.Print
      Next
    
    End Sub
    

    GenerateCombinations does the business.

    Sub GenerateCombinations(ByRef AllFields() As Variant, _
                                                 ByRef Result() As Variant)
    
      Dim InxResultCrnt As Integer
      Dim InxField As Integer
      Dim InxResult As Integer
      Dim I As Integer
      Dim NumFields As Integer
      Dim Powers() As Integer
      Dim ResultCrnt() As String
    
      NumFields = UBound(AllFields) - LBound(AllFields) + 1
    
      ReDim Result(0 To 2 ^ NumFields - 2)  ' one entry per combination 
      ReDim Powers(0 To NumFields - 1)          ' one entry per field name
    
      ' Generate powers used for extracting bits from InxResult
      For InxField = 0 To NumFields - 1
        Powers(InxField) = 2 ^ InxField
      Next
    
     For InxResult = 0 To 2 ^ NumFields - 2
        ' Size ResultCrnt to the max number of fields per combination
        ' Build this loop's combination in ResultCrnt
        ReDim ResultCrnt(0 To NumFields - 1)
        InxResultCrnt = -1
        For InxField = 0 To NumFields - 1
          If ((InxResult + 1) And Powers(InxField)) <> 0 Then
            ' This field required in this combination
            InxResultCrnt = InxResultCrnt + 1
            ResultCrnt(InxResultCrnt) = AllFields(InxField)
          End If
        Next
        ' Discard unused trailing entries
        ReDim Preserve ResultCrnt(0 To InxResultCrnt)
        ' Store this loop's combination in return array
        Result(InxResult) = ResultCrnt
      Next
    
    End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Background A lot of work has gone into optimizing database design, especially in the
Background / Disclaimer First of all, please feel free to skip this entirely if
Background: I've wrote a small library that is able to create asp.net controls from
Background I've got an NSOutlineView that shows TrainingGroup entities. Each TrainingGroup represents a folder
Background: I have viewed this question as well as this one - sadly, to
[background below] I've got my data modelled out in SQLObject in Python on the
Background This is only my second PyQt4 project. Developing a Windows app that has
Background I've recently been a part of a project where twisted was used. We
Background: This is really a general best-practices question, but some background about the specific
Background This question relates to the new Cirrus infrastructure for Aspect Oriented Programming in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.