473,545 Members | 1,744 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Split and Count text in a field

I have a field as follows:

aa_comp
03, 04, 05
03, 07, 05, 20
03, 06, 07

It is imported from xml (along with other fields), and I am aware of
multiple values in a field is bad normalisation.

I need to count of each text occurrence within the whole field column.
The results would look like:

03: 3
04: 1
05: 2
06: 1
07: 2
20: 1

The possible values range from 01 to 99.
The field could contain 0 values or anything up to 99 values
The delimiter is with a comma.

Please can someone offer some help?
I assume I would use split() and count(), but I am not sure how to go
about
it.

Thanks for any help,
Simon

Jun 15 '06 #1
3 6252
Normalize

Create a second table. Something like:

CREATE TABLE tblNormalized(
RecordID Autonumber,
NormalizedField Text(255)
);

Normalize your data... copy this into a module and run it...

Option Compare Database

Public Sub NormalizeTable( )
Dim rsRead As DAO.Recordset
Dim rsWrite As DAO.Recordset
Dim varValues As Variant
Dim intCounter As Integer

Set rsRead = CurrentDb.OpenR ecordset("tblDe normalized",
dbOpenTable, dbOpenForwardOn ly)
Set rsWrite = CurrentDb.OpenR ecordset("tblNo rmalized", dbOpenTable)

Do Until rsRead.EOF
varValues = Split(rsRead.Fi elds("FieldToPa rse"), ",")
For intCounter = 0 To UBound(varValue s)
rsWrite.AddNew
rsWrite.Fields( "RecordID") = rsRead.Fields(" RecordID")
rsWrite.Fields( "NormalizedFiel d") =
Trim$(varValues (intCounter))
rsWrite.Update
Next intCounter
rsRead.MoveNext
Loop

rsRead.Close
rsWrite.Close
Set rsRead = Nothing
Set rsWrite = Nothing
MsgBox "done"
End Sub

then create a query like this:

SELECT tblNormalized.N ormalizedField, Count(tblNormal ized.RecordID) AS
CountOfRecordID
FROM tblNormalized
GROUP BY tblNormalized.N ormalizedField
ORDER BY tblNormalized.N ormalizedField;

and that's it.

Jun 15 '06 #2
Thanks for you help.
After running the code, my table looks like:

RecordID NormalizedField
1 03 04 05
2 03 07 20 05
3 03 06 07

Is this correct?
Should it have broken each value out into a new field?

Thanks for the help

Simon
pi********@hotm ail.com wrote:
Normalize

Create a second table. Something like:

CREATE TABLE tblNormalized(
RecordID Autonumber,
NormalizedField Text(255)
);

Normalize your data... copy this into a module and run it...

Option Compare Database

Public Sub NormalizeTable( )
Dim rsRead As DAO.Recordset
Dim rsWrite As DAO.Recordset
Dim varValues As Variant
Dim intCounter As Integer

Set rsRead = CurrentDb.OpenR ecordset("tblDe normalized",
dbOpenTable, dbOpenForwardOn ly)
Set rsWrite = CurrentDb.OpenR ecordset("tblNo rmalized", dbOpenTable)

Do Until rsRead.EOF
varValues = Split(rsRead.Fi elds("FieldToPa rse"), ",")
For intCounter = 0 To UBound(varValue s)
rsWrite.AddNew
rsWrite.Fields( "RecordID") = rsRead.Fields(" RecordID")
rsWrite.Fields( "NormalizedFiel d") =
Trim$(varValues (intCounter))
rsWrite.Update
Next intCounter
rsRead.MoveNext
Loop

rsRead.Close
rsWrite.Close
Set rsRead = Nothing
Set rsWrite = Nothing
MsgBox "done"
End Sub

then create a query like this:

SELECT tblNormalized.N ormalizedField, Count(tblNormal ized.RecordID) AS
CountOfRecordID
FROM tblNormalized
GROUP BY tblNormalized.N ormalizedField
ORDER BY tblNormalized.N ormalizedField;

and that's it.


Jun 16 '06 #3

Simon wrote:
Thanks for you help.
After running the code, my table looks like:

RecordID NormalizedField
1 03 04 05
2 03 07 20 05
3 03 06 07

Is this correct?
Should it have broken each value out into a new field?

Thanks for the help

Simon


it should write the normalized child records to "tblNormali zed"
Then you can do summaries on that table.

Jun 16 '06 #4

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

Similar topics

5
18242
by: Cro | last post by:
Hello Access Developers, I'd like to know if it is possible to perform a count in an expression that defines a control source. My report is based on a query. In my report, I want a text box to display the number of times a certain value appears in a certain field (i.e. perform a ‘count'). I will be doing this for many values in many...
1
3707
by: Mike P | last post by:
I am using Split to split get a number of items from a multi line text box. But I need to be able to get the number of elements within the text box (e.g. if the text box has 2,4,8 in it, I want to be able to count that this text box has 3 elements entered into it). My code is below : for (int p=0; p < dgNational.Items.Count; p++) {...
19
10897
by: David Logan | last post by:
We need an additional function in the String class. We need the ability to suppress empty fields, so that we can more effectively parse. Right now, multiple whitespace characters create multiple empty strings in the resulting string array.
11
12579
by: pmarisole | last post by:
I am using the following code to split/join values in a multi-select field. It is combining all the values in All the records into one long string in each record in recordset. Example: I have a recordset with 2 records. The 1st contains the split/joined values: Alan Smir, Jeff Karl The 2nd contains the value: Keith Robb When it updates...
0
378
by: simonjackson500 | last post by:
I have a field as follows: aa_comp 03, 04, 05 03, 07, 05, 20 03, 06, 07 It is imported from xml (along with other fields), and I am aware of multiple values in a field is bad normalisation.
1
4509
by: heckstein | last post by:
I am working in Access 2002 and trying to create a report from our company's learming management system. I am not a DBA and most of my SQL knowledge has been self taught through trial and error. I have created an access query to track the number of training hours for a training group. The query is working except for one piece of data and I hoping...
3
13014
by: amitsoni.1984 | last post by:
Hi, I have to write a code in python to read a matrix from a text file and for that i am using following code. But it gives an error saying "NameError: name 'split' is not defined". Can anyone help me with this. ------------------------------------------------- #!/usr/bin/python import numpy file = open('matrix.txt', 'r')
24
4807
by: garyusenet | last post by:
I'm working on a data file and can't find any common delimmiters in the file to indicate the end of one row of data and the start of the next. Rows are not on individual lines but run accross multiple lines. It would appear though that every distinct set of data starts with a 'code' that is always the 25 characters long. The text is variable...
6
24137
by: ivan.perak | last post by:
Hello, im a beginner in VB.NET... The thing i would like to do is as it follows.... I have a text file (list of names, every name to the next line) which is about 350000 lines long. I would like to split it and create a new file at every lets say 20000 lines... so, the directory output would have to be something like this:
2
12062
by: Pete | last post by:
I need to create a single query (Not a SQL query) against a single table that counts the number of records in the table, where the single field "tmp" contains specific string values If the field contains "AAA" the count is X. if the field contains "CCC" the count is Y. if the field contains "Stop" then count is Z. I have tried several...
0
7653
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. ...
0
7803
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7411
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5322
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...
0
4942
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...
0
3444
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...
0
3439
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1871
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
0
695
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.