A method for querying the concatenation of multiple-row field values in a group--access

Source: Internet
Author: User

A method for querying a concatenation string of multiple rows of field values in a group

Collect it first, and then study it back.

There is no custom function in the SQL syntax for access, nor is it possible to manipulate query statements flexibly with variables, cursors, subqueries, and so on, as in SQL Server. However, in the access environment, through the process in the module, the powerful VB language can be used to generate query statements of complex requirements. Here is a reply to my today in the Access section, in this case

There is no custom function in the SQL syntax for access, nor is it possible to manipulate query statements flexibly with variables, cursors, subqueries, and so on, as in SQL Server.

However, in the access environment, through the process in the module, the powerful VB language can be used to generate query statements of complex requirements.

Here is one of my replies in the Access section today, as an example of how access relies on the powerful features of VB.

User questions:

--------------------------------------------------------------------------------

My table in Access is as follows:

Comname name Sex

1 company Xiao Wang men

1 company Xiao Li Female

2 company Xiao Zhang Male

I would like to combine the contents of the company with the following:

Comname name Sex

1 company Xiao Wang, Xiao Li Male, female

2 company Xiao Zhang Male

Would I be able to do this in access? Is there any good way to deal with it?

--------------------------------------------------------------------------------

Answer:

----------------------------------------------------------------------------------

Create a custom function in the module for access:

Public Function Combstr (TableName As String, FieldName as String, GroupField as String, Groupvalue as String) as String
Dim ResultStr as String
Dim RS as Recordset
Set rs = currentdb.openrecordset ("Select" & FieldName & "from" & TableName & "where" & Groupfiel D & "= '" & Groupvalue & "")
If Rs. RecordCount > 0 Then
Do and not Rs. Eof
ResultStr = ResultStr & "," & Rs. Fields (0). Value
Rs. MoveNext
Loop
End If
If resultstr <> "Then ResultStr = Mid (ResultStr, 2)
Combstr = ResultStr
  
End Function


To create a query:


Select T.comname, Combstr ("T", "Name", "Comname", T.comname) as Combname, Combstr ("T", "ses", "comname", T.comname) as Combsex
From T
GROUP by T.comname

A method for querying the concatenation of multiple-row field values in a group--access

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.