0

In my project, I have a query that fetches the right values, but when I use the table as the rowsource for a listbox, it doesn't display the same values.

For my example, I am using Star Wars Scenes.

There are two Tables: Characters, Scenes and one Form.

The Characters Table has these columns: ID and Title. It is filled with names of Star Wars characters.

The Scenes Table has these columns: ID, Title and Appearances. Appearances is a multi-select column and performs a lookup against the Characters.ID field. Here is the lookup string for that column:

SELECT Characters.[Title], Characters.ID FROM Characters ORDER BY Characters.[Title]; 

Here is where the trickiness happens. Although the Appearances field is bound to Characters.ID, it displays Characters.Title Instead of displaying the IDs in the table, I want it to display the names for human readability. Notice how the sql string is set toCharacters.IDas the *second* column - *AND* - it is that column which is bound. This keeps the table bound to theIDcolumn properly, but since the *first* column is theTitlethen it is theTitle` that displays in the table when humans look at it.

Lookup Column for Scenes.Appearances Lookup Column for Scenes.Appearances

When I view the Scenes table, I see the names of the characters in the Appearances column, separated with commas - perfect!

Scenes Table Displaying Titles Instead of IDs Scenes Table displaying titles instead of ids

However, when I use this table as the rowsource for a listbox, I want the same effect but the listbox only displays the ID instead (I assume it is because that is the bound column).

How do I make the listbox display like the Table does? I am hoping there is a simple trick to get the listbox to display the same way as the query does. The table displays the names from the non-bound column, but the listbox does not, even though the listbox is set to display the same table. Maybe there's a better way to go about this? Or is this a bug?

Listbox Displaying Scenes Table Listbox displaying Scenes Table

I have been looking at this question, which is similar, but can't figure this out.

I think I've described everything well enough, but here is my sample database; in my google drive. Here is a good explanation of the multi-select field in the Scenes table.

bgmCoder
  • 6,205
  • 8
  • 58
  • 105
  • are you sure the query isn't working? Have you set the display properties of the box to expect multiple columns and show the ones you want? – Mike M Jun 22 '18 at 01:20
  • ( don't forget to maybe post the actual query ;) ) – Mike M Jun 22 '18 at 01:21
  • The regular query works (Query1), but it's the sql string query that displays numbers instead of text. I even tried copying the sql string from the regular query. But I need to use it as rowsource so I can edit it first and add to it in the VBA code. – bgmCoder Jun 22 '18 at 01:50
  • I'll try and get a minimal query string. – bgmCoder Jun 22 '18 at 01:57
  • Yeah, put some more specific examples... you have good descriptions, but add the specific example queries -- good and bad -- and show us your Properties for the box, such as screenshots. On top of that, you could even show us screen shots of the actual control at runtime. – Mike M Jun 22 '18 at 04:12
  • I've re-written the question with a better description of the problem in the most minimal way possible. – bgmCoder Jun 23 '18 at 16:07
  • I'm still not entirely sure where the problem is... if you understand how to set the BoundColumn property and also the Column Count and Column Widths, you shouldn't have a problem. As you said, you seem to have a working example....it seems like all your pictures and stuff are what is _working_ ... maybe can you just show us - not tell - the not working example? – Mike M Jun 24 '18 at 04:05
  • For the single list box that is _not working_, what is the RowSource, ColumnCount, ColumnWidths, BoundColumn --- and then what is not working as expected? – Mike M Jun 24 '18 at 04:06
  • @MikeM I actually posted a picture of what is not working, you know... And I asked the question of how to make them both look alike... In the listbox, it is supposed to display the NAMES, not the IDs - just like it does in the actual Table. That's the problem. – bgmCoder Jun 24 '18 at 04:29
  • Oh -- I think I got you. You're talking about the columns "Appearances" and "Location". I think the one answer given is on track, then. The automatic lookup bound to the table is not getting added to the query. You have to write that join into your RowSource query. That means writing a query with a Join to the lookup table. – Mike M Jun 24 '18 at 13:20
  • I'm stuck. I can't figure out how to do this and get the character's names into one column in place of the IDs, not even with another query and a join. Can you give an answer? – bgmCoder Jun 24 '18 at 18:04
  • Ok... So how is the data in Characters --- does each row contain multiple character ids? That is, for Mos Eisley, is there only one row as { "1" ; "1,18" } ? Or is it two rows, as { "1" ; "1" } and then also { "1" ; "18" } ? – Mike M Jun 24 '18 at 21:42
  • `Title` in the `Characters` table is a multiple-select checkbox. When you go into the `Scenes` table to add new rows and click on the `Appearances` field, you get the dropdown list for the `Title` column from the `Characters` table, and you can select multiple items. 'Appearances' in the 'Scenes' table is a lookup column with multiple-selections checked-enabled. This column does a lookup on `Characters` - I provided the lookup query string. The `Character` Table itself has only two columns: `ID` and `Title` - it has no lookups or anything - just a flat table. – bgmCoder Jun 25 '18 at 01:14
  • Sorry, I can't translate all that into an answer to what I asked. If you want more ideas about writing a Join, we have to know the data structure. I know Characters has only two columns. What is in the columns? Does ID always have just one number, or does it have a comma-separated list of numbers? – Mike M Jun 25 '18 at 12:51
  • I posted a link to the database in my question some time ago. That would answer all those questions. ID and Title ID is just a number, Title is the name of someone, like "Chewbacca". – bgmCoder Jun 25 '18 at 16:47

1 Answers1

3

You are using lookup fields - a "smart" feature that can be useful in the query (or table) itself but otherwise cause nothing but trouble - as you see now.

The values you see are the IDs related to Bob, Harry, Justin. Move those to a separate table, join this table in the query, and retrieve the names for your form.

Edit:

To collect and concatenate the names, first create a simple query like:

SELECT 
    tblChild.FK, 
    tblChild.Name
FROM 
    tblChild
WHERE 
    tblChild.FK = [Key];

Save this as, say, QueryConcat.

Then modify your original query to include the function below using this query and specifying the separator, like:

SELECT 
    tblParent.ID, 
    tblParent.SomeField, 
    ConcatenateRecords("QueryConcat", [ID], "Name", " ,") AS Names
FROM 
    tblParent;

And the function:

Public Function ConcatenateRecords( _
  ByVal strSource As String, _
  ByVal lngKey As Long, _
  ByVal strField As String, _
  Optional ByVal strSeparator As String = ";") _
  As String

' Concatenates values from one field (strField) from all
' records in query strSource using parameter Value lngKey.
' Values are separated by strSeparator.
' Default output like:
' 34;56;34;67;234
'
' 1999-10-12. Cactus Data ApS, CPH

  Dim dbs         As DAO.Database
  Dim qdf         As DAO.QueryDef
  Dim rst         As DAO.Recordset
  Dim fld         As DAO.Field

  Dim booPluralis As Boolean
  Dim strFields   As String

  On Error GoTo Err_ConcatenateRecords

  Set dbs = CurrentDb()

  If Len(strSource) > 0 And Len(strField) > 0 Then
    Set qdf = dbs.QueryDefs(strSource)
    qdf.Parameters(0) = lngKey
    Set rst = qdf.OpenRecordset()
    Set fld = rst.Fields(strField)

    With rst
      While Not .EOF
        If booPluralis = True Then
          ' There is more than one record.
          ' Add separator.
          strFields = strFields & strSeparator
        End If
        strFields = strFields & Trim(fld.Value)
        booPluralis = True
        .MoveNext
      Wend
      .Close
    End With

    Set fld = Nothing
    Set rst = Nothing
    Set qdf = Nothing
  End If

  Set dbs = Nothing

  ConcatenateRecords = strFields

Exit_ConcatenateRecords:
  Exit Function

Err_ConcatenateRecords:
  MsgBox "Error " & Err.Number & ". " & Err.Description
  Resume Exit_ConcatenateRecords

End Function

MV table field:

enter image description here

Gustav
  • 53,498
  • 7
  • 29
  • 55
  • Yes, I am aware that those numbers are the IDs. The multi-select field pulls the IDs because they are in column 1 which is bound, but it is also set to display column 2. This works in the query, but not in the form's listbox. So, I'm going to have to look up the IDs, I see. – bgmCoder Jun 22 '18 at 14:37
  • The IDs *are* in a separate table, one by one with their IDs and names. The second table pulls them as a multi-select because I need them grouped together under one entry. – bgmCoder Jun 23 '18 at 02:25
  • Well, that's just another "smart" feature - but limited, as you see. – Gustav Jun 23 '18 at 07:03
  • You said that above. If I didn't use a multi-select to choose the IDs into one field, how else would you do that? I'm trying to find a way to use the "smart feature" to display a column besides the bound column. – bgmCoder Jun 23 '18 at 15:02
  • I've narrowed down my problem and have created a sample database. I'm going to re-write my question to be more precise. You have pointed out what the problem is, but not really the solution. – bgmCoder Jun 23 '18 at 15:50
  • The solution is simply to use straight tables and queries, nothing fancy. Think of if as tables and queries never are for display - that's what forms and reports are for. – Gustav Jun 23 '18 at 16:15
  • Can you point me to an example of how to put all three items into one field? – bgmCoder Jun 23 '18 at 16:41
  • Yes. As said, get rid of the lookup table, move those appearances to a separate table, and join that as any other table. – Gustav Jun 24 '18 at 08:59
  • If you do that, you get one item for each entry on the join, not one field with three items in a list. – bgmCoder Jun 24 '18 at 17:39
  • You seem to be saying to NOT use the multiple select field. How do I get the multiple items into one column then? You're not giving me enough information to solve the problem. – bgmCoder Jun 24 '18 at 17:47
  • That's the next step. If you remove the downvote, I'll look it up for you. – Gustav Jun 24 '18 at 19:38
  • nice addition... a useful technique – Mike M Jun 26 '18 at 13:27
  • Thanks, Gustav - I'm beginning to see the light... I'm a little stuck though. I know you said to get rid of the lookup, but do I have to get rid of the multi-select field altogether from my `parent` table? Right now I have `Characters` which has only the `ID` and `Title` for each single character. In my original query, if I concat records from the query based on `Characters` (the QueryConcat), I only get one name. It seems like I need to use the function against the multi-select column, but Access doesn't like that - says the expression isn't valid. – bgmCoder Jun 27 '18 at 15:21
  • I guess the multi-select *IS* the lookup. I don't understand how I would get the multiple values into one field on the parent table... – bgmCoder Jun 27 '18 at 15:47
  • That's an option of the object type _Recordset2_ … it can load a recordset (table) into a field. If you pull the table into a query in design view, it should show up (see picture appended to the answer). – Gustav Jun 27 '18 at 16:31
  • @Gustav I still haven't quite figured this out. I don't have (or want) a form for entering the data into table2, and I need the multi-select column there. Maybe I'm missing something? So, I'm trying a different approach with the multi-select field, but it isn't working either: https://stackoverflow.com/questions/51110666/how-to-pass-multi-select-field-in-query-to-vba-function – bgmCoder Jun 30 '18 at 00:05
  • Well, I refrain from multivalued fields as they cause a lot of trouble later. If you insist, study this guide: [Using multivalued fields in queries](https://support.office.com/en-us/article/Using-multivalued-fields-in-queries-6F64F92D-659F-411C-9503-B6624E1E323A#bm4_6). – Gustav Jun 30 '18 at 08:23