473,666 Members | 2,039 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 "tblContact s" that holds my Contact Info. Because there can be multiple Contact Types for a Contact, I have a second table "tlnkContactTyp es" in which there is a record for every Contact / Type combination.

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

tlnkContactType s: 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 tlnkContactType s into one field.

So tlnkContactType s
ContactID ContactType
1 Resident
1 Super
2 Resident
2 Teacher
3 Teacher

is transformed to qryContactTypes Cnct

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

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

I cannot figure out how to make it enter a "blank" value for Contacts that are not in qryContactTypes Cnct. 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 = "tlnkContactTyp es"

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

SELECT tlnkContactType s.ContactID, Conc("ContactTy pe","ContactID" ,[ContactID],"tlnkContactTy pes") AS ContactTypes
FROM tlnkContactType s
GROUP BY tlnkContactType s.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 2543
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
2100
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 field in any given column has the same control name. So for example, in the last_name column, every row in the table would contain an input field (of type "text") with the name "last_name". Is this safe to do? I know that multiple radio...
2
2234
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 record for each marriage. Each record has a unique Autonum field (ID), and also contains two fields IDPartnerMale and IDPartnerFemale. Both of the latter fields match records in the Person table via IDPerson.
0
2931
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 separate sheet within spreadsheet is created. Particular fields are selected. User requires fields to be auto-fitted. Problem, is that some tables have more than 1200> rows. Code generates error message. Is there a way of getting around this...
8
3889
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 = "'Record ID' = '0'"; theView.RowStateFilter = DataViewRowState.ModifiedCurrent; Debug.WriteLine(string.Format("RowFilter = {0}", theView.RowFilter)); RecordDataGrid.DataSource = theView; RecordDataGrid.DataBind();
4
1543
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 person is editing the record, or does this type lock the record such that it is unavailable until the editor releases it? An explanation of pessimistic and optimistic lock types would be really useful, as would some example code.
4
11789
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" & ", " & "employee_last_name", but it did not like that. I can fill the combo box with either the first or the last, but I cannot manage to concatenate it.
24
8492
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 workaround for lack of a way to define the ordinal position of a field (incrementing a counter variable), but it feels so primitive: dim Fld as Field dim rst1 as new adodb.recordset
11
1978
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 function is used (when the form opens) all runs well and everyone is happy. Then comes the problem - values are added to the recordset (ADO) that the callback function uses to populate the listbox. After the new values are added to the recordset, the...
58
8065
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 the parts section an i seem to be having trouble. When i try to insert into the parts section i get the error Invalid character value for cast specification. But not sure what i am doing wrong. Here is what i am using to insert. All the sections...
6
2889
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 rows in this beside sum i need to sum of the tax for ex: in column1 i added 300 and into 2-nd column i Chose 10% i would like when i chose from drop down box 10% Let in the 2-nd column appear 10% of the value which i addee first column for ex...
0
8444
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8356
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8869
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8639
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7386
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6198
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5664
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4198
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2771
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.