Results 1 to 10 of 10
  1. #1
    ecology is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jul 2015
    Posts
    6

    Crosstab with multiple character fields

    I have data on individuals that may be in one of four areas (A, B, C, D, say) in any given year.

    George 2000 A
    George 2000 B
    John 2000 D
    Paul 2000 C
    Paul 2001 C
    Ringo 2001 A
    Ringo 2001 B
    Ringo 2001 D

    I wish to have a crosstab query that has individuals as rows, years as columns, and the area(s) seen as value.

    2000 2001
    George AB
    John D
    Paul C C


    Ringo ABD

    If I select "first" I get the area it was seen during the first record for the year. If I select "last" I get the final area. However, I wish to see all of them, up to four. Can anyone help on how to accomplish this?

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Cannot do a conventional crosstab query with that data as is for the output you want. If you want the areas combined into a single field for each individual for each year, use VBA code. Review: http://allenbrowne.com/func-concat.html

    Whether or not a crosstab can be based on the resulting output of the VBA code is not something I've ever tried. This VBA procedure is known to perform slowly with large datasets.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    ecology is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jul 2015
    Posts
    6
    That looks very promising. Thanks.

    How large? I have about 20,000 records, though this particular analysis is not one that I need to do frequently.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    That should not be too large for the function. I don't know if crosstab based on the first query using that function will choke.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    ecology is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jul 2015
    Posts
    6
    Just to let you know. It worked a charm. I set it up in two steps. I used your code to concatenate, then a crosstab to lay out the results in a more friendly format.

    The only thing I struggled with a bit was the syntax for the third argument - the Where clause for filtering values. Got is sorted though. Again, many thanks.

  6. #6
    ecology is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jul 2015
    Posts
    6
    I have a quick followup. I will only be doing this concatenation periodically, but I will then be using the output from it a lot in other queries and reports. Because using the calculated field can make things work slowly (the initial crosstab using it takes ages, and seems to go through the same slow process every time I use it), I would like to save the output from the calculated field as values rather than expressions. My first approach was to try making this a make-table query and see if the new table had these as calculated or as values. However, when I did this I got the error message "A calculated column cannot be saved without a valid expression in the Expression property".

    I am not sure how best to convert the results of the expression into text.

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Major modification of Allen Browne code.

    Instead of calling the function from a query, have code open a recordset of the raw data, loop through the recordset and build string of the related values and write record to table. The loop repeats when some value in recordset changes, such as customerID or whatever is used to define the group.

    Code could be in a button Click event.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  8. #8
    ecology is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jul 2015
    Posts
    6
    You lost me completely. Is it really that hard to convert the output of an expression to text in Access?

    I suppose I will just export it to Excel and re-import it.

  9. #9
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    The output from Allen Browne's function is already text. I don't really know why the MAKE TABLE action has an issue with it.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  10. #10
    ecology is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jul 2015
    Posts
    6
    Final piece that got it working properly. I hope this may be helpful to others.

    I know that in some code, the use of table or field names with spaces can cause problems. I had one of those in my relationship. I renamed so that there was nothing that included a space. I initially had it square-bracketed, which the ConcatRelated() function instructions indicated would be adequate. It worked well enough to display in a select query, but gave an error message in make-table, and dragged every other use of the output from that query to a grinding halt.

    Without the space and square brackets it works just as one might wish.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 3
    Last Post: 08-21-2014, 09:48 AM
  2. Replies: 1
    Last Post: 07-09-2014, 06:50 AM
  3. Replies: 1
    Last Post: 08-13-2013, 09:39 PM
  4. Single character fields through memos
    By whiskers197u8 in forum Import/Export Data
    Replies: 3
    Last Post: 12-07-2012, 07:14 PM
  5. Replies: 5
    Last Post: 05-18-2011, 11:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums