- Community Pick
- Experts Exchange Approved
- Editor's Choice
- 1
Introduction
A common request in the various database zones at Experts Exchange involves returning a concatenated list of the various items in ColumnB for each distinct value in ColumnA from a particular table or query. For example:
Student_ID, Subject_ID
1001, 11001
1001, 11023
1001, 11049
1002, 11023
1002, 11046
1003, 11023
etc
So a student can appear in the list a variable number of times depending on how many subjects they are enrolled in. Some students may be in 10 subjects some maybe in as little as one. I want to be able to transpose the information stored in this table and export it in a linear format something similar to.
Student 1, subject 1, subject 2, subject 3 ... subject n
student 2, subject 1, ,,,subject n
Like other databases, Access does not offer native functions that will do this. One could use subqueries to return columns for each subject as above. However, the following limitations apply:
- In a Jet SQL solution, you would have to know how many detail values to allow for, and you could not simply let the SQL statement determine it dynamically
- You could use VBA to dynamically generate a SQL statement, but that is beyond the skill of beginning Access developers, and many intermediate developers as well
- Any such scheme relying on subqueries would require a column providing an ordinal for the detail items within the group
To overcome these obstacles, and to make it easier for most developers to seamlessly integrate this sort of functionality into their Access projects, I decided to create a UDF patterned after the more familiar "domain aggregate" functions such as DSum or DCount. So, the "DConcat" function was created. As the name suggests, this function concatenates the values of a detail column (or detail columns) based on the groupings suggested by criteria the user supplies.
- 2
The DConcat() Function
Here is the VBA source code for DConcat:
The DConcat function will produce a concatenated list of all of the values for the specified column(s) in a specified table or query pertaining to the records that meet the criteria specified in the Criteria argument (or all rows in the table/query if no criteria are specified).
The DConcat function takes the following arguments:
- ConcatColumns are the detail columns whose values are to be concatenated. This will usually be a single column, but can be multiple columns. If more than one column, use commas to delimit the column names. If column names contain anything other than letters, numerals, or underscores, column names must be enclosed in square brackets
- Tbl is the name of table or query from which the data are taken. If the table/query name contains anything other than letters, numerals, or underscores, the name must be enclosed in square brackets
- (Optional) Criteria specifies the criteria to be applied in selecting and grouping the detail information. To specify multiple criteria, and And or Or. Be sure to use single-quotes around text values and # around dates/times. I recommend using square brackets around all column names
- (Optional) Delimiter1 is the delimiter to be used when concatenating each result row to the return value. The default value is comma + space
- (Optional) Delimiter2 is the delimiter to be used when concatenating the values in the ConcatColumns for each result row. The default value is comma + space
- (Optional) Distinct indicates whether all instances of the detail information is returned, or just the distinct items. True by default
- (Optional) Sort indicates whether the detail data are sorted before concatenation. Use "Asc" for ascending (default) or "Desc" for descending. Any other values are ignored. Please note that if multiple columns are specified in ConcatColumns and if you use
- (Optional) Limit specifies an upper limit for how many detail rows are concatenated. If less than one or omitted, then no limit is applied
Each time DConcat is called, the code will run a query structured like this:
The code then loops through that result set:
- If ConcatColumns specifies more than one column, the column values for each returned row are concatenated using the Delimiter2 value as the delimiter
- Those concatenated rows are then concatenated into a larger string, using the Delimiter1 value as the delimiter
- 3
Implementing DConcat in Your Access Projects
To implement DConcat in your Access project, all you need to do is add the source code above to a "regular" VBA module (i.e., not a class module, nor a code module for a Form or Report). Once you have done this, you will be free to use the function in your queries, forms, or reports in the Access UI, or you may call DConcat from your other procedures in the VBA project.
- 4
Examples
The attached sample file, "DConcat.mdb", includes sample data for you to practice using this function. The database includes the following five example queries:
Simple list of all accounts (qryAllAccounts)
List of projects for each account (qryProjectsByAcct)
List of users for each account and project (qryUsersByAcctAndProject)
List of tasks and users by project (qryTasksAndUsersByProject
List of up to 3 tasks and users by project (qryTasksAndUsersByProject
=-=-=-=-=-=-=-=-=-=-=-=-=-
If you liked this article and want to see more from this author, please click here.
If you found this article helpful, please click the Yes button near the:
Was this article helpful?
label that is just below and to the right of this text. Thanks!
=-=-=-=-=-=-=-=-=-=-=-=-=-
by: keith_alabaster on 2010-03-04 at 03:43:00ID: 10432
Cracking - yes from me