How to keep multiple selections from a combo box together on an Access report

Posted on 2016-08-27
Medium Priority
Last Modified: 2016-10-15
I am creating a report, based on a query that has many tables.  Everything is working great, except that the information from multi-select combo boxes are carrying over to a separate page, instead of keeping in line with the other responses.  I've managed to stop every field from duplicating just because there are multiple responses to this control, but what I want is for the report to look like this (what I am having trouble with is the Sections):

Township/Range  17N 3W        Sections  1,2,3,8,9
                                 18N, 3W                        31,32

My main issue is that I don't know what I am asking for.  Sorry!!!
Question by:Linda Nichols
  • 6
  • 4
LVL 24
ID: 41773693
it can be done with code but it would be much easier and much better for you to use a related table to store the choices instead of using a multi-select field

> "I've managed to stop every field from duplicating"

how was that done?
LVL 24
ID: 41773709
also set the Keep Together property of the section to True (Yes)

Author Comment

by:Linda Nichols
ID: 41773764
I set the Hide Duplicates to Yes on the property sheet of the report.
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

LVL 24
ID: 41773766
is Sections a multi-value field?

btw, 'Sections' is a reserved word and should not be used as a name:

also, best to create a table for multiple values rather than use a MV field, which is very hard to control

if you are worried because you already have a lot of data, I have code that will take a day or 2 to post that will do the separation for you.  You will have to tell me a bit about your structure.

Also have code to loop and combine values so you can still get, ie, "1,2,3,8,9"

Author Comment

by:Linda Nichols
ID: 41773772
Just to clarify, I set the hide duplicates for each control that it pertains to.  I tried setting the Keep together on the Township/Range  Sections controls, but then the whole sheet duplicates for just a change in section.  I couldn't find where it says "true."  I have Access 2016 and Keep whole group  together on one page  is a selection
LVL 24
ID: 41773773
Keep Together would be set for the section that the controls are in -- not for each control. True and Yes are the same thing. In code, Yes would be True or -1.

thanks, Linda -- also please read my last comment if you still have issues

Author Comment

by:Linda Nichols
ID: 41773782
I have it set up as a separate table to store just the townships and sections.  Not sure what to change the name Sections to, as it refers to the actual geographic area.   Here is a screenshot of how the table receives the data:
LVL 24

Accepted Solution

crystal (strive4peace) - Microsoft MVP, Access earned 2000 total points
ID: 41773799
great!! change Section to Sectn ... just something different that a human can still understand ;)

here is code to loop through a recordset and combine values, and sort them, that you can call. The first routine can be changed for what you want so that you can test it. The secondroutine is what you would call in SQL:
 '~~~~~~~~~~~~~~~~ RunLoopAndCombine
Sub RunLoopAndCombine()
'test LoopAndCombine
   Dim sTablename As String, _
   sIDFieldname As String, _
   sTextFieldname As String, _
   nValueID As Long
   sTablename = "usys_qPIDFriendlyT"
   sIDFieldname = "PID"
   sTextFieldname = "FriendlyT"
   nValueID = 138
   MsgBox LoopAndCombine(sTablename, sIDFieldname, sTextFieldname, nValueID)

End Sub

'~~~~~~~~~~~~~~~~ LoopAndCombine
Function LoopAndCombine( _
   psTablename As String _
   , psIDFieldname As String _
   , psTextFieldname As String _
   , pnValueID As Long _
   , Optional psWhere As String = "" _
   , Optional psDeli As String = ", " _
   , Optional psNoValue As String = "" _
   , Optional psOrderBy As String = "" _
   ) As String
