473,508 Members | 2,326 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem with concatenating fields in the same column from multiple rows

59 New Member
Hello all,
I have a table "tblContacts" that holds my Contact Info. Because there can be multiple Contact Types for a Contact, I have a second table "tlnkContactTypes" in which there is a record for every Contact / Type combination.

tblContacts: ContactID, NmFirst, NmLast. etc...

tlnkContactTypes: ContactID, ContactType (which is selected from value list)

For a summary query I need to Show all of the Contact Types for a contact together. I have found a public function called "Conc" (see below) that does a nice job of concatenating all Contact Types for a ContactID from tlnkContactTypes into one field.

So tlnkContactTypes
ContactID ContactType
1 Resident
1 Super
2 Resident
2 Teacher
3 Teacher

is transformed to qryContactTypesCnct

ContactID ContactTypes
1 Resident, Super
2 Resident, Teacher
3 Teacher

The problem that I have is that not all Contacts are represented in tlnkContactTypes, because a Contact may not have a Type. When I create my summary query based on tblContacts and include qryContactTypesCnct, I get an error message for every record in tblContacts that is not represented in qryContactTypesCnct.

I cannot figure out how to make it enter a "blank" value for Contacts that are not in qryContactTypesCnct. Any advice would be appreciated!

Banderson

For my project, the variables in the function below are define as follows:
Fieldx = ContactType",
Identity = "ContactID",
Value = [ContactID],
Source = "tlnkContactTypes"

I use the public function below in my qryContactTypesCnct SQL like this:

SELECT tlnkContactTypes.ContactID, Conc("ContactType","ContactID",[ContactID],"tlnkContactTypes") AS ContactTypes
FROM tlnkContactTypes
GROUP BY tlnkContactTypes.ContactID;

Expand|Select|Wrap|Line Numbers
  1.  
  2. Public Function Conc(Fieldx, Identity, Value, Source) As Variant
  3.   Dim cnn As ADODB.Connection
  4.   Dim Rs As ADODB.Recordset
  5.   Dim SQL As String
  6.   Dim vFld As Variant
  7.  
  8.   Set cnn = CurrentProject.Connection
  9.   Set Rs = New ADODB.Recordset
  10.   vFld = Null
  11.  
  12.   SQL = "SELECT [" & Fieldx & "] as Fld" & _
  13.         " FROM [" & Source & "]" & _
  14.         " WHERE [" & Identity & "]=" & Value
  15.  
  16.   ' open recordset.
  17.   Rs.Open SQL, cnn, adOpenForwardOnly, adLockReadOnly
  18.  
  19.  
  20.   ' concatenate the field.
  21.   Do While Not Rs.EOF
  22.     If Not IsNull(Rs!Fld) Then
  23.       vFld = vFld & ", " & Rs!Fld
  24.     End If
  25.     Rs.MoveNext
  26.   Loop
  27.   ' remove leading comma and space.
  28.   vFld = Mid(vFld, 3)
  29.  
  30.   Set cnn = Nothing
  31.   Set Rs = Nothing
  32.  
  33.   ' return concatenated string.
  34.   Conc = vFld
  35. End Function
  36.  
  37.  
