473,396 Members | 1,784 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

populating textfield from multiple records

Hi,

I'm trying to make a calculated text field in a query where the textvalue is
being populated from multiple records.
I already use a VBA function to loop through records and concatenate the
text. It works but the performance is really bad. I'm looking for a solution
in plane SQL, so the looping is done by joining tables. I can't get it to
work....
tblPersons
Persid name
------ ------
1 Dck
2 Peter
3 John
4 Paul
5 Oliver

tblHobbies
Persid Hobby
------ -------
1 Cars
1 Fishing
1 Movies
1 Horses
2 Cars
2 Music
3 Golf
3 Cars
3 Hunting
4 Cooking
5 Cars
5 Music

in the query i like to have the following data presented in two fields.

Name Hobbies
------ ---------
Dick Cars, Fishing, Movies, Horses
Peter Cars, Music
John Golf, Cars, Hunting
Paul Cooking
Oliver Cars, Music

I almost thought i had the solution by using GROUP BY and EXPRESSION in a
total query where a VBA function is called and populates the strings through
a STATIC string variable and STATIC long recordid. But unfortunatly it
didn't work well, got problems with initializing STATIC vars (Also in
genereal it seems that during debugging a function called from a running
query doesnot always return the expected results, while in normal mode the
query will run fine).

Any help would be gratefull
Thanks!
Nov 12 '05 #1
3 5592
Don't think pure SQL will do it. You have to use code. There's code for it here:

http://www.mvps.org/access/modules/mdl0004.htm

"Return a concatenated list of sub-record values"
Nov 12 '05 #2
Hi,

I still don't know if it can be done with pure SQL, but i got it working
without VBA code which loops through records. (Not to mention the time lost
for opening and closing the recordset.)

The trick is using two queries and a bit of VBA code. It works faster than
looping through records in VBA.

1. The base query GROUPS some Recordkey and GROUPS the Textfield you want to
populate. In a 3th column a calculated textfield as EXPRESSION calls a
public function fnPopulateTextfield which takes to params (Recordkey and
Textfield).

RecID
GROUP BY

Textfield
GROUP BY

PopulatedText: fnPopulateTextfield([RecID];[Textfield])
EXPRESSION

The second column is needed because without it the query does not recognizes
that Textfield is part of a statistic function! The side effect is that it
will group on every Textfield, but the final query will correct this!

2. The finishing query based on the above query GROUPS again on the
Recordkey and takes the LAST record of the calculated populated textfield.

RecID
GROUP BY

PopulatedText
LAST

3. The function fnPopulateTextfield uses two STATIC vars (long and string).
every time it sees a different recordkey it reinitializes the STATIC vars
(Long var gets new recordkey and the String var gets empty). Next it adds
the Textfield to the STATIC string var seperated by comma and then returns
the STATIC string var.

Lets say you have a table like the next one :

RecID SomeTextfieldYouWantToPopulate
1 Cat
1 Dog
2 Fish
3 Lion
3 Birth
3 Snake
4 Dog
4 Crock
4 Hyena
4 Fish

After the base query you will get:

RecID PopulatedTextfield
1 Cat
1 Cat, Dog
2 Fish
3 Lion
3 Lion, Birth
3 Lion, Birth, Snake
4 Dog
4 Dog, Crock
4 Dog, Crock, Hyena
4 Dog, Crock, Hyena, Fish

What the f*#%! Don't worry after the final query you will get:

1 Cat, Dog
2 Fish
3 Lion, Birth, Snake
4 Dog, Crock, Hyena, Fish

Voila!

My own code was too complex to show the basic principle, sorry!

Good luck and if you believe hard enough it will happen!

Sidney.

"Pieter Linden" <pi********@hotmail.com> schreef in bericht
news:bf**************************@posting.google.c om...
Don't think pure SQL will do it. You have to use code. There's code for it here:
http://www.mvps.org/access/modules/mdl0004.htm

"Return a concatenated list of sub-record values"

Nov 12 '05 #3
G'day

The word you need is "concatenate". I hadn't had a use for that word
either until I started fiddling with this stuff.

Anyway try this for a simple concatenation of two fields that both have
values. The quotes simply put a space between the two words it is the
ampersands that do the joining.

=[Name]&" "&[Hobby]

if you have a group of possibly empty fields you might like to use a
series of immediate ifs such as

=iif(IsNull([Hobby1]),"",[Hobby1]&" ")&
iif(IsNull([Hobby2]),"",[Hobby2]).....

You will see I have also put the intermediate spaces into the iif
expression so that you do not create spaces if the fields have a null
value.

I frequently use that sort of code for concatenating (there's that word
again) town state and postcode fields so they don't look too clunky on
letters

Hope that helps

Cheers
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 12 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Suzanne | last post by:
Hi, I have a form which our clients can fill in with their personal details. As part of the information we store there is a section - areas of interest - this is a number of radio buttons. ...
7
by: Drew | last post by:
I have a db table like the following, UID, int auto-increment RegNo Person Relation YearsKnown Now here is some sample data from this table,
6
by: nboutelier | last post by:
Is it possible to apply multiple css styles to a textfield. Eg, if the value of the textfield is "foo bar" id like for 'foo' to be arial and 'bar' to be verdana. Or... id like for 'foo' to be...
1
by: John Hargrove | last post by:
I am building a database to manage test samples in an environmental laboratory. I am learning Access as I go and don't know much about the programming aspects. I hope to make the application...
4
by: Lumpierbritches | last post by:
Thank you once again for any and all assistance. I'm building an application that's getting quite bulky due to the number of forms, macros and procedures. I was wondering if there's a way to use 1...
8
by: Patrick McGuire | last post by:
I have a treeview control on a windows form that I want to populate in the form's load event. The problem is that the datatable I am using to populate it contains >20,000 records, and it takes > 1...
6
by: eureka | last post by:
Hi friends, I am developing a web application using Jsp and JS. I have a main Jsp page(Jsp1).Inside it I have an iframe having an Html- table which is created dynamically and contains all...
4
by: Don Do | last post by:
Help I built a form/subform/subsubform setup using the access forms wizard. I have a table1 = parent, table2 = child, table3 = (grandchild?). There will be multiple records in table2 that tie...
3
by: davenumber40 | last post by:
I’m creating a small database in Access 2003(XP) to track issues during software testing. As far as databases go, it’s going to be a relatively small, short term project (No more than 20,000 records...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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,...

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.