'loop through recordset and combine values to one string
   'a Microsoft DAO Library
   ' -- OR --
   ' Microsoft Office #.0 Access Database Engine Object Library
   'psTablename --> tablename to get list from
   'psIDFieldname --> fieldname to link on (ie: "BookID")
   'psTextFieldname --> fieldname to combine (ie: "PageNumber")
   'pnValueID --> actual value of ID for this iteration ( ie: [BookID])
   'psWhere, Optional  --> more criteria (ie: "Year(PubDate) = 2006")
   'psDeli, Optional  --> delimiter other than comma (ie: ";", Chr(13) & Chr(10))
   'psNoValue, Optional  --> value to use if no data (ie: "No Pages")
   'psOrderBy, Optional  --> fieldlist to Order By
   'Set up error handler
   On Error GoTo Proc_Err
   'dimension variables
   Dim rs As DAO.Recordset _
      , vAllValues As Variant _
      , sSQL As String
   vAllValues = Null
   sSQL = "SELECT [" & psTextFieldname & "] " _
       & " FROM [" & psTablename & "]" _
       & " WHERE [" & psIDFieldname _
       & "] = " & pnValueID _
       & IIf(Len(psWhere) > 0, " AND " & psWhere, "") _
       & IIf(Len(psOrderBy) > 0, " ORDER BY " & psOrderBy, "") _
       & ";"
   'open the recordset
   Set rs = CurrentDb.OpenRecordset(sSQL, dbOpenSnapshot)
   'loop through the recordset until the end
   With rs
      Do While Not rs.EOF
         If Not IsNull(.Fields(psTextFieldname)) Then
            '~~~~~~~~~~~~~~~~~~~~~~~~~ CHOOSE ONE
            '---- if field value is numeric
            vAllValues = (vAllValues + psDeli) _
             & Trim(.Fields(psTextFieldname))
            '---- uncomment if you want quotes around data
            'vAllValues = (vAllValues + psDeli) _
             & " '" & Trim(.Fields(psTextFieldname)) & "'"
          End If
   End With 'rs
   If Len(vAllValues) = 0 Then
      vAllValues = psNoValue
   End If
   'close the recordset
   'release the recordset variable
   Set rs = Nothing
   LoopAndCombine = Trim(vAllValues)
   Exit Function
'if there is an error, the following code will execute
   MsgBox Err.Description, , _
     "ERROR " & Err.Number _
      & "   LoopAndCombine"
   Resume Proc_Exit
End Function

Open in new window

If you want help on error-handling, here are some links to video on EE:

basic error handling code for VBA (3:48)

Error Handling Part 3 - Run and Fix Bugs (7:51)

Author Comment

by:Linda Nichols
ID: 41773804
Thank you!  I will try this out.  I need to get my report done for tomorrow, sigh.
LVL 24
ID: 41773807
you're welcome

> "I need to get my report done for tomorrow, sigh."

no problem, Linda ... and now I realize my "run" procedure doesn't exactly have good names ... copied what I used for something else -- just put the code into a new module, Debug, Compile, and Save. Then modify values in the "run" procedure -- Debug, compile, and Save again. then click in the "run" procedure and press F5 to test. Once you are comfortable with the parameters, you can make the substitution in the SQL (for query or record source).

Made a couple substitutions in the "run" procedure to help:
Sub RunLoopAndCombine()
'test LoopAndCombine
   Dim sTablename As String _
      , sIDFieldname As String _
      , sTextFieldname As String _
      , nValueID As Long _
      , sFieldSortBy As String
   sTablename = "MyTablename"
   sIDFieldname = "MyNumericForeignKeyFieldname" 'if FK is not a n umber, you will need to add delimiters to LoopAndCombine where it is referenced
   sTextFieldname = "Description of Fieldname"
   nValueID = 138 'some number you know is in the table
   sFieldSortBy = "FieldnameToSortBy"
   MsgBox LoopAndCombine(sTablename, sIDFieldname, sTextFieldname, nValueID, , , , sFieldSortBy)

End Sub

Open in new window

*** How to Create a Standard (General) Module ***

Press Alt-F11 to go to the VBE (Visual Basic Editor)

From the menu in a the Microsoft Visual Basic window:
Insert --> Module

once the code is in the module sheet, from the menu, do -->

if there are no syntax/reference errors, nothing will appear to happen -- in this case, nothing happening is good ;)

Make sure to give the module a good name when you save it.  You can have several procedures (Subs and Functions) in a module, which gives you a way to categorize them ... ie: basic procedures that would be useful in any database; procedures that are specific to a particular database; procedures for converting data; etc

~~~~~ also be sure to use Option Explicit at the top of each module so variables that are not declared or are misspelled will be picked up
Option Explicit ' require variable declaration

Open in new window

If you have more questions, we can help ... you will get there :)

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

I have had my own IT business for a very long time. I started mostly with hardware and after about a year started to notice a common theme. I had shelves with software boxes -- Peachtree, Quicken, Sage, Ouickbooks -- and yet most of my clients were…
Implementing simple internal controls in the Microsoft Access application.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…

569 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