Feb 24 '10 #1
3 2537
ADezii
8,834 Recognized Expert Expert
I took a different approach, simply because I am at work and needed the quickest possible solution, for me.
  1. Sample Data from tblContacts:
    Expand|Select|Wrap|Line Numbers
    1. ContactID    ContactType
    2. 1            Resident
    3. 1            Super
    4. 2            Resident
    5. 2            Teacher
    6. 3            Teacher
    7. 4    
    8. 5    
    9. 6            Resident
    10. 6            Teacher
    11. 6            Super
    12. 6            Client
    13. 6            Doctor
    14. 6            Lawyer
    15. 6            Indian Chief
  2. SQL Statement for Query:
    Expand|Select|Wrap|Line Numbers
    1. SELECT tblContacts.ContactID, Conc([ContactID]) AS Types, fTotalOfTypes([ContactID]) AS [Total Types]
    2. FROM tblContacts
    3. GROUP BY tblContacts.ContactID
    4. ORDER BY tblContacts.ContactID;
  3. Function Definitions:
    Expand|Select|Wrap|Line Numbers
    1. Public Function Conc(lngContactID As Long) As String
    2. Dim MyDB As DAO.Database
    3. Dim rst As DAO.Recordset
    4. Dim strSQL As String
    5. Dim strBuild As String
    6.  
    7. strSQL = "SELECT * FROM tblContacts WHERE [ContactID] = " & lngContactID
    8.  
    9. Set MyDB = CurrentDb
    10. Set rst = MyDB.OpenRecordset(strSQL, dbOpenForwardOnly)
    11.  
    12. With rst
    13.   Do While Not .EOF
    14.     strBuild = strBuild & ![ContactType] & ","
    15.     .MoveNext
    16.   Loop
    17. End With
    18.  
    19. Conc = Left$(strBuild, Len(strBuild) - 1)
    20.  
    21. rst.Close
    22. Set rst = Nothing
    23. End Function
    Expand|Select|Wrap|Line Numbers
    1. Public Function fTotalOfTypes(lngContactID As Long) As Long
    2.   fTotalOfTypes = DCount("*", "tblContacts", "[ContactID] = " & lngContactID & _
    3.                         " And [ContactType] Is Not Null")
    4. End Function
  4. Output after Query Execution:
    Expand|Select|Wrap|Line Numbers
    1. ContactID    Types                                                        Total Types
    2. 1            Resident,Super                                                     2
    3. 2            Resident,Teacher                                                   2
    4. 3            Teacher                                                            1
    5. 4                                                                               0
    6. 5                                                                               0
    7. 6            Resident,Teacher,Super,Client,Doctor,Lawyer,Indian Chief           7
Feb 26 '10 #2
banderson
59 New Member
@ADezii
ADezii,
This works beautifully! and solves my problem
Thank you so much for the help.

Banderson
Feb 26 '10 #3
ADezii
8,834 Recognized Expert Expert
You are quite welcome.
Feb 26 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

9
2085
by: Paul Morrow | last post by:
I have seen the technique where a number of rows in a database are displayed in an html table so that each column of each row is editable. They use a single form surrounding the table, where each...
2
2227
by: Lyn | last post by:
Hi, I am working on a genealogy project in which I have two tables: Person -- one record for each person in the family. Each record has a unique Autonum field (IDPerson). Partnerships -- one...
0
2923
by: ImraneA | last post by:
Hi there Have a database, where front-end interface allows user to select a ms access database. From there, standard tables are linked. Routine, that creates a spreadsheet, for each table a...
8
3870
by: Dave Hagerich | last post by:
I'm using a DataGrid with a DataSet and I'm trying to filter the data being displayed, using the following code as a test: DataView theView = new DataView(theDataSet.Tables); theView.RowFilter =...
4
1539
by: Prabhat | last post by:
How do I lock a particular record that one user has opened for editing? If I use the pessimistic type, can other users view the record (but not edit it) and return a message telling that another...
4
11774
by: Elena | last post by:
Hi, I am filling in a combobox. I would like to concatenate two fields into the data combo box and display "last name, first name" I tried to displaymember = "employee_last_name" & ", " &...
24
8461
by: Donald Grove | last post by:
I want to populate an array with values from an ado recordset (multiple rows) I use the absolute position of the cursor in the recordset to define the row of my array to be populated. I have a...
11
1968
by: The Frog | last post by:
Hi all, Maybe I am just missing something simple here, but I seem to have an issue with a callback function in A97 that is used to fill a Listbox with values. The first time the callback...
58
8014
by: bonneylake | last post by:
Hey Everyone, Well recently i been inserting multiple fields for a section in my form called "serial". Well now i am trying to insert multiple fields for the not only the serial section but also...
6
2881
by: azegurb | last post by:
Hello, I have one question again i created one table again and in this table i added some another options for ex at the previous table there were only one problem sum of the dynamically added...
0
7224
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7323
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7379
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7493
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5625
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5049
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3192
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.