473,473 Members | 1,894 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Access - if a row has a same value, how to count those values

1 New Member
Hello,
I am fairly new at this and I have a question. I wanted know how i can take same values within a row and return a count of those values
for example:
Col1 Col2
1 A,B,B,B,C
2 A,A,B,C

I would like it so that its
Col1 Col2
1 A, 2(B), C
2 2(A), B, C

Here is the code I have used
Public Function FixTable() As Boolean
On Error Resume Next

Dim db As DAO.Database, rst As DAO.Recordset, sSQL As String
Dim strColumn1 As String, strColumn2 As String

Set db = CurrentDb()
Call RecreateTables(db)

'Stop
sSQL = "SELECT Column1, Column2 FROM tblOriginal " _
& "ORDER BY Column1, Column2 ASC"
Set rst = db.OpenRecordset(sSQL, dbOpenSnapshot)

If Not rst.BOF And Not rst.EOF Then
rst.MoveFirst
strColumn1 = rst!Column1
strColumn2 = rst!Column2

rst.MoveNext
Do Until rst.EOF
If strColumn1 = rst!Column1 Then
strColumn2 = strColumn2 & ", " & rst!Column2
Else
sSQL = "INSERT INTO tblCopy (Column1, Column2) " _
& "VALUES('" & strColumn1 & "','" & strColumn2 & "')"
db.Execute sSQL
strColumn1 = rst!Column1
strColumn2 = rst!Column2
End If
rst.MoveNext
Loop

' Insert Last Record
sSQL = "INSERT INTO tblCopy (Column1, Column2) " _
& "VALUES('" & strColumn1 & "','" & strColumn2 & "')"
db.Execute sSQL
End If

Set rst = Nothing
Set db = Nothing

DoCmd.OpenForm "frmOutput"

End Function

Private Function RecreateTables(ByRef dbs As DAO.Database)
On Error Resume Next
Dim sSQL As String

' Delete Table, if exists
If DCount("*", "MsysObjects", "[Name]='tblOriginal'") = 1 Then
DoCmd.DeleteObject acTable, "tblOriginal"
End If

sSQL = "CREATE TABLE tblOriginal (Column1 Text(10), Column2 Text(10))"
dbs.Execute sSQL

sSQL = "INSERT INTO tblOriginal (Column1, Column2) VALUES ('A','1')"
dbs.Execute sSQL

sSQL = "INSERT INTO tblOriginal (Column1, Column2) VALUES ('A','2')"
dbs.Execute sSQL

sSQL = "INSERT INTO tblOriginal (Column1, Column2) VALUES ('B','1')"
dbs.Execute sSQL

sSQL = "INSERT INTO tblOriginal (Column1, Column2) VALUES ('B','2')"
dbs.Execute sSQL

sSQL = "INSERT INTO tblOriginal (Column1, Column2) VALUES ('B','3')"
dbs.Execute sSQL

sSQL = "INSERT INTO tblOriginal (Column1, Column2) VALUES ('C','1')"
dbs.Execute sSQL

' Delete Table, if exists
If DCount("*", "MsysObjects", "[Name]='tblCopy'") = 1 Then
DoCmd.DeleteObject acTable, "tblCopy"
End If

' Create Temp Table
sSQL = "SELECT Column1, Column2 INTO tblCopy " _
& "FROM tblOriginal WHERE 1 = 0;"
dbs.Execute sSQL

End Function


PLEASE HELP ME AS I AM FAIRLY NEW AT THIS !!!!
Thanks!
Jul 20 '07 #1
1 2647
Rabbit
12,516 Recognized Expert Moderator MVP
Here's the gist.

1) Split the string into a one-dimensional array using the Split() function.
2) Create a two-dimensional array. The first column holds the value, the second column holds the count.
3) Step through array 1 and check it against the values in the second array. Incrementing the count where necessary and creating a new value if it doesn't exist.
4) Rebuild the string.
Jul 20 '07 #2

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

Similar topics

14
by: Sean C. | last post by:
Helpful folks, Most of my previous experience with DB2 was on s390 mainframe systems and the optimizer on this platform always seemed very predictable and consistent. Since moving to a WinNT/UDB...
3
by: Random Person | last post by:
Does anyone know how to use VBA to relink tables between two MS Access databases? We have two databases, one with VBA code and the other with data tables. The tables are referenced by linked...
1
by: Andrew Arace | last post by:
I scoured the groups for some hands on code to perform the menial task of exporting table data from an Access 2000 database to Oracle database (in this case, it was oracle 8i but i'm assuming this...
7
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I...
11
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on...
4
by: alexandre.brisebois | last post by:
Hi, I am using access 2003, I would like to know if there is an option to reorganize the tables in a maner that is readable, as we can do in sql sever 2000 or 2005. I have been given a database...
11
by: bbasberg | last post by:
Hello, I have been struggling with this problem for DAYS and have googled my heart out as well as reading any books I could get my hands on but I cannot find any specific references to my problem....
4
MMcCarthy
by: MMcCarthy | last post by:
To view Access queries in SQL rather than Access query design - open the query design window and change the view to SQL: Select Statement SELECT FROM ; Append Statement INSERT INTO (, , )...
3
by: agrych | last post by:
hello all, I am trying to pull all of the Sales Receipts data, for a specified date range, into MS Access. So far I am unsuccessful in my attempt. Below is the code that I have so far. FYI, I...
1
MMcCarthy
by: MMcCarthy | last post by:
Access has a number of built-in functions which can be generally used in queries or VBA code. Some of the more common ones are: Note: anything in square brackets is optional Date Functions ...
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
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
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...
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,...
1
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
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
1
muto222
php
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.